Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements a dictionary's functionality.
- */
- #include <stdbool.h>
- #include "dictionary.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- //no need to include mispelling counting because it s integrated into speller
- //define the data structure of the dictionnary => defining the node structure (trie)
- typedef struct node
- {
- bool is_word; //char *word[LENGTH + 1]; // for the max lenght which is 45 + the one for the "\0"
- struct node *children[27]; //array space of 27 for the 26 letters + apostrophes
- }
- node;
- node *root;
- node *temp_pointer;
- int letter_position = 0; // position of letter in tries
- int words = 0; // number of word counting
- int word = 0; // word
- unsigned int c = 0;
- int count = 0; //counting caracter
- // free function for recusion
- void freeNode(node *pointer);
- /**
- * Returns true if word is in dictionary else false. Word not in disctionnary = misspelled
- */
- bool check(const char *word)
- {
- //if one letter is not good, than the word is not in the dictionnary but it s a word => result is misspelled
- // iterate every letters of the word until end of file making sur that max lenght is 27 // ide tells me to replace "\0" by strncmp
- for (int i = 0; i < strlen(word); i++) //(int letter_position = 0; word[letter] != EOF; letter++)
- {
- //allowed only alphabetical characters and apostrophes
- if (isalpha(i)) //letter position > 0 means a word more than 1 letter
- {
- //if letter is capital, make it lower to make sure no sensitive case to be count misspelling by error because of capital
- if (isupper(i))
- {
- letter_position = (i - 65);
- //is it the first caracter of the text word
- if (count == 0)
- {
- if(root->children[letter_position] == NULL)
- return false;
- else
- {
- temp_pointer = root->children[letter_position];
- }
- }
- // caracter is not the first caracter of the letter
- else if(count > 0)
- {
- if (temp_pointer->children[letter_position] == NULL)
- return false;
- else
- {
- //make sure to link all the character of the same word
- temp_pointer = temp_pointer->children[letter_position];
- }
- }
- }
- else if (islower(i))
- {
- letter_position = (i - 97);
- //is it the first caracter of the text word
- if (count == 0)
- {
- if(root->children[letter_position] == NULL)
- return false;
- else
- {
- //make sure that next word start with the root
- temp_pointer = root->children[letter_position];
- }
- }
- // caracter is not the first caracter of the letter
- else if(count > 0)
- {
- if (temp_pointer->children[letter_position] == NULL)
- return false;
- else
- {
- //make sure to link all the character of the same word
- temp_pointer = temp_pointer->children[letter_position];
- }
- }
- }
- }
- else if (c == '\'') //the apostrophe
- {
- letter_position = 26;
- if (temp_pointer->children[letter_position] == NULL)
- return false;
- else
- {
- //move to the next nde
- temp_pointer = temp_pointer->children[letter_position];
- }
- }
- //Only return true if the pointer goes until the end of the word
- else if (c == '\0')
- {
- if(temp_pointer->is_word == false)
- return false;
- else
- {
- temp_pointer->is_word = true;
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char *dictionary)
- {
- //no need to consider the case senstive
- root = malloc(sizeof(node));
- temp_pointer = malloc(sizeof(node));
- // note lenght min for a word is 2 letters (+1) and the max is 45 ( the longest word)
- // open the dictionnary file to read his content
- FILE *fp = fopen(dictionary, "r");
- if (fp == NULL)
- {
- printf("Could not open %s.\n", dictionary);
- return false;
- }
- //get dictionaryfile
- for (int c = fgetc(fp); c!= EOF; c = fgetc(fp))
- {
- if (isalpha(c))
- {
- letter_position = (c - 97);
- //if the caracter is the first letter
- if (count == 0)
- {
- if(root->children[letter_position] == NULL)
- {
- //allocate memory to see next node
- (root->children[letter_position]) = malloc(sizeof(node));
- (root->children[letter_position])->is_word = false;
- }
- //make sure that next word start with the root
- temp_pointer = root->children[letter_position];
- count++;
- words++;
- }
- //caracter is not the firts
- else if (count > 0)
- {
- letter_position = (c - 97);
- if (temp_pointer->children[letter_position] == NULL)
- {
- //move to the next node
- temp_pointer->children[letter_position] = malloc(sizeof(node));
- temp_pointer->children[letter_position]->is_word = false;
- }
- //make sure to link all the character of the same word
- temp_pointer = temp_pointer->children[letter_position];
- count++;
- }
- }
- else if(c == '\'')
- {
- letter_position = 26;
- if (temp_pointer->children[letter_position] == NULL)
- {
- //move to the next node
- temp_pointer->children[letter_position] = malloc(sizeof(node));
- temp_pointer->children[letter_position]->is_word = false;
- }
- temp_pointer = temp_pointer->children[letter_position];
- count++;
- }
- else if (c == '\n')
- {
- temp_pointer->is_word = true;
- count = 0;
- }
- }
- fclose(fp);
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- // if dictionary is loaded, return number of words
- if (words > 0)
- return words;
- // if dictionnary hasn't been loaded, return 0
- else
- return 0;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- //FILE *fp = NULL;
- //go through node's children // make sure to free the childrens and come back to the parent
- for (int letter_position = 0; letter_position < LENGTH; letter_position++)
- {
- //if children is a pointer so a parent to his children, check also that children)
- if (root->children[letter_position] != NULL)
- {
- freeNode(root->children[letter_position]);
- return false;
- }
- else
- return true;
- }
- return true;
- }
- void freeNode(node *temp_pointer)
- {
- //go through node's children // make sure to free the childrens and come back to the parent
- for (int letter_position = 0; letter_position < LENGTH; letter_position++)
- {
- //if children is a pointer so a parent to his children, check also that children)
- if (temp_pointer->children[letter_position] != NULL)
- {
- freeNode(temp_pointer->children[letter_position]);
- }
- // when all children are null, it time to free node
- if (root == NULL)
- {
- free(temp_pointer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment