Subversion Repositories wimsdev

Rev

Rev 3247 | Go to most recent revision | Details | 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
 
18
        /* Versatile translation according to a dictionary */
19
 
20
/*************** Customization: change values hereafter ****************/
21
 
22
        /* limit of dictionary entries */
23
#define entrylim 32768
24
        /* limit of dictionary length */
25
#define diclim  1024*1024
26
 
27
/***************** Nothing should need change hereafter *****************/
28
 
29
char inpbuf[MAX_LINELEN+1], outbuf[2*MAX_LINELEN+2];
30
char *dicbuf;
31
struct entry {
32
    unsigned char *original, *replace;
33
    int olen,earlier;
34
} entry[entrylim];
35
int entrycount;
36
 
37
enum {
38
    unk_delete, unk_leave, unk_replace
39
};
40
 
41
int has_digits=0;
42
int unknown_type=unk_delete;
43
int nocase=0,leaveline=0;
44
char *unknown, unkbuf[1024];
45
 
46
int compare(int i1, const char *s2)
47
{
48
    int k;
49
    if(nocase) k=strncasecmp(entry[i1].original,s2,entry[i1].olen);
50
    else k=strncmp(entry[i1].original,s2,entry[i1].olen);
51
    if(k==0 && (isalnum(*(s2+entry[i1].olen)) || (*(s2+entry[i1].olen)&128)!=0)) return -1;
52
    else return k;
53
}
54
 
55
        /* searches a list. Returns index if found, -1 if nomatch.
56
         * Uses binary search, list must be sorted. */
57
int search_list(struct entry *list, int items, size_t item_size, const char *str)
58
{
59
    int i1,i2,j,k,t,t1;
60
    unsigned char c;
61
 
62
    if(items<=0) return -1;
63
    j=0; c=str[0];
64
    k=list[0].original[0]-c; if(k==0) k=compare(0,str);
65
    if(k==0) goto more; if(k>0) return -1;
66
    j=items-1; k=list[j].original[0]-c; if(k==0) k=compare(j,str);
67
    if(k==0) return j;
68
    if(k>0) for(i1=0,i2=j;i2>i1+1;) {
69
        j=i1+(i2-i1)/2;
70
        k=list[j].original[0]-c; if(k==0) k=compare(j,str);
71
        if(k==0) goto more;
72
        if(k>0) {i2=j; continue;}
73
        if(k<0) {i1=j; continue;}      
74
    }
75
    if(k>0) {j--;k=compare(j,str);}
76
    more:
77
    if((t=list[j].earlier)<0) {
78
        if(k==0) return j; else return -1;
79
    }
80
    if(compare(t,str)!=0) return -1;
81
    for(j=t1=t,k=0;j<items && list[j].earlier==t1 && (k=compare(j,str))<=0; j++)
82
      if(k==0) t=j;
83
    return t;
84
}
85
 
86
        /* change all spaces into ' ', and collapse multiple occurences */
87
void singlespace(char *p)
88
{
89
    char *pp, *p2;
90
    for(pp=p;*pp;pp++) {
91
        if(!isspace(*pp)) continue;
92
        if(leaveline) {
93
            if(*pp==13) strcpy(pp,pp+1);
94
            if(*pp=='\n') {
95
                pp++;
96
                gopt: for(p2=pp; isspace(*p2) && *p2!='\n'; p2++);
97
                if(p2>pp) strcpy(pp,p2); pp--;
98
            }
99
            else {
100
                pp++; if(!isspace(*pp) || *pp=='\n') continue;
101
                goto gopt;
102
            }
103
        }
104
        else {
105
            if(*pp!=' ') *pp=' ';
106
            if(!isspace(*(pp+1))) continue;
107
            for(pp++,p2=pp;isspace(*p2);p2++);
108
            strcpy(pp,p2); pp--;
109
        }
110
    }
111
}
112
 
113
#include "suffix.c"
114
 
