Question

In: Computer Science

Java: The goal is to find the number of unique words found in a story file...

Java:

The goal is to find the number of unique words found in a story file text.txt but there some conditions

1. each such word must be found in the dictionary WordList.txt file,

2. each such word should not be among the commonly used ones such as a, it, etc. Such forbidden words are listed in a separate file called stopwords.txt.

Output will be the single number which is the number of unique words in the story file. Use only ArrayLists or arrays as data structures for your coding and eliminate anything other than a-z,A-Z, lowercasing each, removing empty strings.

text.txt:

Louisa May Alcott's novel brings to life vividly the life of New England during the nineteenth century. A life that was tranquil, secure, and productive. It is little wonder, for she drew on her own and on her family's experiences for her work. As one of four daughters growing up in Boston.

WordList.txt:

i
we
did
sometimes
oh
travels
here
there
but
now
it
you
not
alone
serves
neighboring
disk
digitized
aardvark
aardwolf
tranquil

secure

productive.
abandon
abandoned
abandonment
abandons

stopwords.txt:

i
me   
my   
myself
we
our
ours   
ourselves
you
your   
yours
yourself   
yourselves   
he   
him
his

Solutions

Expert Solution

Note: File name should be Solution.java

Below is the required java code

import java.util.*;
import java.io.*;

public class Solution {
    public static void main(String[] args) {
        String story="",dictionary="",forbiddenWords="";

        //reading story words from the story file
        story=readFile("text.txt");

        //Reading Dictionary of wordList
        dictionary=readFile("WordList.txt");

        //Reading forbidden words
        forbiddenWords=readFile("stopwords.txt");

        story=story.toLowerCase();
        String words[]=story.split(" ");
        List<String> uniqueWords = new ArrayList<String>(Arrays.asList(words));

        dictionary=dictionary.toLowerCase();
        String allowedwords[]=dictionary.split(" ");
        List<String> dict = new ArrayList<String>(Arrays.asList(allowedwords));

        forbiddenWords=forbiddenWords.toLowerCase();
        String notAllowedWords[]=forbiddenWords.split(" ");
        List<String> stopwords = new ArrayList<String>(Arrays.asList(notAllowedWords));

        int noOfUniqueWords=UniqueWords(uniqueWords,dict,stopwords);
        //write No Of Unique Words to the story file
        writeFile("text.txt",noOfUniqueWords);

        //Reading No Of Unique Words from the file
        String NoOfUniqueWords=readFile("text.txt");
        System.out.println(NoOfUniqueWords);
    }

    private static String readFile(String filename){
        String s="";

        try{
            File file=new File(filename);
            Scanner reader=new Scanner(file);

            while(reader.hasNextLine()){
                s+=reader.nextLine();//output-No Of Unique words
            }
            reader.close();
        }
        catch (FileNotFoundException e){
            System.out.println("An Error occurred! File not Found..");
            e.printStackTrace();
        }
        return s;
    }

    private static void writeFile(String filename,int n){
        try {
            FileWriter Writer = new FileWriter(filename);
            Writer.write(String.valueOf(n));
            Writer.close();
            System.out.println("Successfully wrote No of Unique Words in the file.");
        }
        catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }

    }

    private static int UniqueWords(List<String> uniqueWords,List<String> dict,List<String> stopwords){
        //Eliminating duplicate words from the story
        for(int i=1; i<uniqueWords.size(); i++) {
            for(int j=0;j<i;j++) {
                if(uniqueWords.get(i).equals(uniqueWords.get(j))) {
                    uniqueWords.remove(i);
                    i--;
                    break;
                }
            }
        }

        //Eliminating words that don't occur in the dict from UniqueWords list
        for(int i=0; i<uniqueWords.size(); i++) {
            int k=0;
            for(int j=0;j<dict.size();j++) {
                if(uniqueWords.get(i).equals(dict.get(j))) {
                    k=1;
                    break;
                }
            }
            if(k==0){
                uniqueWords.remove(i);
                i--;
            }
        }

        // Finally removing the stopwords from the UniqueWords List
        for(int i=0; i<uniqueWords.size(); i++) {
            for(int j=0;j<stopwords.size();j++) {
                if(uniqueWords.get(i).equals(stopwords.get(j))) {
                    uniqueWords.remove(i);
                    i--;
                    break;
                }
            }
        }
       // System.out.println(uniqueWords);
        return uniqueWords.size();
    }
}

**Fell free to ask any queries in the comment section. I am happy to help you. if you like our work, please give Thumbs up**


Related Solutions

Java: The goal is to find the number of unique words found in a story file...
Java: The goal is to find the number of unique words found in a story file text.txt but there some conditions each such word must be found in the dictionary WordList.txt file, each such word should not be among the commonly used ones such as a, it, etc. Such forbidden words are listed in a separate file called stopwords.txt. Your output will be the single number which is the number of unique words in the story file. Use only ArrayLists...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
A file concordance tracks the unique words in a file and their frequencies. Write a program...
A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies.
In Python. A file concordance tracks the unique words in a file and their frequencies. Write...
In Python. A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies. Below is an example file along with the program input and output: Input : test.txt output : 3/4 1, 98 1, AND 2, GUARANTEED 1,...
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Java 20 most frequent words in a text file. Words are supposed to be stored in...
Java 20 most frequent words in a text file. Words are supposed to be stored in array that counts eah word. Write a program that will read an article, parse each line into words, and keep track of how many times each word occurred. Run this program for each of the two articles and print out the 20 most frequently appearing words in each article. You may think you need to use a StringTokenizer, but it turns out that is...
in JAVA, Hash table The goal is to count the number of common elements between two...
in JAVA, Hash table The goal is to count the number of common elements between two sets. Download the following data sets, and add them to your project: girlNames2016.txt boyNames2016.txt These files contain lists of the 1,000 most popular boy and girl names in the US for 2016, as compiled by the Social Security Administration. Each line of the file consists of a first name, and the number of registered births that year using that name. Your task is to...
Assignment: Create data file consisting of integer, double or String values. Create unique Java application to...
Assignment: Create data file consisting of integer, double or String values. Create unique Java application to read all data from the file echoing the data to standard output. After all data has been read, display how many data were read. For example, if 10 integers were read, the application should display all 10 integers and at the end of the output, print "10 data values were read" My issue is displaying how many integers were read and how many strings...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in English (dictionary), to find the number of misspelled words in a story file called Alcott-little-261.txt. Please note that the dictionary words are all lower-cased and there are no punctuation symbols or numbers. Hence it is important that you eliminate anything other than a-z and A-Z and then lower case story words for valid comparisons against the WordList file. When you read each line of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT