Advertisement
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>
- //define the data structure of the dictionnary => defining the node structure (trie)
- typedef struct node
- {
- 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 *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;
- // 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)
- {
- // check if dictionary is loaded
- if (root == NULL)
- {
- printf("root is NULL");
- return false;
- }
- //set pointer to root, first node,
- node *pointer = root;
- pointer = root;//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// ide tells me to replace "\0" by strncmp
- for (int i = 0; i < word[LENGTH + 1]; i++) //(int letter_position = 0; word[letter] != EOF; letter++)
- {
- //allowed only alphabetical characters and apostrophes
- if (isalpha(c) || (c == '\'' && letter_position > 0)) //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
- int letter_position = tolower(word[i]);
- //word[letter_position] = c //match the caracter of the text to the pointer in dictionary
- //letter_position++ // move to the next letter position// find the position of the letter
- // go to next letter and check if it doesnt exist to declare mispelling at the wrong letter encounter
- if (pointer->children[letter_position] == NULL)
- {
- return false;
- }
- else // if not NULL, then move to the next node
- {
- pointer = pointer->children[letter_position];
- }
- }
- //Only return true if the pointer goes until the end of the word
- if (pointer->word[LENGTH])
- return true;
- else
- return false;
- }
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char *dictionary)
- {
- //create memory space for the first node (lenght depend of the word)
- root = 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;
- }
- //keep pointer at the first node, the root
- node *pointer = root;
- pointer = root;
- // until there are no words -> end of file is reached
- while (pointer->children[letter_position] != NULL)
- {
- //get dictionaryfile
- for (int c = fgetc(fp); c!= EOF; c = fgetc(fp))
- {
- if(c != 'n' && c != EOF)
- {
- if (isalpha(c) || (c== '\'' && letter_position > 0))
- {
- // check if next node exist if not NULL
- if (pointer->children[letter_position] == NULL)
- {
- //create memory for the next node
- node *new = malloc(sizeof(node));
- pointer->children[letter_position] = new;
- }
- //move to the next node
- pointer = pointer->children[letter_position];
- }
- }
- //if newline means end of word in dictionary reached
- else if (c == 'n')
- {
- letter_position++;
- pointer = root;
- }
- }
- }
- 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 (letter_position > 0)
- return letter_position;
- // if dictionnary hasn't been loaded, return 0
- else
- return 0;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- pointer = root; //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 + 1); 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 *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 + 1); letter_position++)
- {
- //if children is a pointer so a parent to his children, check also that children)
- if (pointer->children[letter_position] != NULL)
- {
- freeNode(pointer->children[letter_position]);
- }
- // when all children are null, it time to free node
- if (root == NULL)
- {
- free(pointer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement