Question

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 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 randomi...
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
  • 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...

  • 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...

  • create a hash table to implement spell checker in java 1. Create a file of 100...

    create a hash table to implement spell checker in java 1. Create a file of 100 words of varying length (max 8 characters). 2. All the words are in lower case. 3. design a hash table. 4. enter each word in the hash table. Once the hash table is created, run it as a spell checker.enter a word (interactive), find the word in the hash table. If not found enter an error message. Then prompt for next word. End the...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • Assignment 1 In this assignment you will be writing a tool to help you play the...

    Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...

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
ADVERTISEMENT