1 ;;; thai-word.el -- find Thai word boundaries
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; The used Thai word list has been taken from IBM's ICU4J project
27 ;; (file `thai6.ucs', version 1.4, converted to TIS encoding, with
28 ;; removal of three incorrect entries) to which the following license
31 ;; COPYRIGHT AND PERMISSION NOTICE
34 ;; Copyright (c) 1995-2001 International Business Machines
35 ;; Corporation and others
37 ;; All rights reserved.
40 ;; Permission is hereby granted, free of charge, to any person
41 ;; obtaining a copy of this software and associated documentation
42 ;; files (the "Software"), to deal in the Software without
43 ;; restriction, including without limitation the rights to use,
44 ;; copy, modify, merge, publish, distribute, and/or sell copies of
45 ;; the Software, and to permit persons to whom the Software is
46 ;; furnished to do so, provided that the above copyright notice(s)
47 ;; and this permission notice appear in all copies of the Software
48 ;; and that both the above copyright notice(s) and this permission
49 ;; notice appear in supporting documentation.
51 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
52 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
53 ;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
54 ;; NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
55 ;; COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE
56 ;; FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
57 ;; OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
58 ;; PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
59 ;; TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
60 ;; PERFORMANCE OF THIS SOFTWARE.
62 ;; Except as contained in this notice, the name of a copyright
63 ;; holder shall not be used in advertising or otherwise to promote
64 ;; the sale, use or other dealings in this Software without prior
65 ;; written authorization of the copyright holder.
70 ;; This file implements an algorithm to find Thai word breaks using a
71 ;; dictionary. It is based on the C program `cttex' written by
72 ;; Vuthichai Ampornaramveth <vuthi@nii.ac.jp>.
75 ;; Table of Thai words. It is a nested alist (see `nested-alist-p'),
76 ;; which means that you can easily index the list character by
79 (defvar thai-word-table
80 (let ((table (list 'thai-words)))
82 ;;; The following is indented as this to minimize this file size.
6227 "ÊÁºÙóÒÒÊÔ·¸ÔÃÒªÂì"
10737 (set-nested-alist elt 1 table))
10739 "Nested alist of Thai words.")
10742 (defun thai-update-word-table (file &optional append)
10743 "Update Thai word table by replacing the current word list with
10744 FILE. If called with a prefix argument, FILE is appended instead to
10745 the current word list."
10746 (interactive "FThai word table file: \nP")
10747 (let ((buf (generate-new-buffer "*thai-work*"))
10748 (coding-system-for-read 'thai-tis620)
10749 (table (if append thai-word-table (list 'thai-words))))
10753 (insert-file-contents file)
10754 (goto-char (point-min))
10755 (while (re-search-forward "\\ct+" nil t)
10756 (set-nested-alist (match-string 0) 1 table)))
10758 (setq thai-word-table table)))
10761 ;; Two special Thai characters regarded as suffix of words.
10763 (defconst thai-MaiYaMok (make-char 'thai-tis620 ?\xE6))
10764 (defconst thai-PaiYanNoi (make-char 'thai-tis620 ?\xCF))
10767 ;; Find Thai words starting at POS and return a list of positions of
10768 ;; the Thai word ends. It doesn't move point. LIMIT limits the
10769 ;; maximum position. IGNORE is a list of positions to ignore. It is
10770 ;; assumed that all following characters to LIMIT are Thai. If the
10771 ;; following char is not Thai (i.e., POS is equal to LIMIT), return t.
10773 ;; Note that the longest word position comes first.
10775 (defun thai-find-word-ends (pos limit &optional ignore)
10778 (let* ((char (char-after pos))
10779 (this (cdr (assq char (cdr thai-word-table))))
10782 ;; Look up the following character sequence in `thai-word-table'
10783 ;; character by character.
10786 char (or (char-after pos) 0)
10787 category-set (char-category-set char))
10788 ;; If the current sequence is recorded in `thai-word-table'
10789 ;; (i.e. (car THIS) is 1) and the following Thai character is
10790 ;; not an upper-vowel, lower-vowel, or tone-mark, we have
10791 ;; found a possible word ending position.
10792 (if (and (eq (car this) 1)
10793 (not (or (aref category-set ?2)
10794 (aref category-set ?3)
10795 (aref category-set ?4))))
10797 ;; Skip possible Thai suffices.
10798 (while (or (eq char thai-MaiYaMok) (eq char thai-PaiYanNoi))
10800 char (char-after pos)))
10801 ;; Skip character positions in IGNORE list.
10802 (or (memq pos ignore)
10803 (setq positions (cons pos positions)))))
10804 ;; Set up next loop.
10805 (setq this (and (< pos limit) (cdr (assq char this)))))
10809 ;; Move point forward to the end of Thai word which follows point and
10810 ;; update VEC. VEC is a vector of three elements used to cache word
10811 ;; end positions. The Nth element, if non-nil, is a list of end
10812 ;; points of the Nth word, or t indicating that there is no Thai
10813 ;; character. LIMIT limits the point movement.
10815 (defun thai-forward-word-update-info (vec limit)
10816 (let ((pos (point))
10822 ;; If four succeeding Thai words are found, throw t, otherwise
10825 ;; Start with first vector element.
10826 (setq v0 (aref vec 0))
10828 ;; Update VEC if V0 is empty.
10829 (setq v0 (thai-find-word-ends pos limit))
10832 ;; In case we haven't found any wordbreaks resp. point has
10833 ;; reached LIMIT, exit the catch body.
10834 (if (symbolp v0) ; i.e. nil or t?
10836 ;; OK, V0 holds possible word ends for the current position.
10837 ;; We save V0 for later reference.
10839 (setq v1 (aref vec 1))
10840 ;; Now we try all end word positions to find the next word.
10842 (setq pos (car v0))
10844 ;; Update VEC if V1 is empty, ignoring positions already
10845 ;; found -- for Thai, we need the longest match, so if
10848 ;; start(long-word) = start(short-word1)
10849 ;; end(short-word1) = start(short-word2)
10850 ;; end(short-word2) = end(long-word)
10852 ;; only long-word is used.
10853 (setq v1 (thai-find-word-ends pos limit tried))
10856 ;; If point has reached LIMIT, exit the catch body.
10859 ;; Save SECOND-BEST, if this hasn't been done already.
10860 ;; The `second best' solution is the end position of the
10861 ;; longest first word followed by the longest second word.
10864 (setq second-best (cons v0 v1)))
10865 ;; Update the already tried end word positions.
10866 (setq tried (append tried v1))
10867 ;; Now repeat the whole process to find a third word.
10869 (setq v2 (aref vec 2))
10871 (setq pos (car v1))
10873 (setq v2 (thai-find-word-ends pos limit tried))
10877 (setq tried (append tried v2))
10878 ;; And the same for a fourth word.
10880 (setq pos (car v2))
10881 (setq v3 (thai-find-word-ends pos limit tried))
10884 (setq v2 (cdr v2)))
10885 (setq v1 (cdr v1))))
10886 (setq v0 (cdr v0)))
10890 ;; We found four succeeding Thai words (or LIMIT has been
10891 ;; reached). Move to the end of the first word.
10892 (goto-char (car v0))
10893 ;; Update VEC for the next function call. If no larger word
10894 ;; positions have been found, set the corresponding vector
10896 (if (and (consp v1) (< (car v1) (car (aref vec 1))))
10899 (if (and (consp v2) (< (car v2) (car (aref vec 2))))
10902 (aset vec 2 v3)))) ; exit function successfully
10904 ;; We didn't find four consecutive words. If we have found a
10905 ;; `second best' solution and the length of those two words is
10906 ;; longer than the longest word we can see at the current point,
10907 ;; adopt the second best solution. This decision is based on
10908 ;; heuristic tests.
10909 (if (and second-best
10910 (< (car (aref vec 0)) (car (cdr second-best))))
10912 (goto-char (car (car second-best)))
10913 (aset vec 0 (cdr second-best)))
10914 ;; We finally failed to find a word break. For Thai, the best
10915 ;; solution is to extend the first longest word so that the
10916 ;; end point starts a second word.
10917 (setq pos (or (car (aref vec 0)) pos))
10918 (while (and (< pos limit)
10919 (not (setq positions (thai-find-word-ends pos limit))))
10920 (setq pos (1+ pos)))
10922 (aset vec 0 positions))
10927 ;; Return a list of Thai word boundary positions after the current
10928 ;; point. LIMIT, if non-nil, limits the region to check.
10930 (defun thai-find-word-boundaries (&optional limit)
10932 (setq limit (point-max)))
10934 (let ((vec (make-vector 3 nil))
10937 ;; Loop over all (consecutive) Thai regions by using the
10938 ;; character property `t' until LIMIT is reached.
10939 (while (and (< (point) limit)
10940 (re-search-forward "\\ct+" nil t))
10941 (setq this-limit (point))
10942 (goto-char (match-beginning 0))
10943 (fillarray vec nil)
10944 ;; Check the first word, initializing VEC.
10945 (thai-forward-word-update-info vec this-limit)
10946 ;; Then loop over the remaining words in the current Thai
10947 ;; region, collecting the boundaries.
10948 (while (< (point) this-limit)
10949 (setq boundaries (cons (point) boundaries))
10950 (thai-forward-word-update-info vec this-limit)))
10954 (defun thai-break-words (separator &optional limit)
10955 "Break Thai words by inserting a separator string at word boundaries."
10956 (interactive "sSeparator: ")
10958 (let ((boundaries (thai-find-word-boundaries limit)))
10960 (goto-char (car boundaries))
10962 (setq boundaries (cdr boundaries))))))
10965 (defun thai-forward-word (count)
10966 "Move point forward COUNT words considering Thai word boundaries.
10967 If COUNT is negative, move point backward (- COUNT) words."
10971 (skip-syntax-forward "^w")
10972 (if (looking-at "\\ct+")
10973 ;; We have reached a Thai region, so we must do something
10974 ;; special instead of using forward-word.
10975 (let ((start (point))
10976 (limit (match-end 0))
10979 ;; If thai-forward-word has been called within a Thai
10980 ;; region, we must go back until the Thai region starts
10981 ;; to do the contextual analysis for finding word
10983 (while (aref (char-category-set (preceding-char)) ?t)
10985 ;; OK, we ask for the list of word boundaries in
10987 (setq boundaries (nreverse (thai-find-word-boundaries limit)))
10988 ;; Now we search for the next boundary after START.
10989 (while (and boundaries (<= (car boundaries) start))
10990 (setq boundaries (cdr boundaries)))
10991 ;; Adjust loop for next while loop.
10992 (setq count (1- count))
10993 ;; Now we skip Thai words until the BOUNDARIES list is
10994 ;; empty or count-1 words have been passed.
10995 (if (not boundaries)
10997 (while (and (> count 0)
10999 (setq boundaries (cdr boundaries)
11001 ;; If BOUNDARIES is empty, the word counter is not
11002 ;; zero yet (remember that we have decreased COUNT by
11003 ;; one), so we go to LIMIT. Otherwise go to next
11006 (goto-char (car boundaries))
11007 (goto-char limit))))
11010 (setq count (1- count))))
11011 ;; The symmetrical action for negative values.
11014 (skip-syntax-backward "^w")
11015 (if (aref (char-category-set (preceding-char)) ?t)
11016 (let ((start (point))
11017 (limit (if (looking-at "\\ct+") (match-end 0)
11021 (while (aref (char-category-set (preceding-char)) ?t)
11023 (setq boundaries (thai-find-word-boundaries limit))
11024 (while (and boundaries (>= (car boundaries) start))
11025 (setq boundaries (cdr boundaries)))
11026 (setq count (1+ count))
11029 (while (and (< count 0) boundaries)
11030 (setq boundaries (cdr boundaries)
11033 (goto-char (car boundaries))))))
11035 (setq count (1+ count)))))))
11038 (defun thai-backward-word (count)
11039 "Move point backward COUNT words considering Thai word boundaries.
11040 If COUNT is negative, move point forward (- COUNT) words."
11042 (thai-forward-word (- count)))
11045 (defun thai-kill-word (arg)
11046 "Like kill-word but pay attention to Thai word boundaries.
11047 With argument, do this that many times."
11049 (kill-region (point) (progn (thai-forward-word arg) (point))))
11052 (defun thai-backward-kill-word (arg)
11053 "Like backward-kill-word but pay attention to Thai word boundaries."
11055 (thai-kill-word (- arg)))
11058 (defun thai-transpose-words (arg)
11059 "Like transpose-words but pay attention to Thai word boundaries."
11061 (transpose-subr 'thai-forward-word arg))
11063 (defun thai-fill-find-break-point (linebeg)
11064 "Go to a line breaking position near point considering Thai word boundaries."
11065 (let ((pos (point)))
11066 (thai-forward-word -1)
11067 (when (<= (point) linebeg)
11069 (thai-forward-word 1))
11070 (kinsoku linebeg)))
11072 (provide 'thai-word)
11075 ;; Local Variables:
11079 ;; end of thai-word.el
11081 ;; arch-tag: 29927f02-e177-4224-a270-7e67210b038a