Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.86 KB | None | 0 0
  1.  
  2.  
  3. #include "autocomplete.h"
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. static int TotalWords=0;
  10. static int TotalMemory=0;
  11.  
  12. typedef struct table{
  13.     struct table *nextLevel[26];
  14.     char *completions[10];
  15.     int lastIndex;
  16. }
  17. table;
  18.  
  19. static struct table Root = { {NULL}, {NULL}, 0 };
  20.  
  21. void AutoComplete_AddWord(const char *word)
  22. {
  23.     // Begin at root
  24.     struct table* current = &Root;
  25.  
  26.     // Flags for inserted words and new words
  27.     int haveInsert=0;
  28.     int index;
  29.     int newword = 0;
  30.  
  31.     int level;
  32.  
  33.     // We do 4 levels because we count the root table as a level.
  34.     // So the project documentation asks for 3 levels, but because
  35.     // we're counting root, it becomes 4 levels for this code.
  36.  
  37.     // If the word is less than 4 letters long, then we need to end
  38.     // the loop with word[level]!='\0'
  39.     for (level=0; level<4 && word[level] != '\0'; level++)
  40.     {
  41.         // Grab the index
  42.         index=word[level] - 'a';
  43.  
  44.         // If the index is invalid (not a regular letter), then replace it
  45.         // with an x
  46.         if(index>26 || index<0)
  47.         {
  48.             index = 'x'-'a';
  49.         }
  50.  
  51.         // If the next table pointer for this letter has not been allocated
  52.         // then we need to allocate it
  53.         if(current->nextLevel[index]==NULL)
  54.         {
  55.             // allocate the memory
  56.             current->nextLevel[index] = (table*)malloc(sizeof(struct table)); // allocated memory(first word) is put into next layer
  57.  
  58.             // Add it to our memory used variable
  59.             TotalMemory+=sizeof(struct table); //add newly allocated
  60.  
  61.             // Initialize the newly allocated memory
  62.             int i;
  63.             for(i=0;i<26;i++)
  64.             {
  65.                 current -> nextLevel[index] -> nextLevel[i] = NULL; //set all pointer to null
  66.  
  67.                 // lastIndex is set to 1 because we copy the word into completions list later
  68.                 current -> nextLevel[index] -> lastIndex = 1; //initialize lat index
  69.  
  70.                 if(i<10)
  71.                 {
  72.                     current -> nextLevel[index] -> completions[i]=NULL;
  73.                 } //set completions list to null
  74.  
  75.                 // We're going to copy this word into completions right here
  76.                 // and we're going to flag this as a new word.
  77.                 current->nextLevel[index]->completions[0] = _strdup(word);
  78.                 newword = 1;
  79.  
  80.                 // Add the memory from strdup
  81.                 TotalMemory += strlen(word);
  82.             }
  83.         }
  84.  
  85.         int i;
  86.  
  87.         // We need to check to see if this is a duplicate word so long as this is not a new word
  88.         for(i=0; i< current->lastIndex && newword == 0; i++)
  89.         {
  90.             if(!strcmp(current-> completions[i], word)) //is word in completiion list
  91.             {
  92.                 return; //word found
  93.             }
  94.         }
  95.  
  96.         // Add this word to the completions list for the current table level
  97.         if(current->lastIndex <10)
  98.         {
  99.             current -> completions[current->lastIndex] = _strdup(word);
  100.             current->lastIndex++;
  101.             haveInsert=1; //flag as insert word into completed words
  102.  
  103.             TotalMemory += strlen(word); //keeping track of strdup - allocated memory
  104.         }
  105.  
  106.         // Move to the next table level
  107.         current = current->nextLevel[index];
  108.     }
  109.  
  110.     TotalWords++; //add a word
  111. }
  112.  
  113. int AutoComplete_AddDictionary(const char *filename)
  114. {
  115.     char array[100];
  116.  
  117.     FILE *file = fopen(filename, "r");
  118.  
  119.     if(file==NULL)
  120.     {
  121.         //printf("%s not found",filename);
  122.         return 1;
  123.     }
  124.  
  125.     while(!feof(file))
  126.     {
  127.         //end of file not reached
  128.         fscanf(file,"%s",&array[0]);
  129.  
  130.         // For every word encountered in the file, send it to our
  131.         // add word function to add our word to autocomplete's internal dictionary
  132.         AutoComplete_AddWord(array);
  133.     }
  134.  
  135.     fclose( file );
  136.     return 0;
  137. }
  138.  
  139. int AutoComplete_TotalMemory()
  140. {
  141.     return TotalMemory;
  142. }
  143.  
  144. int AutoComplete_TotalWords()
  145. {
  146.     return TotalWords;
  147. }
  148.  
  149.  
  150. char **AutoComplete_SuggestCompletion(const char *word, int *number)
  151. {
  152.     // Begin at the root level
  153.     struct table *current = &Root;
  154.  
  155.     int i;
  156.     int index;
  157.  
  158.     // Go through 4 levels (3 + root)
  159.     for(i=0; i<4 && word[i] != '\0'; i++) //iterations dependent upon num of typed ldetters
  160.     {
  161.         // Grab the index
  162.         index = word[i] - 'a';
  163.         if(index <0 || index>26)
  164.         {
  165.             index = 'x'-'a'; //retrieve indexs 0 to 26, if higher replace with x
  166.         }
  167.  
  168.         // If the next level is NULL, then we don't need to proceed, and can suggest based upon
  169.         // what is already there.
  170.         if(current != NULL && current->nextLevel[index] != NULL )
  171.         {
  172.             current = current->nextLevel[index]; //move onto next level
  173.         }
  174.  
  175.     }
  176.  
  177.     int numWords = current -> lastIndex; //use lastindex to find out how many words can guess out
  178.  
  179.     // If we don't have suggestions, return NULL
  180.     if( numWords == 0 )
  181.         return NULL;
  182.  
  183.     // Set the number of results equal to the lesser of the two: requested number and retrieved results.
  184.     if(*number<numWords)
  185.     {
  186.         numWords=*number; //num of max suggestions wanted
  187.     }
  188.     if( *number > numWords )
  189.     {
  190.         *number = numWords;
  191.     }
  192.  
  193.     char** suggested = (char**)malloc(sizeof(char*)*numWords); //allocate list of pointers
  194.  
  195.     for(i=0;i<numWords;i++)
  196.     {
  197.         suggested[i]=_strdup(current->completions[i]); //copy suggested completions at current node
  198.     }
  199.  
  200.     return suggested;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement