Subversion Repositories wimsdev

Rev

Rev 8100 | Rev 11124 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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. #include "../Lib/libwims.h"
  19. #include "suffix.h"
  20.  
  21. #define suflim    256
  22. #define sufbuflim 102400
  23.  
  24. int suffixcnt;
  25. struct {
  26.     unsigned char *original;
  27.     int olen;
  28.     unsigned char *replace;
  29. }
  30. suf[suflim];
  31. char *sufbuf;
  32. int sufwordlen, sufminlen;
  33.  
  34. /* Suffix translation, to be used within translator. */
  35.  
  36. int sufcomp(int t, const unsigned char *s2)
  37. {
  38.     int k;
  39.  
  40.     for(k=0;k<suf[t].olen && k<sufwordlen
  41.       && suf[t].original[k]==s2[sufwordlen-k-1];k++);
  42.     if(k>=suf[t].olen) {
  43.       if(sufwordlen>k) return -1; else return 0;
  44.     }
  45.     else return suf[t].original[k]-s2[sufwordlen-k-1];
  46. }
  47.  
  48. /* searches a list. Returns index if found, -1 if nomatch.
  49.  * This routine is faster than naive one by one comparisons,
  50.  * and is especially suited for large lists.
  51.  */
  52. int suffix_list(void *list, int items, size_t item_size, const unsigned char *str)
  53. {
  54.     int i1,i2,j,k,t,v;
  55.     unsigned char c,d;
  56.  
  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;) {
  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;}
  67.     }
  68.     if(k>0 && j>0) j--;
  69.     backcheck:
  70.     v=j;for(t=0;t<suf[j].olen && t<sufwordlen
  71.       && suf[j].original[t]==str[sufwordlen-t-1];t++);
  72.     if(t<sufminlen) return -1; if(t>=suf[j].olen) return j;
  73.     for(j--,c=str[sufwordlen-1],d=str[sufwordlen-t];
  74.       j>=0 && suf[j].original[0]==c && suf[j].olen>t
  75.       && suf[j].original[t-1]==d;j--);
  76.     if(j>=0 && suf[j].original[0]==c &&
  77.        strncmp((char*)suf[j].original,(char*)suf[v].original,suf[j].olen)==0)
  78.       return j;
  79.     else goto backcheck;
  80. }
  81.  
  82. /* Prepare dictionary.  */
  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) {
  99.     p2=strchr(p1+1,'\n'); if(p2>p1) *p2++=0;
  100.     pp=strchr(p1,':'); if(pp==NULL) continue;
  101.     *pp++=0;
  102.     strip_trailing_spaces2(p1); strip_trailing_spaces2(pp);
  103.     singlespace2(p1);
  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++;
  109.     }
  110.     suffixcnt=i;
  111. }
  112.  
  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.  
  118. void suffix_translate(char *p)
  119. {
  120.     char *p1, *p2;
  121.     int t;
  122.  
  123.     for(p1=find_word_start(p);
  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;
  136. }
  137.  
  138. void suffix(char *p, char *sdicname)
  139. {
  140.     suffix_dic(sdicname); if(suffixcnt>0) suffix_translate(p);
  141. }
  142.  
  143.