Question

Hi, I need help with my comp sci assignment. The parameters are listed below, but I...

Hi, I need help with my comp sci assignment.

The parameters are listed below, but I am having trouble generating the number of occurrences of each word. Please use a standard library.

Read in the clean text you generated in part 2 (start a new cpp file). Create a list of all the unique words found in the entire text file (use cleanedTextTest.txt for testing). Your list should be in the form of an array of structs, where each struct has two members. One member contains the word, one member contains the number of occurrences of that word. The struct definition

should look like this:
struct UniqueWord
{
string word;
int numOccurrences;
};


There are two sub-parts that we want to accomplish:-
1. Sort the array in ascending order based on the number of occurrences. Display to the console the number of unique words, the 10 most frequently occurring, and 10 least frequently occurring. Again, since we have not yet covered techniques for “growing” arrays in C++, preallocate your array to some large number. To play it safe, this could mean the number of words in the entire input file (this is assuming worst case scenario, where every word in your input file is unique).
Hint: Consider the algorithm for this before starting to write code. Start by writing pseudocode. You need to think about looking at every word in the input file and comparing it against a list of unique words.


2. Write the contents of the array to 2 new files named “words.txt” and “counts.txt”. Each line of the “words.txt” file will have a unique word, i.e.
word1
word2

Each line of the “counts.txt” file will have the numOccurrences/counts for the corresponding words, i.e.
count of word1
count of word2

Note that the words will be in the ascending order based on the the number of occurrences since your array is already sorted in this fashion.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream> //for cout
#include<fstream> //for ifstream and ofstream
using namespace std;

//structure declaration
struct UniqueWord
{
string word;
int numOccurrences;
};

//present_index function returns -1 if the word is not present in Uniquewords array
//returns the index of the array if the word is present.
int present_index(UniqueWord uw[],string word,int n){
for(int i=0;i<n;i++){
if(uw[i].word==word)
return i;
}
return -1;
}

//sorts the uniqueword array based on numOccurrences in ascending order.
void sort(UniqueWord uw[],int n)
{
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(uw[j].numOccurrences>uw[j+1].numOccurrences){
UniqueWord temp = uw[j];
uw[j] = uw[j+1];
uw[j+1] = temp;
}
}
}
}

//main function
int main()
{
ifstream in; //ifstream to read files.
in.open("cleanedTextTest.txt"); //opens file
if(in==NULL){ //if file opening fails then
cout<<"Unable to open input file"; //prints message and
return -1; //terminates the program.
}

UniqueWord uw[1000]; //declared array of struct of size 1000
int n = 0; //to keep track of number of words stored in array
string word; //to store the read string from file
while(in>>word){ //reads words until end of file gets reached
int index = present_index(uw,word,n); //calls present_index function
if(index==-1){ //if index is -1 then word is not present
uw[n].word = word; //stores the word into array
uw[n].numOccurrences = 1; //assingns the numOccurrences to 1.
n += 1; //increases n value by 1.
}
else uw[index].numOccurrences += 1; //if the word is present then just increases the numOccurrences.
}
in.close(); //closes in stream.

sort(uw,n); //calls sort function

//prints the data about words.
cout<<"Unique Words: "<<n<<endl;
cout<<"\n10 most frequently occurring words\n";
for(int i=n-1;i>=n-10;i--){
cout<<uw[i].word<<" "<<uw[i].numOccurrences<<endl;
}
cout<<"\n10 least frequently occurring words\n";
for(int i=0;i<10;i++){
cout<<uw[i].word<<" "<<uw[i].numOccurrences<<endl;
}

ofstream out1, out2; //declares two ofstreams
out1.open("words.txt"); //opens words.txt file
out2.open("counts.txt"); //opens counts.txt file
if(out1==NULL&&out2==NULL){ //if any file falls to open then
cout<<"Error occurred while opening output files";
return -1; //terminates the program
}
//loops through array and writes into them
for(int i=0;i<n;i++){
out1<<uw[i].word<<"\n";
out2<<uw[i].numOccurrences<<"\n";
}
out1.close(); //closes out1 stream
out2.close(); //closes out2 stream

return 0;
}

//cleanedTextTest.txt file screenshot

deanedTetlest Notepad File Edit Format View Help Nory was a Catholic because her mother was a Catholic and Nory mother was a

//console ouput screenshot

CAC&C+withlbin\Debuglwith.exe Unique Words: 26 10 most frequently occurring words as Catholic 6 because 5 happy 4 and 3 mothe

//words.txt screenshot

words - Notepad File Edit Format View Help hi 1s or been felt saw the others Were knew should feel but not really Nory father

//counts.txt screenshot

counts Note File Edit Forma

//any query, post in the comment section

Add a comment
Answer #2

