Question

In this assignment you will implement the second version of your spell checker. Using the randomized...

In this assignment you will implement the second version of your spell checker. Using the randomized dictionary (random_dictionary.txt) given, read in the dictionary into an array of 26 Binary Search Trees (BST) , one for each letter of the alphabet. Thus the first BST would contain only those words starting with letter 'a', while the last would contain only those words starting with letter 'z'. Then, when you read in the book (oliver.txt), you examine the first character of each word, and search (this method is already implemented in BinarySearchTee class) the word in one of the BST. For each word read from the book, see if it is in the corresponding BST. If it is not found, then output the word. This word is either mis-spelled, or not in the dictionary. The words in the dictionary and the book should be run through the String Parser you wrote in Assignment 4. It is not necessary to store the book in a BST, only the dictionary should be stored. You are allowed to implement any other methods you see necessary. DO NOT use Java API containers to create BST. Use the BinaryTree and other support classes we have already implemented in the class. You should count the number of words found, number of words not found, number of comparisons for words found, and number of comparisons for words not found just as in assignment 4. Maintain counters to do this. At the end of the program, print out the average number of comparisons for the words found and average comparisons for words not found.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include<string.h>

char dictionary[1000000];
char article[100000];

void spellCheck(char[], char[]);
int isLetter(char c);
void removePunc(char article[]);
void toLower( char article[]);
void lowerDictionary( char dictionary[]);
int artLength( char article[]);
void nextArticleWord(char article[], char articleWord[],  int artLength, char dictionary[]);

int main(void) {
    FILE* dict_file;
    FILE* article_file;
    int bytes_read;
    char* p;

    dict_file = fopen("american-english.txt", "r");
    if (dict_file == 0) {
        printf("unable to open dictionary file \"american-english.txt\"\n");
        return -1;
    }

    article_file = fopen("article.txt", "r");
    if (article_file == 0) {
        printf("unable to open file \"article.txt\"\n");
        return -1;
    }

    /* read dictionary */
    p = dictionary;
    p = fgets(p, 100, dict_file);
    while (p != 0) {
        while (*p != '\0') { 
            p += 1; 
        }
        p = fgets(p, 100, dict_file);
    }

    /* read article */
    p = article;
    bytes_read = fread(p, 1, 1000, article_file);
    p += bytes_read;
    while (bytes_read != 0) {
        bytes_read = fread(p, 1, 1000, article_file);
        p += bytes_read;
    }
    *p = 0;

    spellCheck(article, dictionary);
}   



int articlePosition =0;
int dictionaryPosition = 0;




void spellCheck(char article[], char dictionary[]) {
    char articleWord[50]; 
    char dictionaryWord[50];
    int articleLength = artLength(article);
    removePunc(article);
    toLower(article);
    lowerDictionary(dictionary);
    nextArticleWord(article, articleWord, articleLength, dictionary);

}

void nextDictionaryWord(char dictionary[], char dictionaryWord[]){
    int i;
    for(i =0; dictionary[dictionaryPosition] != '\n'; i++){
        dictionaryWord[i] = dictionary[dictionaryPosition];
        dictionaryPosition++;
    }
}
int isLetter(char c){
    if ( (c>='a'&&c<='z') || (c>='A'&&c<='Z'))
        return 1;
    return 0;
}

void removePunc(char article[]){
    int i, j=0;
    for ( i =0; article[i] != 0; i++){
        if (isLetter(article[i])){
            article[j] = article[i];
            j++;
        }
        else if (!isLetter(article[i])){
            article[j] = ' ';
            j++;
        }       
    }
}

void toLower( char article[]){
    int i=0;
    for( i; article[i] != 0; i++){
        if ( article[i] >= 'A' && article[i] <='Z')
            article[i] = article[i] + 32;
    }
}

void lowerDictionary( char dictionary[]){
    int i=0;
    for(i; dictionary[i] != 0; i++){
        if (dictionary[i] >= 'A' && dictionary[i] <= 'Z'){
            dictionary[i] = dictionary[i] + 32;
        }
    }
}


