1 /* phonetic.c - generic replacement aglogithms for phonetic transformation
2 Copyright (C) 2000 Bjoern Jacke
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License version 2.1 as published by the Free Software Foundation;
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; If not, see
15 <http://www.gnu.org/licenses/>.
19 2000-01-05 Bjoern Jacke <bjoern at j3e.de>
20 Initial Release insprired by the article about phonetic
21 transformations out of c't 25/1999
23 2007-07-26 Bjoern Jacke <bjoern at j3e.de>
24 Released under MPL/GPL/LGPL tri-license for Hunspell
26 2007-08-23 Laszlo Nemeth <nemeth at OOo>
27 Porting from Aspell to Hunspell using C-like structs
38 void init_phonet_hash(phonetable & parms)
42 for (i = 0; i < HASHSIZE; i++) {
46 for (i = 0; parms.rules[i][0] != '\0'; i += 2) {
47 /** set hash value **/
48 k = (unsigned char) parms.rules[i][0];
50 if (parms.hash[k] < 0) {
56 // like strcpy but safe if the strings overlap
57 // but only if dest < src
58 static inline void strmove(char * dest, char * src) {
64 static int myisalpha(char ch) {
65 if ((unsigned char) ch < 128) return isalpha(ch);
69 /* phonetic transcription algorithm */
70 /* see: http://aspell.net/man-html/Phonetic-Code.html */
71 /* convert string to uppercase before this call */
72 int phonet (const char * inword, char * target,
76 /** Do phonetic transformation. **/
77 /** "len" = length of "inword" incl. '\0'. **/
79 /** result: >= 0: length of "target" **/
80 /** otherwise: error **/
86 typedef unsigned char uchar;
87 char word[MAXPHONETUTF8LEN + 1];
88 if (len == -1) len = strlen(inword);
89 if (len > MAXPHONETUTF8LEN) return 0;
94 while ((c = word[i]) != '\0') {
95 n = parms.hash[(uchar) c];
99 /** check all rules for the same letter **/
100 while (parms.rules[n][0] == c) {
102 /** check whole string **/
103 k = 1; /** number of found letters **/
104 p = 5; /** default priority **/
106 s++; /** important for (see below) "*(s-1)" **/
108 while (*s != '\0' && word[i+k] == *s
109 && !isdigit ((unsigned char) *s) && strchr ("(-<^$", *s) == NULL) {
114 /** check letters in "(..)" **/
115 if (myisalpha(word[i+k]) // ...could be implied?
116 && strchr(s+1, word[i+k]) != NULL) {
125 while (*s == '-' && k > 1) {
131 if (isdigit ((unsigned char) *s)) {
132 /** determine priority **/
136 if (*s == '^' && *(s+1) == '^')
141 && (i == 0 || ! myisalpha(word[i-1]))
143 || (! myisalpha(word[i+k0]) )))
144 || (*s == '$' && i > 0
145 && myisalpha(word[i-1])
146 && (! myisalpha(word[i+k0]) )))
148 /** search for followup rules, if: **/
149 /** parms.followup and k > 1 and NO '-' in searchstring **/
151 n0 = parms.hash[(uchar) c0];
153 // if (parms.followup && k > 1 && n0 >= 0
155 && p0 != (int) '-' && word[i+k] != '\0') {
156 /** test follow-up rule for "word[i+k]" **/
157 while (parms.rules[n0][0] == c0) {
159 /** check whole string **/
164 while (*s != '\0' && word[i+k0] == *s
165 && ! isdigit((unsigned char) *s) && strchr("(-<^$",*s) == NULL) {
170 /** check letters **/
171 if (myisalpha(word[i+k0])
172 && strchr (s+1, word[i+k0]) != NULL) {
174 while (*s != ')' && *s != '\0')
181 /** "k0" gets NOT reduced **/
182 /** because "if (k0 == k)" **/
187 if (isdigit ((unsigned char) *s)) {
193 /** *s == '^' cuts **/
194 || (*s == '$' && ! myisalpha(word[i+k0])))
197 /** this is just a piece of the string **/
203 /** priority too low **/
207 /** rule fits; stop search **/
211 } /** End of "while (parms.rules[n0][0] == c0)" **/
213 if (p0 >= p && parms.rules[n0][0] == c0) {
217 } /** end of follow-up stuff **/
219 /** replace string **/
220 s = parms.rules[n+1];
221 p0 = (parms.rules[n][0] != '\0'
222 && strchr (parms.rules[n]+1,'<') != NULL) ? 1:0;
223 if (p0 == 1 && z == 0) {
224 /** rule with '<' is used **/
225 if (j > 0 && *s != '\0'
226 && (target[j-1] == c || target[j-1] == *s)) {
232 while (*s != '\0' && word[i+k0] != '\0') {
238 strmove (&word[0]+i+k0, &word[0]+i+k);
240 /** new "actual letter" **/
243 else { /** no '<' rule used **/
247 && *(s+1) != '\0' && j < len) {
248 if (j == 0 || target[j-1] != *s) {
254 /** new "actual letter" **/
256 if (parms.rules[n][0] != '\0'
257 && strstr (parms.rules[n]+1, "^^") != NULL) {
262 strmove (&word[0], &word[0]+i+1);
268 } /** end of follow-up stuff **/
270 } /** end of while (parms.rules[n][0] == c) **/
271 } /** end of if (n >= 0) **/
273 // if (k && (assert(p0!=-333),!p0) && j < len && c != '\0'
274 // && (!parms.collapse_result || j == 0 || target[j-1] != c)){
275 if (k && !p0 && j < len && c != '\0'
276 && (1 || j == 0 || target[j-1] != c)){
277 /** condense only double letters **/
279 ///printf("\n setting \n");
287 } /** end of while ((c = word[i]) != '\0') **/
292 } /** end of function "phonet" **/