Write a program thatshall calculatethe vocabulary richness of a text in a file and the frequency of the mostcommonword.The vocabulary richness is the number of words in the text divided by the number...

1 answer below »

Write a program thatshall calculatethe vocabulary richness of a text in a file and the frequency of the mostcommonword.The vocabulary richness is the number of words in the text divided by the number of distinct words.The frequency of a word is the number of times the word is mentioned in the text divided by the total number of words in the text.


Define and implementclass WordCounterwith two private fieldsString wordandint count, constructorWordCounter(String word), and public methodsString getName(),int getCount(), andvoid addToCounter().


Define and implementclass Corpus(as in text corpus) with one private fieldArrayList words, constructorCorpus(BufferedReaderinfile), and public methodsdouble getVocabularyRichness()andString getMostFrequentWord().


Implement a test program (as thepublic static void mainmethodinCorpus) that reads all files in a specific folder, creates aCorpusobject from each (previously opened) file, and saves the requested statistics into another filestats.csv. You can either create a newCorpusobject for each file or define anArrayListof the corpora.


Each line of the CSV file must consist of three fields separated by commas (but no spaces!): the file name, the vocabulary richness, and the most frequently used word. Run your program on allShakespeare's plays.Submit the CSV file together with the Java file.


Suggestions:



  1. ArrayList+ WordCounteris not an efficient implementation of aCorpus, but quite acceptable as an exercise. Any "production" implementation should use aHashMap.

  2. Usefolder.listFiles()to obtain the list of files in the folder. The folder shall be a previously createdFileobject (e.g.,new File(".")for the current folder.)

  3. Consider anything returned by thenext()method as one word (even if it has punctuation).

  4. To count the next word, check if the word is already present in the ArrayList(you will have to use a loop). If it is, increment the word's counter. Otherwise, add another WordCounterto the list.

  5. Convert "words" to the lower case before adding them to the corpus.

  6. The requested statistics can be obtained by combining the counts of the words (e.g., the sum of all counts is the total number of words in the text).

Answered Same DayApr 16, 2021

Answer To: Write a program thatshall calculatethe vocabulary richness of a text in a file and the frequency of...

Aditi answered on Apr 17 2021
142 Votes
Solution/Corpus.java
Solution/Corpus.java
import java.io.BufferedReader;
import java.io.BufferedW
riter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Corpus {
    private ArrayList words;
    public Corpus() {
        words = new ArrayList<>();
    }

    Corpus(BufferedReader infile){
        String line;
        try {
            while((line = infile.readLine()) != null) {
                String allWords[] = line.toLowerCase().split("([,.\\s]+)");
                for (String word : allWords) {
                    boolean isFound = false;
                    for(int i = 0; i < words.size(); i++) { 
                        if (word.equals(words.get(i).getWord())) { 
                            isFound = true; 
                           ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here