Subversion Repositories wimsdev

Rev

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