115
        /* Prepare dictionary */
116
void prepare_dic(char *fname)
117
{
118
    int i,l;
119
    FILE *dicf;
120
    char *p1, *p2, *pp;
121
    long int flen;
122
 
123
    entrycount=0;
124
    dicf=fopen(fname,"r"); if(dicf==NULL) return;
125
    fseek(dicf,0,SEEK_END);flen=ftell(dicf); fseek(dicf,0,SEEK_SET);
126
    if(flen>diclim) return;
127
    dicbuf=xmalloc(flen+16);flen=fread(dicbuf,1,flen,dicf);
128
    fclose(dicf);
129
    if(flen>0 && flen<diclim) dicbuf[flen]=0;
130
    else return;
131
    for(i=0,p1=dicbuf;p1!=NULL && *p1!=0 && i<entrylim;p1=p2) {
132
        p2=strchr(p1+1,'\n'); if(p2>p1) *p2++=0;
133
        pp=strchr(p1,':'); if(pp==NULL) continue;
134
        *pp++=0;
135
        strip_trailing_spaces(p1); strip_trailing_spaces(pp);
136
        singlespace(p1);
137
        p1=find_word_start(p1); pp=find_word_start(pp);
138
        if(*p1==0) continue;
139
        if(has_digits==0) {
140
            char *p;
141
            for(p=p1;*p!=0 && p<pp && !isdigit(*p);p++);
142
            if(isdigit(*p)) has_digits=1;
143
        }
144
        entry[i].original=p1; entry[i].replace=pp;
145
        entry[i].olen=l=strlen(p1); entry[i].earlier=-1;
146
        if(i>0) {
147
            int l1,l2;
148
            l1=entry[i-1].earlier; if(l1>=0) l2=entry[l1].olen;
149
            else {l2=entry[i-1].olen;l1=i-1;}
150
            if(l>l2 && isspace(p1[l2])
151
               && strncmp(entry[l1].original,p1,l2)==0)
152
              entry[i].earlier=entry[i-1].earlier=l1;
153
        }
154
        i++;
155
    }
156
    entrycount=i;
157
}
158
 
159
        /* now make the translation. */
160
void translate(char *p)
161
{
162
    char *p1, *p2, *pp;
163
    int t;
164
 
165
    if(entrycount<=0 && suffixcnt<=0) return;
166
    snprintf(outbuf,sizeof(outbuf),"%s",p);
167
    for(p1=find_word_start(outbuf);
168
        p1!=NULL && p1-outbuf<MAX_LINELEN && *p1!=0;
169
        p1=p2) {
170
        p2=find_word_end(p1);
171
        for(pp=p1;pp<p2 &&
172
            ((!has_digits && isalpha(*pp)) ||
173
             (has_digits && isalnum(*pp)) || (*pp&128)!=0 ||
174
             strchr("_",*pp)!=NULL);pp++);
175
        p2=find_word_start(p2);
176
        if(pp==p1 ||
177
           (has_digits==0 && isdigit(*pp)) ||
178
           (*pp!=0 && !isspace(*pp) && strchr(",.?!/;",*pp)==NULL)) continue;
179
        t=search_list(entry,entrycount,sizeof(entry[0]),p1);
180
        if(t<0) {
181
            switch(unknown_type) {
182
                case unk_leave: break;
183
                case unk_delete: {
184
                    strcpy(p1,find_word_start(pp)); p2=p1;
185
                    break;
186
                }
187
                case unk_replace: {
188
                    string_modify(outbuf,p1,pp,unkbuf);
189
                    p2=find_word_start(p1+strlen(unkbuf));
190
                }
191
            }
192
            continue;
193
        }
194
        string_modify(outbuf,p1,p1+strlen(entry[t].original),
195
                      entry[t].replace);
196
        p2=find_word_start(p1+strlen(entry[t].replace));
197
    }
198
    snprintf(p,MAX_LINELEN,"%s",outbuf);
199
}
200