Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5.  
  6. int total=0;
  7.  
  8. struct index
  9. {
  10.     char word[51];
  11.     struct index *ne;
  12. };
  13.  
  14. struct conter
  15. {
  16.     char word[51];
  17.     int n,pos[2000];
  18.     struct conter *ne;
  19. };
  20.  
  21. unsigned int hash(char *str)
  22. {
  23.     unsigned int ha=0;
  24.     char *p;
  25.     for(p=str; *p!='\0'; p++)
  26.         ha = 37*ha + *p;
  27.     return ha % 450000;
  28. }
  29.  
  30. struct index *newindex(char* str)
  31. {
  32.     int i;
  33.     struct index *p=(struct index*)malloc(sizeof(struct index));
  34.     p->ne=NULL;
  35.     for(i=0;str[i]!='\0';i++)
  36.     {
  37.         p->word[i]=str[i];
  38.     }
  39.     p->word[i]='\0';
  40.     return p;
  41. }
  42.  
  43. void append(struct index *head,struct index *newElement)
  44. {
  45.     struct index *p;
  46.     for(p=head;p->ne!=NULL;p=p->ne);
  47.     p->ne=newElement;
  48. }
  49.  
  50. struct conter *newconter(char *str,int pos)
  51. {
  52.     struct conter *p=(struct conter *)malloc(sizeof(struct conter));
  53.     p->ne=NULL;
  54.     p->n=1;
  55.     p->pos[0]=pos;
  56.     strcpy(p->word,str);
  57.     return p;
  58. }
  59.  
  60. void count(struct conter *miss[],char *str,int pos)
  61. {
  62.     unsigned int h=hash(str);
  63.     if(miss[h]==NULL)
  64.     {
  65.         miss[h]=newconter(str,pos);
  66.         total++;
  67.         return;
  68.     }
  69.     struct conter *p;
  70.     for(p=miss[h];p!=NULL;p=p->ne)
  71.     {
  72.         if(!strcmp(str,p->word))
  73.         {
  74.             p->pos[p->n++]=pos;
  75.             return;
  76.         }
  77.     }
  78.     p=newconter(str,pos);
  79.     total++;
  80.     p->ne=miss[h];
  81.     miss[h]=p;
  82. }
  83.  
  84. int emp(const void *a, const void *b)
  85. {
  86.     struct conter** t1=(struct conter * *)a;
  87.     struct conter** t2=(struct conter * *)b;
  88.     int n=(*t1)->n-(*t2)->n;
  89.     if(n)
  90.         return -n;
  91.     return strcmp((*t1)->word,(*t2)->word);
  92. }
  93.  
  94. void print(struct conter * con, FILE * file)
  95. {
  96.     int i;
  97.     fprintf(file,"%s %d %d",con->word,con->n,con->pos[0]);
  98.     for (i = 1; i < con->n; i++)
  99.     {
  100.         fprintf(file," %d", con->pos[i]);
  101.     }
  102.     fprintf(file, "\n");
  103. }
  104.  
  105. int main()
  106. {
  107.     FILE *in,*out;
  108.     in=fopen("dictionary.txt","r");
  109.     char temp[51];
  110.     char t;
  111.     struct index *words[450000];
  112.     struct conter *miss[25000];
  113.     int i,o,pos;
  114.     unsigned int h;
  115.     for(i=0;i<25000;i++)
  116.     {
  117.         words[i]=NULL;
  118.         miss[i]=NULL;
  119.     }
  120.     for(;i<450000;i++)
  121.     {
  122.         words[i]=NULL;
  123.     }
  124.     while(!feof(in))
  125.     {
  126.         i=0;
  127.         do
  128.         {
  129.             t=fgetc(in);
  130.             if(t==EOF)
  131.                 break;
  132.         }while(!((t>='A'&&t<='Z')||(t>='a'&&t<='z')));
  133.         do
  134.         {
  135.             if(t>='A'&&t<='Z')
  136.                 t+='a'-'A';
  137.             temp[i]=t;
  138.             i++;
  139.             t=fgetc(in);
  140.         }while(((t>='A'&&t<='Z')||(t>='a'&&t<='z'))&&i<=50);
  141.         temp[i]='\0';
  142.         h=hash(temp);
  143.         if(words[h]==NULL)
  144.             words[h]=newindex(temp);
  145.         else
  146.             append(words[h],newindex(temp));
  147.     }
  148.     fclose(in);
  149.     in=fopen("article.txt","r");
  150.     pos=-1;
  151.     while(!feof(in))
  152.     {
  153.         i=0;
  154.         do
  155.         {
  156.             t=fgetc(in);
  157.             pos++;
  158.             if(t==EOF)
  159.                 break;
  160.         }while(!((t>='A'&&t<='Z')||(t>='a'&&t<='z')));
  161.         o=pos;
  162.         do
  163.         {
  164.             if(t>='A'&&t<='Z')
  165.                 t+='a'-'A';
  166.             temp[i]=t;
  167.             i++;
  168.             t=fgetc(in);
  169.             pos++;
  170.         }while(((t>='A'&&t<='Z')||(t>='a'&&t<='z'))&&i<=30);
  171.         temp[i]='\0';
  172.         h=hash(temp);
  173.         if(words[h]==NULL)
  174.         {
  175.             count(miss,temp,o);
  176.             continue;
  177.         }
  178.         struct index *p;
  179.         for(p=words[h];p!=NULL;p=p->ne)
  180.         {
  181.             if(!strcmp(temp,p->word))
  182.                 break;
  183.         }
  184.         if(p==NULL)
  185.         {
  186.             count(miss,temp,o);
  187.         }
  188.     }
  189.     fclose(in);
  190.     out=fopen("misspelling.txt","w");
  191.     i=0;
  192.     o=0;
  193.     struct conter *a;
  194.     struct conter **missa=(struct conter * *)malloc(sizeof(struct conter*)*total);
  195.     for(i=0;i<25000;i++)
  196.     {
  197.         a=miss[i];
  198.         while(a!=NULL)
  199.         {
  200.             missa[o++]=a;
  201.             a=a->ne;
  202.         }
  203.     }
  204.     qsort(missa,total,sizeof(struct conter*),emp);
  205.     for(i=0;i<total;i++)
  206.     {
  207.         print(missa[i],out);
  208.     }
  209.     fclose(out);
  210.     return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement