extensions/spellcheck/hunspell/src/phonet.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /******* BEGIN LICENSE BLOCK *******
michael@0 2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
michael@0 3 *
michael@0 4 * The contents of this file are subject to the Mozilla Public License Version
michael@0 5 * 1.1 (the "License"); you may not use this file except in compliance with
michael@0 6 * the License. You may obtain a copy of the License at
michael@0 7 * http://www.mozilla.org/MPL/
michael@0 8 *
michael@0 9 * Software distributed under the License is distributed on an "AS IS" basis,
michael@0 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
michael@0 11 * for the specific language governing rights and limitations under the
michael@0 12 * License.
michael@0 13 *
michael@0 14 * The Initial Developer of the Original Code is Björn Jacke. Portions created
michael@0 15 * by the Initial Developers are Copyright (C) 2000-2007 the Initial
michael@0 16 * Developers. All Rights Reserved.
michael@0 17 *
michael@0 18 * Contributor(s): Björn Jacke (bjoern.jacke@gmx.de)
michael@0 19 * László Németh (nemethl@gyorsposta.hu)
michael@0 20 * Caolan McNamara (caolanm@redhat.com)
michael@0 21 *
michael@0 22 * Alternatively, the contents of this file may be used under the terms of
michael@0 23 * either the GNU General Public License Version 2 or later (the "GPL"), or
michael@0 24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
michael@0 25 * in which case the provisions of the GPL or the LGPL are applicable instead
michael@0 26 * of those above. If you wish to allow use of your version of this file only
michael@0 27 * under the terms of either the GPL or the LGPL, and not to allow others to
michael@0 28 * use your version of this file under the terms of the MPL, indicate your
michael@0 29 * decision by deleting the provisions above and replace them with the notice
michael@0 30 * and other provisions required by the GPL or the LGPL. If you do not delete
michael@0 31 * the provisions above, a recipient may use your version of this file under
michael@0 32 * the terms of any one of the MPL, the GPL or the LGPL.
michael@0 33 *
michael@0 34 * Changelog:
michael@0 35 * 2000-01-05 Björn Jacke <bjoern.jacke AT gmx.de>
michael@0 36 * Initial Release insprired by the article about phonetic
michael@0 37 * transformations out of c't 25/1999
michael@0 38 *
michael@0 39 * 2007-07-26 Björn Jacke <bjoern.jacke AT gmx.de>
michael@0 40 * Released under MPL/GPL/LGPL tri-license for Hunspell
michael@0 41 *
michael@0 42 * 2007-08-23 László Németh <nemeth at OOo>
michael@0 43 * Porting from Aspell to Hunspell using C-like structs
michael@0 44 *
michael@0 45 ******* END LICENSE BLOCK *******/
michael@0 46
michael@0 47 #include <stdlib.h>
michael@0 48 #include <string.h>
michael@0 49 #include <stdio.h>
michael@0 50 #include <ctype.h>
michael@0 51
michael@0 52 #include "csutil.hxx"
michael@0 53 #include "phonet.hxx"
michael@0 54
michael@0 55 void init_phonet_hash(phonetable & parms)
michael@0 56 {
michael@0 57 int i, k;
michael@0 58
michael@0 59 for (i = 0; i < HASHSIZE; i++) {
michael@0 60 parms.hash[i] = -1;
michael@0 61 }
michael@0 62
michael@0 63 for (i = 0; parms.rules[i][0] != '\0'; i += 2) {
michael@0 64 /** set hash value **/
michael@0 65 k = (unsigned char) parms.rules[i][0];
michael@0 66
michael@0 67 if (parms.hash[k] < 0) {
michael@0 68 parms.hash[k] = i;
michael@0 69 }
michael@0 70 }
michael@0 71 }
michael@0 72
michael@0 73 // like strcpy but safe if the strings overlap
michael@0 74 // but only if dest < src
michael@0 75 static inline void strmove(char * dest, char * src) {
michael@0 76 while (*src)
michael@0 77 *dest++ = *src++;
michael@0 78 *dest = '\0';
michael@0 79 }
michael@0 80
michael@0 81 static int myisalpha(char ch) {
michael@0 82 if ((unsigned char) ch < 128) return isalpha(ch);
michael@0 83 return 1;
michael@0 84 }
michael@0 85
michael@0 86 /* phonetic transcription algorithm */
michael@0 87 /* see: http://aspell.net/man-html/Phonetic-Code.html */
michael@0 88 /* convert string to uppercase before this call */
michael@0 89 int phonet (const char * inword, char * target,
michael@0 90 int len,
michael@0 91 phonetable & parms)
michael@0 92 {
michael@0 93 /** Do phonetic transformation. **/
michael@0 94 /** "len" = length of "inword" incl. '\0'. **/
michael@0 95
michael@0 96 /** result: >= 0: length of "target" **/
michael@0 97 /** otherwise: error **/
michael@0 98
michael@0 99 int i,j,k=0,n,p,z;
michael@0 100 int k0,n0,p0=-333,z0;
michael@0 101 char c, c0;
michael@0 102 const char * s;
michael@0 103 typedef unsigned char uchar;
michael@0 104 char word[MAXPHONETUTF8LEN + 1];
michael@0 105 if (len == -1) len = strlen(inword);
michael@0 106 if (len > MAXPHONETUTF8LEN) return 0;
michael@0 107 strcpy(word, inword);
michael@0 108
michael@0 109 /** check word **/
michael@0 110 i = j = z = 0;
michael@0 111 while ((c = word[i]) != '\0') {
michael@0 112 n = parms.hash[(uchar) c];
michael@0 113 z0 = 0;
michael@0 114
michael@0 115 if (n >= 0) {
michael@0 116 /** check all rules for the same letter **/
michael@0 117 while (parms.rules[n][0] == c) {
michael@0 118
michael@0 119 /** check whole string **/
michael@0 120 k = 1; /** number of found letters **/
michael@0 121 p = 5; /** default priority **/
michael@0 122 s = parms.rules[n];
michael@0 123 s++; /** important for (see below) "*(s-1)" **/
michael@0 124
michael@0 125 while (*s != '\0' && word[i+k] == *s
michael@0 126 && !isdigit ((unsigned char) *s) && strchr ("(-<^$", *s) == NULL) {
michael@0 127 k++;
michael@0 128 s++;
michael@0 129 }
michael@0 130 if (*s == '(') {
michael@0 131 /** check letters in "(..)" **/
michael@0 132 if (myisalpha(word[i+k]) // ...could be implied?
michael@0 133 && strchr(s+1, word[i+k]) != NULL) {
michael@0 134 k++;
michael@0 135 while (*s != ')')
michael@0 136 s++;
michael@0 137 s++;
michael@0 138 }
michael@0 139 }
michael@0 140 p0 = (int) *s;
michael@0 141 k0 = k;
michael@0 142 while (*s == '-' && k > 1) {
michael@0 143 k--;
michael@0 144 s++;
michael@0 145 }
michael@0 146 if (*s == '<')
michael@0 147 s++;
michael@0 148 if (isdigit ((unsigned char) *s)) {
michael@0 149 /** determine priority **/
michael@0 150 p = *s - '0';
michael@0 151 s++;
michael@0 152 }
michael@0 153 if (*s == '^' && *(s+1) == '^')
michael@0 154 s++;
michael@0 155
michael@0 156 if (*s == '\0'
michael@0 157 || (*s == '^'
michael@0 158 && (i == 0 || ! myisalpha(word[i-1]))
michael@0 159 && (*(s+1) != '$'
michael@0 160 || (! myisalpha(word[i+k0]) )))
michael@0 161 || (*s == '$' && i > 0
michael@0 162 && myisalpha(word[i-1])
michael@0 163 && (! myisalpha(word[i+k0]) )))
michael@0 164 {
michael@0 165 /** search for followup rules, if: **/
michael@0 166 /** parms.followup and k > 1 and NO '-' in searchstring **/
michael@0 167 c0 = word[i+k-1];
michael@0 168 n0 = parms.hash[(uchar) c0];
michael@0 169
michael@0 170 // if (parms.followup && k > 1 && n0 >= 0
michael@0 171 if (k > 1 && n0 >= 0
michael@0 172 && p0 != (int) '-' && word[i+k] != '\0') {
michael@0 173 /** test follow-up rule for "word[i+k]" **/
michael@0 174 while (parms.rules[n0][0] == c0) {
michael@0 175
michael@0 176 /** check whole string **/
michael@0 177 k0 = k;
michael@0 178 p0 = 5;
michael@0 179 s = parms.rules[n0];
michael@0 180 s++;
michael@0 181 while (*s != '\0' && word[i+k0] == *s
michael@0 182 && ! isdigit((unsigned char) *s) && strchr("(-<^$",*s) == NULL) {
michael@0 183 k0++;
michael@0 184 s++;
michael@0 185 }
michael@0 186 if (*s == '(') {
michael@0 187 /** check letters **/
michael@0 188 if (myisalpha(word[i+k0])
michael@0 189 && strchr (s+1, word[i+k0]) != NULL) {
michael@0 190 k0++;
michael@0 191 while (*s != ')' && *s != '\0')
michael@0 192 s++;
michael@0 193 if (*s == ')')
michael@0 194 s++;
michael@0 195 }
michael@0 196 }
michael@0 197 while (*s == '-') {
michael@0 198 /** "k0" gets NOT reduced **/
michael@0 199 /** because "if (k0 == k)" **/
michael@0 200 s++;
michael@0 201 }
michael@0 202 if (*s == '<')
michael@0 203 s++;
michael@0 204 if (isdigit ((unsigned char) *s)) {
michael@0 205 p0 = *s - '0';
michael@0 206 s++;
michael@0 207 }
michael@0 208
michael@0 209 if (*s == '\0'
michael@0 210 /** *s == '^' cuts **/
michael@0 211 || (*s == '$' && ! myisalpha(word[i+k0])))
michael@0 212 {
michael@0 213 if (k0 == k) {
michael@0 214 /** this is just a piece of the string **/
michael@0 215 n0 += 2;
michael@0 216 continue;
michael@0 217 }
michael@0 218
michael@0 219 if (p0 < p) {
michael@0 220 /** priority too low **/
michael@0 221 n0 += 2;
michael@0 222 continue;
michael@0 223 }
michael@0 224 /** rule fits; stop search **/
michael@0 225 break;
michael@0 226 }
michael@0 227 n0 += 2;
michael@0 228 } /** End of "while (parms.rules[n0][0] == c0)" **/
michael@0 229
michael@0 230 if (p0 >= p && parms.rules[n0][0] == c0) {
michael@0 231 n += 2;
michael@0 232 continue;
michael@0 233 }
michael@0 234 } /** end of follow-up stuff **/
michael@0 235
michael@0 236 /** replace string **/
michael@0 237 s = parms.rules[n+1];
michael@0 238 p0 = (parms.rules[n][0] != '\0'
michael@0 239 && strchr (parms.rules[n]+1,'<') != NULL) ? 1:0;
michael@0 240 if (p0 == 1 && z == 0) {
michael@0 241 /** rule with '<' is used **/
michael@0 242 if (j > 0 && *s != '\0'
michael@0 243 && (target[j-1] == c || target[j-1] == *s)) {
michael@0 244 j--;
michael@0 245 }
michael@0 246 z0 = 1;
michael@0 247 z = 1;
michael@0 248 k0 = 0;
michael@0 249 while (*s != '\0' && word[i+k0] != '\0') {
michael@0 250 word[i+k0] = *s;
michael@0 251 k0++;
michael@0 252 s++;
michael@0 253 }
michael@0 254 if (k > k0)
michael@0 255 strmove (&word[0]+i+k0, &word[0]+i+k);
michael@0 256
michael@0 257 /** new "actual letter" **/
michael@0 258 c = word[i];
michael@0 259 }
michael@0 260 else { /** no '<' rule used **/
michael@0 261 i += k - 1;
michael@0 262 z = 0;
michael@0 263 while (*s != '\0'
michael@0 264 && *(s+1) != '\0' && j < len) {
michael@0 265 if (j == 0 || target[j-1] != *s) {
michael@0 266 target[j] = *s;
michael@0 267 j++;
michael@0 268 }
michael@0 269 s++;
michael@0 270 }
michael@0 271 /** new "actual letter" **/
michael@0 272 c = *s;
michael@0 273 if (parms.rules[n][0] != '\0'
michael@0 274 && strstr (parms.rules[n]+1, "^^") != NULL) {
michael@0 275 if (c != '\0') {
michael@0 276 target[j] = c;
michael@0 277 j++;
michael@0 278 }
michael@0 279 strmove (&word[0], &word[0]+i+1);
michael@0 280 i = 0;
michael@0 281 z0 = 1;
michael@0 282 }
michael@0 283 }
michael@0 284 break;
michael@0 285 } /** end of follow-up stuff **/
michael@0 286 n += 2;
michael@0 287 } /** end of while (parms.rules[n][0] == c) **/
michael@0 288 } /** end of if (n >= 0) **/
michael@0 289 if (z0 == 0) {
michael@0 290 // if (k && (assert(p0!=-333),!p0) && j < len && c != '\0'
michael@0 291 // && (!parms.collapse_result || j == 0 || target[j-1] != c)){
michael@0 292 if (k && !p0 && j < len && c != '\0'
michael@0 293 && (1 || j == 0 || target[j-1] != c)){
michael@0 294 /** condense only double letters **/
michael@0 295 target[j] = c;
michael@0 296 ///printf("\n setting \n");
michael@0 297 j++;
michael@0 298 }
michael@0 299
michael@0 300 i++;
michael@0 301 z = 0;
michael@0 302 k=0;
michael@0 303 }
michael@0 304 } /** end of while ((c = word[i]) != '\0') **/
michael@0 305
michael@0 306 target[j] = '\0';
michael@0 307 return (j);
michael@0 308
michael@0 309 } /** end of function "phonet" **/

mercurial