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) {
39 for (int i = 0; i < HASHSIZE; i++) {
43 for (int i = 0; parms.rules[i][0] != '\0'; i += 2) {
44 /** set hash value **/
45 int k = (unsigned char)parms.rules[i][0];
47 if (parms.hash[k] < 0) {
53 // like strcpy but safe if the strings overlap
54 // but only if dest < src
55 static inline void strmove(char* dest, char* src) {
61 static int myisalpha(char ch) {
62 if ((unsigned char)ch < 128)
67 /* Do phonetic transformation. */
68 /* phonetic transcription algorithm */
69 /* see: http://aspell.net/man-html/Phonetic-Code.html */
70 /* convert string to uppercase before this call */
71 std::string phonet(const std::string& inword, phonetable& parms) {
74 int k0, n0, p0 = -333;
76 typedef unsigned char uchar;
78 size_t len = inword.size();
79 if (len > MAXPHONETUTF8LEN)
81 char word[MAXPHONETUTF8LEN + 1];
82 strncpy(word, inword.c_str(), MAXPHONETUTF8LEN);
83 word[MAXPHONETUTF8LEN] = '\0';
88 while ((c = word[i]) != '\0') {
89 int n = parms.hash[(uchar)c];
92 if (n >= 0 && !parms.rules[n].empty()) {
93 /** check all rules for the same letter **/
94 while (parms.rules[n][0] == c) {
95 /** check whole string **/
96 k = 1; /** number of found letters **/
97 p = 5; /** default priority **/
98 const char*s = parms.rules[n].c_str();
99 s++; /** important for (see below) "*(s-1)" **/
101 while (*s != '\0' && word[i + k] == *s && !isdigit((unsigned char)*s) &&
102 strchr("(-<^$", *s) == NULL) {
107 /** check letters in "(..)" **/
108 if (myisalpha(word[i + k]) // ...could be implied?
109 && strchr(s + 1, word[i + k]) != NULL) {
118 while (*s == '-' && k > 1) {
124 if (isdigit((unsigned char)*s)) {
125 /** determine priority **/
129 if (*s == '^' && *(s + 1) == '^')
132 if (*s == '\0' || (*s == '^' && (i == 0 || !myisalpha(word[i - 1])) &&
133 (*(s + 1) != '$' || (!myisalpha(word[i + k0])))) ||
134 (*s == '$' && i > 0 && myisalpha(word[i - 1]) &&
135 (!myisalpha(word[i + k0])))) {
136 /** search for followup rules, if: **/
137 /** parms.followup and k > 1 and NO '-' in searchstring **/
138 char c0 = word[i + k - 1];
139 n0 = parms.hash[(uchar)c0];
141 // if (parms.followup && k > 1 && n0 >= 0
142 if (k > 1 && n0 >= 0 && p0 != (int)'-' && word[i + k] != '\0' && !parms.rules[n0].empty()) {
143 /** test follow-up rule for "word[i+k]" **/
144 while (parms.rules[n0][0] == c0) {
145 /** check whole string **/
148 s = parms.rules[n0].c_str();
150 while (*s != '\0' && word[i + k0] == *s &&
151 !isdigit((unsigned char)*s) &&
152 strchr("(-<^$", *s) == NULL) {
157 /** check letters **/
158 if (myisalpha(word[i + k0]) &&
159 strchr(s + 1, word[i + k0]) != NULL) {
161 while (*s != ')' && *s != '\0')
168 /** "k0" gets NOT reduced **/
169 /** because "if (k0 == k)" **/
174 if (isdigit((unsigned char)*s)) {
180 /** *s == '^' cuts **/
181 || (*s == '$' && !myisalpha(word[i + k0]))) {
183 /** this is just a piece of the string **/
189 /** priority too low **/
193 /** rule fits; stop search **/
197 } /** End of "while (parms.rules[n0][0] == c0)" **/
199 if (p0 >= p && parms.rules[n0][0] == c0) {
203 } /** end of follow-up stuff **/
205 /** replace string **/
206 s = parms.rules[n + 1].c_str();
207 p0 = (!parms.rules[n].empty() &&
208 strchr(parms.rules[n].c_str() + 1, '<') != NULL)
211 if (p0 == 1 && z == 0) {
212 /** rule with '<' is used **/
213 if (!target.empty() && *s != '\0' &&
214 (target[target.size()-1] == c || target[target.size()-1] == *s)) {
215 target.erase(target.size() - 1);
220 while (*s != '\0' && word[i + k0] != '\0') {
226 strmove(&word[0] + i + k0, &word[0] + i + k);
228 /** new "actual letter" **/
230 } else { /** no '<' rule used **/
233 while (*s != '\0' && *(s + 1) != '\0' && target.size() < len) {
234 if (target.empty() || target[target.size()-1] != *s) {
235 target.push_back(*s);
239 /** new "actual letter" **/
241 if (!parms.rules[n].empty() &&
242 strstr(parms.rules[n].c_str() + 1, "^^") != NULL) {
246 strmove(&word[0], &word[0] + i + 1);
252 } /** end of follow-up stuff **/
254 } /** end of while (parms.rules[n][0] == c) **/
255 } /** end of if (n >= 0) **/
257 if (k && !p0 && target.size() < len && c != '\0') {
258 /** condense only double letters **/
260 /// printf("\n setting \n");
267 } /** end of while ((c = word[i]) != '\0') **/
270 } /** end of function "phonet" **/