1 ;;; thai-word.el -- find Thai word boundaries
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
4 ;; National Institute of Advanced Industrial Science and Technology (AIST)
5 ;; Registration Number H14PRO021
7 ;; Author: Kenichi HANDA <handa@etl.go.jp>
9 ;; Keywords: thai, word break, emacs
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; The used Thai word list has been taken from IBM's ICU4J project
26 ;; (file `thai6.ucs', version 1.4, converted to TIS encoding, with
27 ;; removal of three incorrect entries) to which the following license
30 ;; COPYRIGHT AND PERMISSION NOTICE
33 ;; Copyright (c) 1995-2001 International Business Machines
34 ;; Corporation and others
36 ;; All rights reserved.
39 ;; Permission is hereby granted, free of charge, to any person
40 ;; obtaining a copy of this software and associated documentation
41 ;; files (the "Software"), to deal in the Software without
42 ;; restriction, including without limitation the rights to use,
43 ;; copy, modify, merge, publish, distribute, and/or sell copies of
44 ;; the Software, and to permit persons to whom the Software is
45 ;; furnished to do so, provided that the above copyright notice(s)
46 ;; and this permission notice appear in all copies of the Software
47 ;; and that both the above copyright notice(s) and this permission
48 ;; notice appear in supporting documentation.
50 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
51 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
52 ;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
53 ;; NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
54 ;; COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE
55 ;; FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
56 ;; OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
57 ;; PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
58 ;; TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
59 ;; PERFORMANCE OF THIS SOFTWARE.
61 ;; Except as contained in this notice, the name of a copyright
62 ;; holder shall not be used in advertising or otherwise to promote
63 ;; the sale, use or other dealings in this Software without prior
64 ;; written authorization of the copyright holder.
67 ;; This file implements an algorithm to find Thai word breaks using a
68 ;; dictionary. It is based on the C program `cttex' written by
69 ;; Vuthichai Ampornaramveth <vuthi@nii.ac.jp>.
72 ;; Table of Thai words. It is a nested alist (see `nested-alist-p'),
73 ;; which means that you can easily index the list character by
76 (defvar thai-word-table
77 (let ((table (list 'thai-words)))
79 ;;; The following is indented as this to minimize this file size.
6224 "ÊÁºÙóÒÒÊÔ·¸ÔÃÒªÂì"
10734 (set-nested-alist elt 1 table))
10736 "Nested alist of Thai words.")
10739 (defun thai-update-word-table (file &optional append)
10740 "Update Thai word table by replacing the current word list with
10741 FILE. If called with a prefix argument, FILE is appended instead to
10742 the current word list."
10743 (interactive "FThai word table file: \nP")
10744 (let ((buf (generate-new-buffer "*thai-work*"))
10745 (coding-system-for-read 'thai-tis620)
10746 (table (if append thai-word-table (list 'thai-words))))
10750 (insert-file-contents file)
10751 (goto-char (point-min))
10752 (while (re-search-forward "\\ct+" nil t)
10753 (set-nested-alist (match-string 0) 1 table)))
10755 (setq thai-word-table table)))
10758 ;; Two special Thai characters regarded as suffix of words.
10760 (defconst thai-MaiYaMok (make-char 'thai-tis620 ?\xE6))
10761 (defconst thai-PaiYanNoi (make-char 'thai-tis620 ?\xCF))
10764 ;; Find Thai words starting at POS and return a list of positions of
10765 ;; the Thai word ends. It doesn't move point. LIMIT limits the
10766 ;; maximum position. IGNORE is a list of positions to ignore. It is
10767 ;; assumed that all following characters to LIMIT are Thai. If the
10768 ;; following char is not Thai (i.e., POS is equal to LIMIT), return t.
10770 ;; Note that the longest word position comes first.
10772 (defun thai-find-word-ends (pos limit &optional ignore)
10775 (let* ((char (char-after pos))
10776 (this (cdr (assq char (cdr thai-word-table))))
10779 ;; Look up the following character sequence in `thai-word-table'
10780 ;; character by character.
10783 char (or (char-after pos) 0)
10784 category-set (char-category-set char))
10785 ;; If the current sequence is recorded in `thai-word-table'
10786 ;; (i.e. (car THIS) is 1) and the following Thai character is
10787 ;; not an upper-vowel, lower-vowel, or tone-mark, we have
10788 ;; found a possible word ending position.
10789 (if (and (eq (car this) 1)
10790 (not (or (aref category-set ?2)
10791 (aref category-set ?3)
10792 (aref category-set ?4))))
10794 ;; Skip possible Thai suffices.
10795 (while (or (eq char thai-MaiYaMok) (eq char thai-PaiYanNoi))
10797 char (char-after pos)))
10798 ;; Skip character positions in IGNORE list.
10799 (or (memq pos ignore)
10800 (setq positions (cons pos positions)))))
10801 ;; Set up next loop.
10802 (setq this (and (< pos limit) (cdr (assq char this)))))
10806 ;; Move point forward to the end of Thai word which follows point and
10807 ;; update VEC. VEC is a vector of three elements used to cache word
10808 ;; end positions. The Nth element, if non-nil, is a list of end
10809 ;; points of the Nth word, or t indicating that there is no Thai
10810 ;; character. LIMIT limits the point movement.
10812 (defun thai-forward-word-update-info (vec limit)
10813 (let ((pos (point))
10819 ;; If four succeeding Thai words are found, throw t, otherwise
10822 ;; Start with first vector element.
10823 (setq v0 (aref vec 0))
10825 ;; Update VEC if V0 is empty.
10826 (setq v0 (thai-find-word-ends pos limit))
10829 ;; In case we haven't found any wordbreaks resp. point has
10830 ;; reached LIMIT, exit the catch body.
10831 (if (symbolp v0) ; i.e. nil or t?
10833 ;; OK, V0 holds possible word ends for the current position.
10834 ;; We save V0 for later reference.
10836 (setq v1 (aref vec 1))
10837 ;; Now we try all end word positions to find the next word.
10839 (setq pos (car v0))
10841 ;; Update VEC if V1 is empty, ignoring positions already
10842 ;; found -- for Thai, we need the longest match, so if
10845 ;; start(long-word) = start(short-word1)
10846 ;; end(short-word1) = start(short-word2)
10847 ;; end(short-word2) = end(long-word)
10849 ;; only long-word is used.
10850 (setq v1 (thai-find-word-ends pos limit tried))
10853 ;; If point has reached LIMIT, exit the catch body.
10856 ;; Save SECOND-BEST, if this hasn't been done already.
10857 ;; The `second best' solution is the end position of the
10858 ;; longest first word followed by the longest second word.
10861 (setq second-best (cons v0 v1)))
10862 ;; Update the already tried end word positions.
10863 (setq tried (append tried v1))
10864 ;; Now repeat the whole process to find a third word.
10866 (setq v2 (aref vec 2))
10868 (setq pos (car v1))
10870 (setq v2 (thai-find-word-ends pos limit tried))
10874 (setq tried (append tried v2))
10875 ;; And the same for a fourth word.
10877 (setq pos (car v2))
10878 (setq v3 (thai-find-word-ends pos limit tried))
10881 (setq v2 (cdr v2)))
10882 (setq v1 (cdr v1))))
10883 (setq v0 (cdr v0)))
10887 ;; We found four succeeding Thai words (or LIMIT has been
10888 ;; reached). Move to the end of the first word.
10889 (goto-char (car v0))
10890 ;; Update VEC for the next function call. If no larger word
10891 ;; positions have been found, set the corresponding vector
10893 (if (and (consp v1) (< (car v1) (car (aref vec 1))))
10896 (if (and (consp v2) (< (car v2) (car (aref vec 2))))
10899 (aset vec 2 v3)))) ; exit function successfully
10901 ;; We didn't find four consecutive words. If we have found a
10902 ;; `second best' solution and the length of those two words is
10903 ;; longer than the longest word we can see at the current point,
10904 ;; adopt the second best solution. This decision is based on
10905 ;; heuristic tests.
10906 (if (and second-best
10907 (< (car (aref vec 0)) (car (cdr second-best))))
10909 (goto-char (car (car second-best)))
10910 (aset vec 0 (cdr second-best)))
10911 ;; We finally failed to find a word break. For Thai, the best
10912 ;; solution is to extend the first longest word so that the
10913 ;; end point starts a second word.
10914 (setq pos (or (car (aref vec 0)) pos))
10915 (while (and (< pos limit)
10916 (not (setq positions (thai-find-word-ends pos limit))))
10917 (setq pos (1+ pos)))
10919 (aset vec 0 positions))
10924 ;; Return a list of Thai word boundary positions after the current
10925 ;; point. LIMIT, if non-nil, limits the region to check.
10927 (defun thai-find-word-boundaries (&optional limit)
10929 (setq limit (point-max)))
10931 (let ((vec (make-vector 3 nil))
10934 ;; Loop over all (consecutive) Thai regions by using the
10935 ;; character property `t' until LIMIT is reached.
10936 (while (and (< (point) limit)
10937 (re-search-forward "\\ct+" nil t))
10938 (setq this-limit (point))
10939 (goto-char (match-beginning 0))
10940 (fillarray vec nil)
10941 ;; Check the first word, initializing VEC.
10942 (thai-forward-word-update-info vec this-limit)
10943 ;; Then loop over the remaining words in the current Thai
10944 ;; region, collecting the boundaries.
10945 (while (< (point) this-limit)
10946 (setq boundaries (cons (point) boundaries))
10947 (thai-forward-word-update-info vec this-limit)))
10951 (defun thai-break-words (separator &optional limit)
10952 "Break Thai words by inserting a separator string at word boundaries."
10953 (interactive "sSeparator: ")
10955 (let ((boundaries (thai-find-word-boundaries limit)))
10957 (goto-char (car boundaries))
10959 (setq boundaries (cdr boundaries))))))
10962 (defun thai-forward-word (count)
10963 "Move point forward COUNT words considering Thai word boundaries.
10964 If COUNT is negative, move point backward (- COUNT) words."
10968 (skip-syntax-forward "^w")
10969 (if (looking-at "\\ct+")
10970 ;; We have reached a Thai region, so we must do something
10971 ;; special instead of using forward-word.
10972 (let ((start (point))
10973 (limit (match-end 0))
10976 ;; If thai-forward-word has been called within a Thai
10977 ;; region, we must go back until the Thai region starts
10978 ;; to do the contextual analysis for finding word
10980 (while (aref (char-category-set (preceding-char)) ?t)
10982 ;; OK, we ask for the list of word boundaries in
10984 (setq boundaries (nreverse (thai-find-word-boundaries limit)))
10985 ;; Now we search for the next boundary after START.
10986 (while (and boundaries (<= (car boundaries) start))
10987 (setq boundaries (cdr boundaries)))
10988 ;; Adjust loop for next while loop.
10989 (setq count (1- count))
10990 ;; Now we skip Thai words until the BOUNDARIES list is
10991 ;; empty or count-1 words have been passed.
10992 (if (not boundaries)
10994 (while (and (> count 0)
10996 (setq boundaries (cdr boundaries)
10998 ;; If BOUNDARIES is empty, the word counter is not
10999 ;; zero yet (remember that we have decreased COUNT by
11000 ;; one), so we go to LIMIT. Otherwise go to next
11003 (goto-char (car boundaries))
11004 (goto-char limit))))
11007 (setq count (1- count))))
11008 ;; The symmetrical action for negative values.
11011 (skip-syntax-backward "^w")
11012 (if (aref (char-category-set (preceding-char)) ?t)
11013 (let ((start (point))
11014 (limit (if (looking-at "\\ct+") (match-end 0)
11018 (while (aref (char-category-set (preceding-char)) ?t)
11020 (setq boundaries (thai-find-word-boundaries limit))
11021 (while (and boundaries (>= (car boundaries) start))
11022 (setq boundaries (cdr boundaries)))
11023 (setq count (1+ count))
11026 (while (and (< count 0) boundaries)
11027 (setq boundaries (cdr boundaries)
11030 (goto-char (car boundaries))))))
11032 (setq count (1+ count)))))))
11035 (defun thai-backward-word (count)
11036 "Move point backward COUNT words considering Thai word boundaries.
11037 If COUNT is negative, move point forward (- COUNT) words."
11039 (thai-forward-word (- count)))
11042 (defun thai-kill-word (arg)
11043 "Like kill-word but pay attention to Thai word boundaries.
11044 With argument, do this that many times."
11046 (kill-region (point) (progn (thai-forward-word arg) (point))))
11049 (defun thai-backward-kill-word (arg)
11050 "Like backward-kill-word but pay attention to Thai word boundaries."
11052 (thai-kill-word (- arg)))
11055 (defun thai-transpose-words (arg)
11056 "Like transpose-words but pay attention to Thai word boundaries."
11058 (transpose-subr 'thai-forward-word arg))
11060 (defun thai-fill-find-break-point (linebeg)
11061 "Go to a line breaking position near point considering Thai word boundaries."
11062 (let ((pos (point)))
11063 (thai-forward-word -1)
11064 (when (<= (point) linebeg)
11066 (thai-forward-word 1))
11067 (kinsoku linebeg)))
11069 (provide 'thai-word)
11072 ;; Local Variables:
11076 ;; end of thai-word.el
11078 ;; arch-tag: 29927f02-e177-4224-a270-7e67210b038a