Subversion Repositories wimsdev

Rev

Rev 8185 | Rev 8898 | 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. /* Sort dictionary */
  19.  
  20. /*************** Customization: change values hereafter ****************/
  21.  
  22. /* limit of dictionary entries */
  23. #define entrylim 512*1024
  24. /* limit of dictionary length */
  25. #define diclim 32*1024*1024
  26. /* separation character */
  27. char sepchar=':', grpchar=0;
  28.  
  29. /***************** Nothing should need change hereafter *****************/
  30.  
  31. #include "../Lib/libwims.h"
  32. #include "suffix.h"
  33.  
  34. char inpbuf[MAX_LINELEN+1], outbuf[2*MAX_LINELEN+2];
  35. char *dicbuf;
  36. char dicname[1024], suffixname[1024];
  37.  
  38. struct entry {
  39.     char *original;
  40.     char *replace;
  41. } entry[entrylim];
  42. int entrycount;
  43.  
  44. int nocase=0, hassuffix=0, leaveline=0;
  45. int entrycount, ocount;
  46.  
  47. int compare(const void *s1, const void *s2)
  48. {
  49.     const struct entry *p1, *p2;
  50.     p1=s1; p2=s2;
  51.     if(nocase) return strcasecmp(p1->original,p2->original);
  52.     else return strcmp(p1->original,p2->original);
  53. }
  54.  
  55. void sortdic(void)
  56. {
  57.     qsort(entry,entrycount,sizeof(entry[0]),compare);
  58. }
  59.  
  60. /* change all spaces into ' ', and collapse multiple occurences */
  61. void singlespace2(char *p)
  62. {
  63.     char *pp, *p2;
  64.     for(pp=p;*pp;pp++) {
  65.       if(!isspace(*pp)) continue;
  66.       if(leaveline) {
  67.           if(*pp==13) ovlstrcpy(pp,pp+1);
  68.           if(*pp=='\n') {
  69.             pp++;
  70.             gopt: for(p2=pp; isspace(*p2) && *p2!='\n'; p2++);
  71.             if(p2>pp) ovlstrcpy(pp,p2); pp--;
  72.           }
  73.           else {
  74.             pp++; if(!isspace(*pp) || *pp=='\n') continue;
  75.             goto gopt;
  76.           }
  77.       }
  78.       else {
  79.           if(*pp!=' ') *pp=' ';
  80.           pp++; if(!isspace(*pp)) continue;
  81.           for(p2=pp;isspace(*p2);p2++);
  82.           ovlstrcpy(pp,p2); pp--;
  83.       }
  84.     }
  85. }
  86.  
  87. /* Prepare dictionary */
  88. void prepare_dic(void)
  89. {
  90.     int i;
  91.     FILE *dicf;
  92.     char *p1, *p2, *pp;
  93.     long int flen;
  94.  
  95.     entrycount=0;
  96.     dicf=fopen(dicname,"r"); if(dicf==NULL) return;
  97.     fseek(dicf,0,SEEK_END);flen=ftell(dicf); fseek(dicf,0,SEEK_SET);
  98.     if(flen>diclim) return;
  99.     dicbuf=xmalloc(2*flen+1024);flen=fread(dicbuf,1,flen,dicf);
  100.     fclose(dicf);
  101.     if(flen>0 && flen<diclim) dicbuf[flen]=0;
  102.     else return;
  103.     for(i=0,p1=dicbuf;p1!=NULL && *p1!=0 && i<entrylim;p1=p2) {
  104.       p2=strchr(p1+1,'\n'); if(p2>p1) *p2++=0;
  105.       pp=strchr(p1,sepchar); if(pp==NULL) continue;
  106.       *pp++=0;
  107.       strip_trailing_spaces2(p1); strip_trailing_spaces2(pp);
  108.       singlespace2(p1);
  109.       p1=find_word_start(p1); pp=find_word_start(pp);
  110.       if(*p1==0) continue;
  111.       entry[i].original=p1; entry[i].replace=pp; i++;
  112.     }
  113.     entrycount=i;
  114. }
  115.  
  116. void output(void)
  117. {
  118.     int i;
  119.     FILE *f;
  120.  
  121.     ocount=0;
  122.     strcat(dicname,".sorted");
  123.     f=fopen(dicname,"w"); if(f==NULL) return;
  124.     for(i=0;i<entrycount;i++) {
  125.       if(i>0 && strcmp(entry[i].original,entry[i-1].original)==0
  126.          && strcmp(entry[i].replace,entry[i-1].replace)==0)
  127.         continue;
  128.       if(grpchar!=0) {
  129.           if(i>0 && strcmp(entry[i].original,entry[i-1].original)==0)
  130.             fprintf(f,"%c%s",grpchar, entry[i].replace);
  131.           else {
  132.             if(i>0) fprintf(f,"\n");
  133.             fprintf(f,"%s%c%s",entry[i].original,sepchar,entry[i].replace);
  134.             ocount++;
  135.           }
  136.  
  137.       }
  138.       else {
  139.           fprintf(f,"%s%c%s\n",entry[i].original,sepchar,entry[i].replace);
  140.           ocount++;
  141.       }
  142.     }
  143.     if(grpchar!=0) fprintf(f,"\n");
  144.     fclose(f);
  145. }
  146.  
  147. int main(int argc, char *argv[])
  148. {
  149.     char *ss, *gr;
  150.     if(argc<2) return -1;
  151.  
  152.     ss=getenv("dicsort_separator");
  153.     if(ss!=NULL && *ss!=0) sepchar=*ss;
  154.     gr=getenv("dicsort_grouping");
  155.     if(gr!=NULL && *gr!=0) grpchar=*gr;
  156.     snprintf(dicname,sizeof(dicname)-128,"%s",argv[1]); prepare_dic();
  157.     if(argc>2) {
  158.       snprintf(suffixname,sizeof(suffixname),"%s",argv[2]);
  159.       suffix_dic(suffixname); hassuffix=1;
  160.     }
  161.     else suffixname[0]=hassuffix=0;
  162.     sortdic(); output();
  163.     printf("%s: sorted %d entries.\n",dicname, ocount);
  164.     return 0;
  165. }
  166.  
  167.