1 /* phonetic.c - generic replacement aglogithms for phonetic transformation
2 Copyright (C) 2000 Bjoern Jacke
3
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;
7
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.
12
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/>.
16
17 Changelog:
18
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
22
23 2007-07-26 Bjoern Jacke <bjoern at j3e.de>
24 Released under MPL/GPL/LGPL tri-license for Hunspell
25
26 2007-08-23 Laszlo Nemeth <nemeth at OOo>
27 Porting from Aspell to Hunspell using C-like structs
28 */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <ctype.h>
34
35 #include "csutil.hxx"
36 #include "phonet.hxx"
37
init_phonet_hash(phonetable & parms)38 void init_phonet_hash(phonetable& parms) {
39 int i, k;
40
41 for (i = 0; i < HASHSIZE; i++) {
42 parms.hash[i] = -1;
43 }
44
45 for (i = 0; parms.rules[i][0] != '\0'; i += 2) {
46 /** set hash value **/
47 k = (unsigned char)parms.rules[i][0];
48
49 if (parms.hash[k] < 0) {
50 parms.hash[k] = i;
51 }
52 }
53 }
54
55 // like strcpy but safe if the strings overlap
56 // but only if dest < src
strmove(char * dest,char * src)57 static inline void strmove(char* dest, char* src) {
58 while (*src)
59 *dest++ = *src++;
60 *dest = '\0';
61 }
62
myisalpha(char ch)63 static int myisalpha(char ch) {
64 if ((unsigned char)ch < 128)
65 return isalpha(ch);
66 return 1;
67 }
68
69 /* Do phonetic transformation. */
70 /* phonetic transcription algorithm */
71 /* see: http://aspell.net/man-html/Phonetic-Code.html */
72 /* convert string to uppercase before this call */
phonet(const std::string & inword,phonetable & parms)73 std::string phonet(const std::string& inword, phonetable& parms) {
74
75 int i, k = 0, p, z;
76 int k0, n0, p0 = -333, z0;
77 char c;
78 const char* s;
79 typedef unsigned char uchar;
80
81 size_t len = inword.size();
82 if (len > MAXPHONETUTF8LEN)
83 return std::string();
84 char word[MAXPHONETUTF8LEN + 1];
85 strncpy(word, inword.c_str(), MAXPHONETUTF8LEN);
86 word[MAXPHONETUTF8LEN] = '\0';
87
88 std::string target;
89 /** check word **/
90 i = z = 0;
91 while ((c = word[i]) != '\0') {
92 int n = parms.hash[(uchar)c];
93 z0 = 0;
94
95 if (n >= 0) {
96 /** check all rules for the same letter **/
97 while (parms.rules[n][0] == c) {
98 /** check whole string **/
99 k = 1; /** number of found letters **/
100 p = 5; /** default priority **/
101 s = parms.rules[n];
102 s++; /** important for (see below) "*(s-1)" **/
103
104 while (*s != '\0' && word[i + k] == *s && !isdigit((unsigned char)*s) &&
105 strchr("(-<^$", *s) == NULL) {
106 k++;
107 s++;
108 }
109 if (*s == '(') {
110 /** check letters in "(..)" **/
111 if (myisalpha(word[i + k]) // ...could be implied?
112 && strchr(s + 1, word[i + k]) != NULL) {
113 k++;
114 while (*s != ')')
115 s++;
116 s++;
117 }
118 }
119 p0 = (int)*s;
120 k0 = k;
121 while (*s == '-' && k > 1) {
122 k--;
123 s++;
124 }
125 if (*s == '<')
126 s++;
127 if (isdigit((unsigned char)*s)) {
128 /** determine priority **/
129 p = *s - '0';
130 s++;
131 }
132 if (*s == '^' && *(s + 1) == '^')
133 s++;
134
135 if (*s == '\0' || (*s == '^' && (i == 0 || !myisalpha(word[i - 1])) &&
136 (*(s + 1) != '$' || (!myisalpha(word[i + k0])))) ||
137 (*s == '$' && i > 0 && myisalpha(word[i - 1]) &&
138 (!myisalpha(word[i + k0])))) {
139 /** search for followup rules, if: **/
140 /** parms.followup and k > 1 and NO '-' in searchstring **/
141 char c0 = word[i + k - 1];
142 n0 = parms.hash[(uchar)c0];
143
144 // if (parms.followup && k > 1 && n0 >= 0
145 if (k > 1 && n0 >= 0 && p0 != (int)'-' && word[i + k] != '\0') {
146 /** test follow-up rule for "word[i+k]" **/
147 while (parms.rules[n0][0] == c0) {
148 /** check whole string **/
149 k0 = k;
150 p0 = 5;
151 s = parms.rules[n0];
152 s++;
153 while (*s != '\0' && word[i + k0] == *s &&
154 !isdigit((unsigned char)*s) &&
155 strchr("(-<^$", *s) == NULL) {
156 k0++;
157 s++;
158 }
159 if (*s == '(') {
160 /** check letters **/
161 if (myisalpha(word[i + k0]) &&
162 strchr(s + 1, word[i + k0]) != NULL) {
163 k0++;
164 while (*s != ')' && *s != '\0')
165 s++;
166 if (*s == ')')
167 s++;
168 }
169 }
170 while (*s == '-') {
171 /** "k0" gets NOT reduced **/
172 /** because "if (k0 == k)" **/
173 s++;
174 }
175 if (*s == '<')
176 s++;
177 if (isdigit((unsigned char)*s)) {
178 p0 = *s - '0';
179 s++;
180 }
181
182 if (*s == '\0'
183 /** *s == '^' cuts **/
184 || (*s == '$' && !myisalpha(word[i + k0]))) {
185 if (k0 == k) {
186 /** this is just a piece of the string **/
187 n0 += 2;
188 continue;
189 }
190
191 if (p0 < p) {
192 /** priority too low **/
193 n0 += 2;
194 continue;
195 }
196 /** rule fits; stop search **/
197 break;
198 }
199 n0 += 2;
200 } /** End of "while (parms.rules[n0][0] == c0)" **/
201
202 if (p0 >= p && parms.rules[n0][0] == c0) {
203 n += 2;
204 continue;
205 }
206 } /** end of follow-up stuff **/
207
208 /** replace string **/
209 s = parms.rules[n + 1];
210 p0 = (parms.rules[n][0] != '\0' &&
211 strchr(parms.rules[n] + 1, '<') != NULL)
212 ? 1
213 : 0;
214 if (p0 == 1 && z == 0) {
215 /** rule with '<' is used **/
216 if (!target.empty() && *s != '\0' &&
217 (target[target.size()-1] == c || target[target.size()-1] == *s)) {
218 target.erase(target.size() - 1);
219 }
220 z0 = 1;
221 z = 1;
222 k0 = 0;
223 while (*s != '\0' && word[i + k0] != '\0') {
224 word[i + k0] = *s;
225 k0++;
226 s++;
227 }
228 if (k > k0)
229 strmove(&word[0] + i + k0, &word[0] + i + k);
230
231 /** new "actual letter" **/
232 c = word[i];
233 } else { /** no '<' rule used **/
234 i += k - 1;
235 z = 0;
236 while (*s != '\0' && *(s + 1) != '\0' && target.size() < len) {
237 if (target.empty() || target[target.size()-1] != *s) {
238 target.push_back(*s);
239 }
240 s++;
241 }
242 /** new "actual letter" **/
243 c = *s;
244 if (parms.rules[n][0] != '\0' &&
245 strstr(parms.rules[n] + 1, "^^") != NULL) {
246 if (c != '\0') {
247 target.push_back(c);
248 }
249 strmove(&word[0], &word[0] + i + 1);
250 i = 0;
251 z0 = 1;
252 }
253 }
254 break;
255 } /** end of follow-up stuff **/
256 n += 2;
257 } /** end of while (parms.rules[n][0] == c) **/
258 } /** end of if (n >= 0) **/
259 if (z0 == 0) {
260 if (k && !p0 && target.size() < len && c != '\0' &&
261 (1 || target.empty() || target[target.size()-1] != c)) {
262 /** condense only double letters **/
263 target.push_back(c);
264 /// printf("\n setting \n");
265 }
266
267 i++;
268 z = 0;
269 k = 0;
270 }
271 } /** end of while ((c = word[i]) != '\0') **/
272
273 return target;
274 } /** end of function "phonet" **/
275