Project specification In this project you are going to imple

Project specification: In this project you are going to implement a word search engine using C++. The search engine would help users to count number of occurrence of a word among given set of text files.

Input specification: The program takes as an input a set of files, containing of multiple words. Words can appear multiple times in a single file or in different files. The input files should be stored in a dedicated directory and loaded in memory upon program execution.

Program functionality:Upon execution, the program should load all the words from the input files in the memory. The program should run until the user explicitly specifies “exit”. The following functionality should be supported:

1. Count how many times a user specified word appears in the files.

2. Display the name of input files and corresponding number of occurrences of the specified word.

Upon execution of you program, you should specify as input parameter the path to a directory containing the input files. If executed (which somesone enters an input like \"cat\"), the program should return the name of files that contain the word \"cat\" and the number of occurrences of the word in each file

First, you need to implement a class for an abstract data type, in which you are going to store files in memory.You need to declare a class Word that would store a word and a pointer to the beginning of a linked list and another class File that would store a file-name and number of times that the word occurred in this file. The process of “loading files in the memory” consists of

(i) creating an object of type Word for each new word that occurs in the set of input files,

(ii) appending this object to a linked list (e.g. the green linked list from the picture),

(iii) creating an object File for each occurrence of the word in a file and

(iv) updating the corresponding (blue) linked list with the object File.

Once you have the files loaded in such structure, the searching would be as easy as finding the queried word in the “green” linked list and tracing in which files does it occur by going over the corresponding “blue” linked list. To start gaining experience in how to structure your C++ projects, you are required to split the different aspects of this program and implement them in separate files. Then you will have to put them all back together to achieve the program functionality. You will need to create several .h and .cpp files for this project. We will help guiding you through this process.

First you need to identify the important object-types and methods you will need for your implementation. In this project, your main object types are going to be class Word and class File. Go ahead and create a file called itemtype.h and declare class File in it.

Then create another file called itemtype.cpp and define your class File in it. These two files are going to be related to objects from the “blue” lists on the picture. The next step is to implement the functionality for creating the “blue” lists. For this purpose, you need to create two more files – list.h and list.cpp. In list.h declare all the methods needed for building a “blue” list and in list.cpp define these methods.

Next, you need to implement the functionality related to objects like these in the “green” list. Create two more files – word.h and word.cpp, declare class Word and all methods to this class in word.h and define them in word.cpp. Now what\'s left is to put it all together by writing a main function that utilizes both file and word. To do so, create wordsearch.h and wordsearch.cpp, declare your main function in wordsearch.h and define it in wordsearch.cpp.

So all in all you will need eight files: wordsearch.h, wordsearch.cpp, itemtype.h, itemtype.cpp, list.h, list.cpp, word.h, word.cpp.

You need to submit 8 files. Eight of these are the implementation of your project assignment, namely wordsearch.h, wordsearch.cpp, itemtype.h, itemtype.cpp, list.h, list.cpp, word.h, word.cpp.

And yeah that\'s about it.

Solution

import java.io.*; class WordData { // A little class to hold the data for one word. String word; // The word (in lower case letters). int count; // The number of times the word has been found. WordData(String w) { // Constructor creates an object with the specified word // and with the counter initialized to 1. word = w; count = 1; } } public class WordFrequencies { static WordData[] words; // An array to hold the words from the file. // Note that the array will be expanded as // necessary, in the insertWord() subroutine. static int wordCount; // The number of words currently stored in // the array. public static void main(String[] args) { TextReader in; // A stream for reading from the input file. PrintWriter out; // A stream for writing to the output file. String inputFileName; // Input file name, specified by the user. String outputFileName; // Output file name, specified by the user. words = new WordData[10]; // Start with space for 10 words. wordCount = 0; // Currently, there are no words in the array. /* Get the input file name from the user and try to create the input stream. If there is a FileNotFoundException, print a message and terminate the program. */ TextIO.put(\"Input file name? \"); inputFileName = TextIO.getln().trim(); try { in = new TextReader(new FileReader(inputFileName)); } catch (FileNotFoundException e) { TextIO.putln(\"Can\'t find file \\\"\" + inputFileName + \"\\\".\"); return; } /* Get the output file name from the user and try to create the output stream. If there is an IOException, print a message and terminate the program. */ TextIO.put(\"Output file name? \"); outputFileName = TextIO.getln().trim(); try { out = new PrintWriter(new FileWriter(outputFileName)); } catch (IOException e) { TextIO.putln(\"Can\'t open file \\\"\" + outputFileName + \"\\\" for output.\"); TextIO.putln(e.toString()); return; } /* Read all the words from the input stream and insert them into the array of words. Reading from a TextReader can result in an error of type TextReader.Error. If one occurs, print an error message and terminate the program. */ try { while (true) { // Skip past and non-letters in the input stream. If an // end-of-stream has been reached, end the loop. Otherwise, // read a word and insert it into the array of words. while ( ! in.eof() && ! Character.isLetter(in.peek()) ) in.getAnyChar(); if (in.eof()) break; insertWord(in.getAlpha()); } } catch (TextReader.Error e) { TextIO.putln(\"An error occurred while reading from the input file.\"); TextIO.putln(e.toString()); return; } /* Output the words in alphabetical order. */ out.println(\"Words in alphabetical order, with the number of occurrences:\"); out.println(\"-----------------------------------------------------------\"); out.println(); putWordList(out); /* Sort the list of words according the frequency with which they were found in the file, and then output the list again. */ sortByFrequency(); out.println(); out.println(); out.println(\"Words in order of frequency:\"); out.println(\"---------------------------\"); out.println(); putWordList(out); if (out.checkError()) { TextIO.putln(\"Some error occurred while writing to the output file.\"); TextIO.putln(\"The output might be missing, incomplete, or corrupted.\"); } else { TextIO.putln(\"Done.\"); } } // end main() static void insertWord(String w) { // Insert the word w into the array of words, unless it already // appears there. If the word already appears in the list, // add 1 to the counter for that word. The words in the array are in // lower case, and w is converted to lower case before it is processed. // Note that the words in the array are kept in alphabetical order. // If the array has no more space to hold w, then it is doubled // in size. int pos = 0; // This will be the position in the array where w belongs. w = w.toLowerCase(); /* Find the position in the array where w belongs, after all the words that precede w alphabetically. If a copy of w already occupies that position, then it is not necessary to insert w, so just add 1 to the counter associated with the word and return. */ while (pos < wordCount && words[pos].word.compareTo(w) < 0) pos++; if (pos < wordCount && words[pos].word.equals(w)) { words[pos].count++; return; } /* If the array is full, make a new array that is twice as big, copy all the words from the old array to the new, and set the variable, words, to refer to the new array. */ if (wordCount == words.length) { WordData[] newWords = new WordData[words.length*2]; System.arraycopy(words,0,newWords,0,wordCount); words = newWords; } /* Put w into its correct position in the array. Move any words that come after w up one space in the array to make room for w. Create a new WordData object to hold the new word. */ for (int i = wordCount; i > pos; i--) words[i] = words[i-1]; words[pos] = new WordData(w); wordCount++; } // end insertWord() static void putWordList(PrintWriter output) { // Write the list of words from the words array, along with their // associated frequencies, to the output stream specified in the // parameter to this subroutine. Words are output in a column // that is 15 spaces wide. If an individual word is longer than // 15 spaces, the output won\'t be quite so neat. for (int i = 0; i < wordCount; i++) { output.print(\" \"); output.print(words[i].word); for (int space = words[i].word.length(); space < 15; space++) output.print(\" \"); output.print(\" \"); output.println(words[i].count); } } // end putWordList() static void sortByFrequency() { // Use insertion sort to sort the words in the list so that they // are in order of decreasing frequency. (Note that insertion sort // has the following neat property: In a group of words that all // have the same frequency, the words will remain in alphabetical // order after the words have been sorted by frequency.) for (int i = 1; i < wordCount; i++) { WordData temp = words[i]; // Save the word in position i. int location; // An index in the array. location = i - 1; while (location >= 0 && words[location].count < temp.count) { // Bump words that have frequencies less than the frequency // of temp up one space in the array. words[location + 1] = words[location]; location--; } words[location + 1] = temp; // Put temp in last vacated space. } } // end sortByFrequency() }
Project specification: In this project you are going to implement a word search engine using C++. The search engine would help users to count number of occurren
Project specification: In this project you are going to implement a word search engine using C++. The search engine would help users to count number of occurren

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site