Rev 8100 | Rev 11124 | 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 | |||
8123 | bpr | 18 | #include "../Lib/libwims.h" |
19 | #include "suffix.h" |
||
20 | |||
6895 | bpr | 21 | #define suflim 256 |
10 | reyssat | 22 | #define sufbuflim 102400 |
23 | |||
24 | int suffixcnt; |
||
25 | struct { |
||
26 | unsigned char *original; |
||
27 | int olen; |
||
28 | unsigned char *replace; |
||
6895 | bpr | 29 | } |
10 | reyssat | 30 | suf[suflim]; |
31 | char *sufbuf; |
||
32 | int sufwordlen, sufminlen; |
||
33 | |||
6895 | bpr | 34 | /* Suffix translation, to be used within translator. */ |
10 | reyssat | 35 | |
36 | int sufcomp(int t, const unsigned char *s2) |
||
37 | { |
||
38 | int k; |
||
6895 | bpr | 39 | |
10 | reyssat | 40 | for(k=0;k<suf[t].olen && k<sufwordlen |
8100 | bpr | 41 | && suf[t].original[k]==s2[sufwordlen-k-1];k++); |
10 | reyssat | 42 | if(k>=suf[t].olen) { |
8100 | bpr | 43 | if(sufwordlen>k) return -1; else return 0; |
10 | reyssat | 44 | } |
45 | else return suf[t].original[k]-s2[sufwordlen-k-1]; |
||
46 | } |
||
47 | |||
6895 | bpr | 48 | /* searches a list. Returns index if found, -1 if nomatch. |
49 | * This routine is faster than naive one by one comparisons, |
||
8100 | bpr | 50 | * and is especially suited for large lists. |
51 | */ |
||
3247 | bpr | 52 | int suffix_list(void *list, int items, size_t item_size, const unsigned char *str) |
10 | reyssat | 53 | { |
54 | int i1,i2,j,k,t,v; |
||
3247 | bpr | 55 | unsigned char c,d; |
6895 | bpr | 56 | |
10 | reyssat | 57 | if(items<=0) return -1; |
58 | k=sufcomp(0,str); |
||
59 | if(k==0) return 0; if(k>0) return -1; |
||
60 | j=items-1; k=sufcomp(j,str); |
||
61 | if(k==0) return j; |
||
62 | if(k>0) for(i1=0,i2=j;i2>i1+1;) { |
||
8100 | bpr | 63 | j=i1+(i2-i1)/2; k=sufcomp(j,str); |
64 | if(k==0) return j; |
||
65 | if(k>0) {i2=j; continue;} |
||
66 | if(k<0) {i1=j; continue;} |
||
10 | reyssat | 67 | } |
68 | if(k>0 && j>0) j--; |
||
69 | backcheck: |
||
70 | v=j;for(t=0;t<suf[j].olen && t<sufwordlen |
||
8100 | bpr | 71 | && suf[j].original[t]==str[sufwordlen-t-1];t++); |
10 | reyssat | 72 | if(t<sufminlen) return -1; if(t>=suf[j].olen) return j; |
73 | for(j--,c=str[sufwordlen-1],d=str[sufwordlen-t]; |
||
8100 | bpr | 74 | j>=0 && suf[j].original[0]==c && suf[j].olen>t |
75 | && suf[j].original[t-1]==d;j--); |
||
6895 | bpr | 76 | if(j>=0 && suf[j].original[0]==c && |
3247 | bpr | 77 | strncmp((char*)suf[j].original,(char*)suf[v].original,suf[j].olen)==0) |
10 | reyssat | 78 | return j; |
79 | else goto backcheck; |
||
80 | } |
||
81 | |||
6895 | bpr | 82 | /* Prepare dictionary. */ |
10 | reyssat | 83 | void suffix_dic(char *sdicname) |
84 | { |
||
85 | int i,l; |
||
86 | FILE *suff; |
||
87 | char *p1, *p2, *pp; |
||
88 | long int flen; |
||
89 | |||
90 | suffixcnt=0; sufminlen=100000; |
||
91 | suff=fopen(sdicname,"r"); if(suff==NULL) return; |
||
92 | fseek(suff,0,SEEK_END);flen=ftell(suff); fseek(suff,0,SEEK_SET); |
||
93 | if(flen>sufbuflim) return; |
||
94 | sufbuf=xmalloc(flen+16);flen=fread(sufbuf,1,flen,suff); |
||
95 | fclose(suff); |
||
96 | if(flen>0 && flen<sufbuflim) sufbuf[flen]=0; |
||
97 | else return; |
||
98 | for(i=0,p1=sufbuf;p1!=NULL && *p1!=0 && i<suflim;p1=p2) { |
||
6895 | bpr | 99 | p2=strchr(p1+1,'\n'); if(p2>p1) *p2++=0; |
100 | pp=strchr(p1,':'); if(pp==NULL) continue; |
||
101 | *pp++=0; |
||
8100 | bpr | 102 | strip_trailing_spaces2(p1); strip_trailing_spaces2(pp); |
103 | singlespace2(p1); |
||
6895 | bpr | 104 | p1=find_word_start(p1); pp=find_word_start(pp); |
105 | if(*p1==0) continue; |
||
106 | suf[i].original=(unsigned char*)p1; suf[i].olen=l=strlen(p1); |
||
107 | if(l<sufminlen) sufminlen=l; |
||
108 | suf[i].replace=(unsigned char*)pp; i++; |
||
10 | reyssat | 109 | } |
110 | suffixcnt=i; |
||
111 | } |
||
112 | |||
6895 | bpr | 113 | /* Suffix translation. */ |
114 | /* FIXME : ne rien faire si le résultat est de longueur inferieur à 2 |
||
115 | * car ensuite cela sera neglige. |
||
116 | */ |
||
117 | |||
10 | reyssat | 118 | void suffix_translate(char *p) |
119 | { |
||
120 | char *p1, *p2; |
||
121 | int t; |
||
122 | |||
123 | for(p1=find_word_start(p); |
||
8100 | bpr | 124 | p1!=NULL && p1-p<MAX_LINELEN && *p1!=0; |
125 | p1=p2) { |
||
126 | if(!isalpha(*p1)) {p2=p1+1; continue;} |
||
127 | for(p2=p1;isalpha(*p2);p2++); |
||
128 | if(*p2!=0 && strchr(" ,.?!'\"\n`:;()[]{}<>",*p2)==NULL) continue; |
||
129 | sufwordlen=p2-p1; |
||
130 | t=suffix_list(suf,suffixcnt,sizeof(suf[0]),(unsigned char*)p1); |
||
131 | if(t<0) continue; |
||
132 | string_modify3(p,p2-suf[t].olen,p2,(char*)suf[t].replace); |
||
133 | p2=p2-suf[t].olen+strlen((char*)suf[t].replace); |
||
134 | } |
||
135 | p[MAX_LINELEN]=0; |
||
10 | reyssat | 136 | } |
137 | |||
138 | void suffix(char *p, char *sdicname) |
||
139 | { |
||
140 | suffix_dic(sdicname); if(suffixcnt>0) suffix_translate(p); |
||
141 | } |
||
142 |