In: Computer Science
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
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**