Advertisement
Mary_99

LAB 6

Jan 30th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.93 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. int leven_shtein (const char* word_1, const char* word_2)
  4. {
  5.     int lenght_1 = strlen(word_1);
  6.     int lenght_2 = strlen(word_2);
  7.     int tab[lenght_1 + 1][lenght_2 + 1]; // table  length + 1 (because of first  0)
  8.     int i;
  9.     for (i = 0; i <= lenght_1; i++)
  10.     {                                     //filling first column of the table
  11.         tab[i][0] = i;
  12.     }
  13.     for (i = 0; i <= lenght_2; i++)
  14.     {                                   //filling first row of the table
  15.         tab[0][i] = i;
  16.     }
  17.     for (i = 1; i <= lenght_1; i++)
  18.     { //compare single chars and fill empty spaces in the table
  19.         int j;
  20.         char c_1;
  21.          c_1 = word_1[i-1];
  22.         for (j = 1; j <= lenght_2; j++)
  23.         {
  24.             char c_2;
  25.  
  26.             c_2 = word_2[j-1];
  27.             if ( c_1 == c_2)
  28.             {                                    //if char = char --> fill the current slot
  29.                                             //with the value from the diagonal
  30.                 tab[i][j] = tab[i-1][j-1];
  31.             }
  32.             else
  33.             {                                        //if char != char
  34.                                                      //define the neighbors
  35.                 int left;
  36.                 int top;
  37.                 int diag; //diagonal
  38.                 int min;
  39.  
  40.                 left = tab[i-1][j] + 1;            // number on the left  slot
  41.                 top = tab[i][j-1] + 1;            // number above slot
  42.                 diag = tab[i-1][j-1] + 1;        // number from the diagonal
  43.                 //finding the smallest
  44.                 min = left;
  45.                 if (top < min)
  46.                 {
  47.                     min= top;
  48.                 }
  49.                 if (diag < min)
  50.                 {
  51.                     min = diag;
  52.                 }              // fill the current slot with this value
  53.                 tab[i][j] = min;
  54.             }
  55.         }
  56.     }
  57.     return tab[lenght_1][lenght_2]; //return the last element of the table =levelstein distance
  58. }
  59.  
  60. int main ()
  61. {
  62.  
  63.     FILE* inp;
  64.     inp = fopen("ocr_output.txt","r");
  65.     char ocr_output[5000][50];
  66.     int i = 0;
  67.     while(1)
  68.     {
  69.         char b = (char)fgetc(inp);
  70.         int j = 0;
  71.         while(b!=' ' && b!=',' && b!='.' && b!='\n' && !feof(inp))
  72.         {                                                     //read till /EOF
  73.             ocr_output[i][j++] = b;             //store in arr
  74.             b = (char)fgetc(inp);
  75.         }
  76.         ocr_output[i][j]=0;     //make last character 0/null
  77.         if(feof(inp))
  78.         {                        //checking for EOF
  79.             break;
  80.         }
  81.         i++;
  82.     }
  83.     fclose(inp);
  84.  
  85.     inp = fopen("wrt.dic","r");
  86.     char wrt_output[25000][50];
  87.     i = 0;
  88.     while(1)
  89.     {
  90.         char b = (char)fgetc(inp);
  91.         int j = 0;
  92.         while(b!=' ' && b!=',' && b!='.' && b!='\n' && !feof(inp))
  93.         {
  94.             wrt_output[i][j++] = b;
  95.             b = (char)fgetc(inp);
  96.         }
  97.         wrt_output[i][j]=0;
  98.         if(feof(inp))
  99.         {
  100.             break;
  101.         }
  102.         i++;
  103.     }
  104.     fclose(inp);
  105.  
  106.     int distance[24000];
  107.     int j;
  108.     //check if the word is in the dictionary
  109.     int k = 7; //number of the word
  110.  
  111.     for(i=0; i<24000; i++)
  112.     {
  113.         distance[i] = leven_shtein(ocr_output[k], wrt_output[i]);
  114.         if ( distance[i] == 0 )
  115.         {
  116.             printf("'%s'  found in the dictionary\n", ocr_output[k]);
  117.             break;
  118.         }
  119.         else if (distance[i] == 1 )
  120.         {
  121.             printf("'%s'Not found in the dictionary\n", ocr_output[k]);
  122.             printf(" List of the similar words: \n");
  123.             for(j=0; j<24000; j++)
  124.             {
  125.                 if( leven_shtein(ocr_output[k], wrt_output[j]) == 1 )
  126.                     printf("%s\n", wrt_output[j]);
  127.             }
  128.             break;
  129.         }
  130.  
  131.     }
  132.  
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement