Subversion Repositories wimsdev

Rev

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