C++ Code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

   struct UniqueWords{
       string word;
       int numOccurrences;
   };

   UniqueWords List[100];
   int uniqueWordCount = 0;

   void nextWord(string next)
   {
       int wordFound = 0;
       for (int i = 0; i < uniqueWordCount; i++)
       {
           if (List[i].word == next)
           {
               wordFound = 1;
               List[i].numOccurrences++;
               break;
           }
       }
       if (wordFound == 0)
       {
           List[uniqueWordCount].word = next;
           List[uniqueWordCount].numOccurrences = 1;
           uniqueWordCount++;
       }
   }

   void swap(UniqueWords *a, UniqueWords *b)
   {
     UniqueWords temp = *a;
     *a=*b;
     *b=temp;
   }

   void sortWords()
   {
     int i,j;
       for (i = 0; i < uniqueWordCount - 1; i++)
       {
         for (j = 0; j < uniqueWordCount-i-1; j++)
         if (List[j].numOccurrences > List[j+1].numOccurrences)
            swap(&List[j], &List[j+1]);
       }
   }

   int main()
   {
       ifstream ifs;
       string word;
       int count = 0;
       ifs.open("uniqueword.txt");
       if (!ifs)
       {
           cout << "Unable to Open File";
           exit(1);
       }
       while (!ifs.eof())
       {
           ifs >> word;
           nextWord(word);
       }

       sortWords();
       if (uniqueWordCount > 10)
       {
           cout << "\n10 Most frequently Occurring List"<<endl;
           for (int i = uniqueWordCount - 1, count = 1; count <= 10; i--, count++)
           {
               cout << List[i].word << endl;
           }
           cout << "\n10 least frequently Occurring List" << endl;
           for (int i = 0, count = 1; count <= 10; i++, count++)
           {
               cout << List[i].word << endl;
           }
       }
       else
       {
           cout << "Most frequently Occurring List" << endl;
           for (int i = uniqueWordCount - 1; i>=0; i--)
           {
               cout << List[i].word << endl;
           }
           cout << "least frequently Occurring List" << endl;
           for (int i = 0; i < uniqueWordCount; i++)
           {
               cout << List[i].word << endl;
           }
       }
       // writing files
       ofstream wordFile("uniquewordList.txt");
       ofstream countFile("uniquewordcounts.txt");
       if (wordFile.is_open() && countFile.is_open())
       {
           for (int i = 0; i < uniqueWordCount; i++)
           {
               wordFile << List[i].word << "\n";
               countFile << List[i].numOccurrences << "\n";
           }
       }
       wordFile.close();
       countFile.close();
       system("pause");
   }

Input Text file:

Output:

Output text file words:

Output text file count:

Add a comment
Know the answer?
Add Answer to:
Hi, I need help with my comp sci assignment. The parameters are listed below, but I...
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
  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

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

  • # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from #...

    # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from # http://www.cs.uiowa.edu/~cremer/courses/cs1210/etc/ds4/ # Save both in the same folder. # # 2. TA (aloud) and STUDENTS: Read the comments from START HERE! (just after these instructions) # to definition of anagramInfo function. Discuss any questions about what the functions should do. # # 3. TA demonstrate running anagramInfo("wordsMany.txt") on this unchanged file, to # see that it behaves reasonably despite having incomplete anagram-testing functions. #...

  • In this assignment, you will explore more on text analysis and an elementary version of sentiment...

    In this assignment, you will explore more on text analysis and an elementary version of sentiment analysis. Sentiment analysis is the process of using a computer program to identify and categorise opinions in a piece of text in order to determine the writer’s attitude towards a particular topic (e.g., news, product, service etc.). The sentiment can be expressed as positive, negative or neutral. Create a Python file called a5.py that will perform text analysis on some text files. You can...

  • Need some help on this Python Problem called "unique words" the text.txt file can be any...

    Need some help on this Python Problem called "unique words" the text.txt file can be any .txt file with words in it. Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not...

  • Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed...

    Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed by words from the file who’s filename is provided as an argument. It uses the words as keys and the count of occurrences as values. It returns the dictionary it constructed. It can use the ‘tokenize()’ function that is provided in the lecture slides. b. inverse_dict(dict) – This method takes a dictionary (generated by build_word_dictionary() and inverts it (as was done with students and...

  • Problem 1. Implement a C++ program that has the following functions: Reads in a paragraph of...

    Problem 1. Implement a C++ program that has the following functions: Reads in a paragraph of English text up to 100 words from the keyboard and stores this paragraph in a string object. Feel free to include this task in the main() function. Identifies the least frequent letter (case insensitive) in the above paragraph. Implement a separate function getLeastFreqLetter() for this task. The main() function then calls this function to find out the least frequent letter and its frequency. Calculate...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

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