int articleLength( char article[] ){
    int count=0;
    while (article[count] != 0)
        count++;
    return count;
}

void nextArticleWord(char article[], char articleWord[],  int articleLength, char dictionaryWord[], char dictionary[]){
    int j, i;
check:
    while(!isLetter(article[articlePosition])){
        if (article[articlePosition] == 0){
            return;
        }
        articlePosition++;
    }
    for(j=0; article[articlePosition] != ' ' || articlePosition == articleLength; j++){
        articleWord[j] = article[articlePosition];
        articlePosition++;
    }   

    if (strlen(articleWord)<2){
        goto check;
    }
    articleWord[j+1] = 0;
    //dictionary search
        while (!strncmp(articleWord, dictionaryWord,strlen(articleWord))){
            nextDictionaryWord(dictionary, dictionaryWord);
        }
        if(strncmp(articleWord, dictionaryWord,strlen(articleWord)))
            return;
        printf(articleWord);
}
Add a comment
Know the answer?
Add Answer to:
In this assignment you will implement the second version of your spell checker. Using the randomized...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment you will implement the second version of your spell checker. Using the randomi...

    In this assignment you will implement the second version of your spell checker. Using the randomized dictionary (random_dictionary.txt) given, read in the dictionary into an array of 26 Binary Search Trees (BST) , one for each letter of the alphabet. Thus the first BST would contain only those words starting with letter 'a', while the last would contain only those words starting with letter 'z'. Then, when you read in the book (oliver.txt), you examine the first character of each...

  • Overview: The goal of this assignment is to implement a simple spell checker using a hash...

    Overview: The goal of this assignment is to implement a simple spell checker using a hash table. You will be given the basic guidelines for your implementation, but other than that you are free to determine and implement the exact classes and methods that you might need. Your spell-checker will be reading from two input files. The first file is a dictionary containing one word per line. The program should read the dictionary and insert the words into a hash...

  • 26-ary tree for spell checker in JAVA You are asked to write some functionalities for a...

    26-ary tree for spell checker in JAVA You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number...

  • Write a spell checker that stores a set of words, W, in a hash table and...

    Write a spell checker that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a spell check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, because it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to spellCheck(s) returns a...

  • INSTRUCTIONS : Make-up a UML Diagram for this Question. Write a spell checker that stores a...

    INSTRUCTIONS : Make-up a UML Diagram for this Question. Write a spell checker that stores a set of words, W, in a hash table and implements a function,spellCheck(s), which performs a spell check on the string swith respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, because it is assumed to be spelled correctly in this case. Otherwise, if s is not in W,...

  • Read a "dictionary" file and store the listed words in an array (not an arraylist) Analyze...

    Read a "dictionary" file and store the listed words in an array (not an arraylist) Analyze a given text file "file.txt" and search for every word in "file.txt" in the "dictionary.txt" file using a binary search algorithm. (You may not use the binarySearch method in arrays class) Print words not found in the dictionary as potentially incorrect; count the number of incorrectly spelled/not located words, count the number of correctly spelled words, and count the total number of words. Output...

  • Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...

    Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can be a string of one or more words (it’s your choice of implementation, you can restrict the meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same . Your dictionary application must provide its operations through the following menu (make sure that...

  • Complete the following code: You are asked to write some functionalities for a spelling checker i...

    Complete the following code: You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number 26 indicates that...

  • Vliestion (1) while make testman I will generate an executable called Testman IX to test your...

    Vliestion (1) while make testman I will generate an executable called Testman IX to test your program for Question (2). If you just issue make, then both will be generated. (1) In this question, you will re-implement the question in Assignment 1 by using vectors instead dynamic arrays. All the functionalities are the same. Of course, you need to make appropriate changes to the function declarations by using vectors instead of pointers to strings or string arrays). See below. Also...

  • Could you guys write an efficient java program to implement BST. Your java program should read...

    Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name. To compile: javac xxxxx5.java To execute: java xxxxx5 < any data file name Your main method should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT