Subversion Repositories wimsdev

Rev

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