Rev 8100 | Rev 8185 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program 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 |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
17 | |||
7676 | bpr | 18 | /* Versatile translation according to a dictionary */ |
10 | reyssat | 19 | |
20 | /*************** Customization: change values hereafter ****************/ |
||
8123 | bpr | 21 | #include "suffix.h" |
22 | #include "../Lib/libwims.h" |
||
23 | #include "../wimsdef.h" |
||
24 | #include "translator_.h" |
||
7676 | bpr | 25 | /* limit of dictionary entries */ |
10 | reyssat | 26 | #define entrylim 32768 |
7676 | bpr | 27 | /* limit of dictionary length */ |
28 | #define diclim 1024*1024 |
||
10 | reyssat | 29 | |
30 | /***************** Nothing should need change hereafter *****************/ |
||
31 | |||
3718 | reyssat | 32 | |
10 | reyssat | 33 | char inpbuf[MAX_LINELEN+1], outbuf[2*MAX_LINELEN+2]; |
34 | char *dicbuf; |
||
8123 | bpr | 35 | struct entry entry[entrylim]; |
10 | reyssat | 36 | int entrycount; |
8123 | bpr | 37 | int entry_size=sizeof(entry); |
10 | reyssat | 38 | |
39 | int has_digits=0; |
||
40 | int unknown_type=unk_delete; |
||
41 | int nocase=0,leaveline=0; |
||
42 | char *unknown, unkbuf[1024]; |
||
43 | |||
44 | int compare(int i1, const char *s2) |
||
45 | { |
||
46 | int k; |
||
3247 | bpr | 47 | if(nocase) k=strncasecmp((char*)entry[i1].original,s2,entry[i1].olen); |
48 | else k=strncmp((char*)entry[i1].original,s2,entry[i1].olen); |
||
10 | reyssat | 49 | if(k==0 && (isalnum(*(s2+entry[i1].olen)) || (*(s2+entry[i1].olen)&128)!=0)) return -1; |
50 | else return k; |
||
51 | } |
||
52 | |||
7676 | bpr | 53 | /* searches a list. Returns index if found, -1 if nomatch. |
54 | * Uses binary search, list must be sorted. */ |
||
8100 | bpr | 55 | int search_list2(struct entry *list, int items, size_t item_size, const char *str) |
10 | reyssat | 56 | { |
57 | int i1,i2,j,k,t,t1; |
||
58 | unsigned char c; |
||
59 | |||
60 | if(items<=0) return -1; |
||
61 | j=0; c=str[0]; |
||
62 | k=list[0].original[0]-c; if(k==0) k=compare(0,str); |
||
63 | if(k==0) goto more; if(k>0) return -1; |
||
64 | j=items-1; k=list[j].original[0]-c; if(k==0) k=compare(j,str); |
||
65 | if(k==0) return j; |
||
66 | if(k>0) for(i1=0,i2=j;i2>i1+1;) { |
||
7676 | bpr | 67 | j=i1+(i2-i1)/2; |
68 | k=list[j].original[0]-c; if(k==0) k=compare(j,str); |
||
69 | if(k==0) goto more; |
||
70 | if(k>0) {i2=j; continue;} |
||
71 | if(k<0) {i1=j; continue;} |
||
10 | reyssat | 72 | } |
73 | if(k>0) {j--;k=compare(j,str);} |
||
74 | more: |
||
75 | if((t=list[j].earlier)<0) { |
||
7676 | bpr | 76 | if(k==0) return j; else return -1; |
10 | reyssat | 77 | } |
78 | if(compare(t,str)!=0) return -1; |
||
8100 | bpr | 79 | for(j=t1=t,k=0;j<items && list[j].earlier==t1 && (k=compare(j,str))<=0; j++) { |
10 | reyssat | 80 | if(k==0) t=j; |
8100 | bpr | 81 | } |
10 | reyssat | 82 | return t; |
83 | } |
||
84 | |||
7676 | bpr | 85 | /* change all spaces into ' ', and collapse multiple occurences */ |
8100 | bpr | 86 | void singlespace2(char *p) |
10 | reyssat | 87 | { |
88 | char *pp, *p2; |
||
89 | for(pp=p;*pp;pp++) { |
||
7676 | bpr | 90 | if(!isspace(*pp)) continue; |
91 | if(leaveline) { |
||
92 | if(*pp==13) ovlstrcpy(pp,pp+1); |
||
93 | if(*pp=='\n') { |
||
94 | pp++; |
||
95 | gopt: for(p2=pp; isspace(*p2) && *p2!='\n'; p2++); |
||
96 | if(p2>pp) ovlstrcpy(pp,p2); pp--; |
||
97 | } |
||
98 | else { |
||
99 | pp++; if(!isspace(*pp) || *pp=='\n') continue; |
||
100 | goto gopt; |
||
101 | } |
||
102 | } |
||
103 | else { |
||
104 | if(*pp!=' ') *pp=' '; |
||
105 | if(!isspace(*(pp+1))) continue; |
||
106 | for(pp++,p2=pp;isspace(*p2);p2++); |
||
107 | ovlstrcpy(pp,p2); pp--; |
||
108 | } |
||
10 | reyssat | 109 | } |
110 | } |
||
111 | |||
7676 | bpr | 112 | /* Prepare dictionary */ |
10 | reyssat | 113 | void prepare_dic(char *fname) |
114 | { |
||
115 | int i,l; |
||
116 | FILE *dicf; |
||
117 | char *p1, *p2, *pp; |
||
118 | long int flen; |
||
7676 | bpr | 119 | |
10 | reyssat | 120 | entrycount=0; |
121 | dicf=fopen(fname,"r"); if(dicf==NULL) return; |
||
122 | fseek(dicf,0,SEEK_END);flen=ftell(dicf); fseek(dicf,0,SEEK_SET); |
||
123 | if(flen>diclim) return; |
||
124 | dicbuf=xmalloc(flen+16);flen=fread(dicbuf,1,flen,dicf); |
||
125 | fclose(dicf); |
||
126 | if(flen>0 && flen<diclim) dicbuf[flen]=0; |
||
127 | else return; |
||
128 | for(i=0,p1=dicbuf;p1!=NULL && *p1!=0 && i<entrylim;p1=p2) { |
||
7676 | bpr | 129 | p2=strchr(p1+1,'\n'); if(p2>p1) *p2++=0; |
130 | pp=strchr(p1,':'); if(pp==NULL) continue; |
||
131 | *pp++=0; |
||
8100 | bpr | 132 | strip_trailing_spaces2(p1); strip_trailing_spaces2(pp); |
133 | singlespace2(p1); |
||
7676 | bpr | 134 | p1=find_word_start(p1); pp=find_word_start(pp); |
135 | if(*p1==0) continue; |
||
136 | if(has_digits==0) { |
||
137 | char *p; |
||
138 | for(p=p1;*p!=0 && p<pp && !isdigit(*p);p++); |
||
139 | if(isdigit(*p)) has_digits=1; |
||
140 | } |
||
141 | entry[i].original=(unsigned char*)p1; |
||
142 | entry[i].replace=(unsigned char*)pp; |
||
143 | entry[i].olen=l=strlen(p1); entry[i].earlier=-1; |
||
144 | if(i>0) { |
||
145 | int l1,l2; |
||
146 | l1=entry[i-1].earlier; if(l1>=0) l2=entry[l1].olen; |
||
147 | else {l2=entry[i-1].olen;l1=i-1;} |
||
148 | if(l>l2 && isspace(p1[l2]) |
||
149 | && strncmp((char*)entry[l1].original,p1,l2)==0) |
||
150 | entry[i].earlier=entry[i-1].earlier=l1; |
||
151 | } |
||
152 | i++; |
||
10 | reyssat | 153 | } |
154 | entrycount=i; |
||
155 | } |
||
156 | |||
7676 | bpr | 157 | /* now make the translation. */ |
10 | reyssat | 158 | void translate(char *p) |
159 | { |
||
160 | char *p1, *p2, *pp; |
||
161 | int t; |
||
162 | |||
163 | if(entrycount<=0 && suffixcnt<=0) return; |
||
164 | snprintf(outbuf,sizeof(outbuf),"%s",p); |
||
165 | for(p1=find_word_start(outbuf); |
||
7676 | bpr | 166 | p1!=NULL && p1-outbuf<MAX_LINELEN && *p1!=0; |
167 | p1=p2) { |
||
168 | p2=find_word_end(p1); |
||
169 | for(pp=p1;pp<p2 && |
||
170 | ((!has_digits && isalpha(*pp)) || |
||
171 | (has_digits && isalnum(*pp)) || (*pp&128)!=0 || |
||
172 | strchr("_",*pp)!=NULL);pp++); |
||
173 | p2=find_word_start(p2); |
||
174 | if(pp==p1 || |
||
175 | (has_digits==0 && isdigit(*pp)) || |
||
176 | (*pp!=0 && !isspace(*pp) && strchr(",.?!/;",*pp)==NULL)) continue; |
||
8100 | bpr | 177 | t=search_list2(entry,entrycount,sizeof(entry[0]),p1); |
7676 | bpr | 178 | if(t<0) { |
179 | switch(unknown_type) { |
||
180 | case unk_leave: break; |
||
181 | case unk_delete: { |
||
182 | ovlstrcpy(p1,find_word_start(pp)); p2=p1; |
||
183 | break; |
||
184 | } |
||
185 | case unk_replace: { |
||
8100 | bpr | 186 | string_modify3(outbuf,p1,pp,unkbuf); |
7676 | bpr | 187 | p2=find_word_start(p1+strlen(unkbuf)); |
188 | } |
||
189 | } |
||
190 | continue; |
||
191 | } |
||
8100 | bpr | 192 | string_modify3(outbuf,p1,p1+strlen((char*)entry[t].original), |
7676 | bpr | 193 | (char*)entry[t].replace); |
194 | p2=find_word_start(p1+strlen((char*)entry[t].replace)); |
||
10 | reyssat | 195 | } |
196 | snprintf(p,MAX_LINELEN,"%s",outbuf); |
||
197 | } |
||
198 |