I want to write a poem. One that I know I’d like, so I’ve decided that the poem
ID: 3666490 • Letter: I
Question
I want to write a poem. One that I know I’d like, so I’ve decided that the poem should contain words from other poems that I like. I’ve collected a sample of poems and put them into the file:
I now want you to create a program, WordCloud.java, that can read in a text file and print out the most commonly used words in the file. Once done, my job of writing a new poem will be easier because I’ll know which words I should definitely use.
Implementation details:
Words occurrence count is case insensitive (River, river, rivER would all count as the same word).
I’m not interested in how many times common words like “the, a, an, ...” (also case insensitive) show up, so your program will also need to read in the text file: /home/linux/ieng6/cs11wb/public/HW6/common.txt
And make sure these common are not counted and don’t appear in the resulting output
The program will take two arguments: the text file to read in and the number of words (N) to print out.
The output of the program will be the top N words along with the number of times each word occurred in the text.
If the user requests the top 100 words, but there are only 50 unique words in the input file, then the top 50 words should be printed.
You’ll need to remove any “,.!?;:-“ from the end of strings that you read, otherwise “time” and “time.” will be counted separately.
If multiple words have the same frequency, you can print them in any order
If the user does not enter the file name to read, or the number of words to report, the
program should state how to use the program (example below):
Explanation / Answer
import java.io.*;
import java.util.*;
public class WordCloud
{
void getWordsFrom(String [] wodsArray)
{
int count=0;
for(String each: wordsArray)
{
for(String i: words)
{
if(each.equals(words.get(i)))
{
count++;
}
else
continue;
}
for(int j=0;j<numW;j++)
{
String newWord[j]= wordsArray[each];
int newCounter[j]= count;
} // end of the outer for loop
printTopNwords(newWord[], newCounter[]);
}
}
void removeCommon()
{
File f1=new File("common.txt");
BufferedReader b= new BufferedReader(new FileReader(f1));
String[ ] common;
while(true)
{
String line;
line= b.readLine();
if(line == null)
{break;}
else
{
common=line.split("[ .,;:!?(){}]");
}
}
for(String each: words)
{
for(String eachCom: common)
{
if(each.equals(eachCom))
{
words.remove(each);
}
else
continue;
}
}
void printTopNwords(String newWord[], int newCounter[])
{
for(int i=0;i<newCounter.length();i++)
{
for(int j=0;j<newWord.length();j++)
{
if(newCounter[i] < newCounter[i+1])
{
int temp=newCounter[i+1];
newCounter[i+1]= newCounter[i];
newCounter[i]=temp;
String tempW=newWord[j+1];
newWord[j+1]= newWord[j];
newWord[j]= tempW;
}
}
System.out.println("Top repeated words are:"+newWords[j]);
}
}
}
public static void main(String [] args)
{
int numW;
System.out.println("enter your file");
Scanner s=new Scanner(System.in);
f1=s.readLine();
File filname= new File(f1);
BufferedReader buf= new BufferedReader(new FileReader(filename));
ArrayList<String> words = new ArrayList<>();
String [] wordsArray;
String line=null;
while(tru)
{
line= buf.readLine();
if(line == null){break;}
else
{
wordArrays =line.split("[ .,;:(){}]");
for(String each: wordsArray)
{
words.toLowercase();
if(!" ".equals(each))
{
words.add(each);
}
}
}
}
System.out.println("enter the number of top words to printout");
numW= Integer.parseInt(s.readLine());
getWordsFrom(wordsArray[]);
removeCommon();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.