This is my Test cases: "hi.txt" but the most frequent word I got is "is" instead of "Lawrence". plz debug and fix it, show the screenshot of the output and code when its finish I am Lawrence Lawrence is a student Who is Lawrence He is Lawrence here is my main method: import java.util.*; import java.io.*; public class TwitterMain { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("hi.txt")); // Make Scanner over tweet file List tweets = new ArrayList<>(); while (input.hasNextLine()) { // Add each tweet in file to List tweets.add(input.nextLine()); } TweetBot bot = new TweetBot(tweets); // Create TweetBot object with list of tweets TwitterTrends trends = new TwitterTrends(bot); // Create TwitterTrends object // TODO: Call and display results from getMostFrequentWord and your // creative method here String mostFrequentWord = trends.getMostFrequentWord(); System.out.println("The most frequent word is: " + mostFrequentWord); } } Here is my TweetBot method: import java.util.*; import java.io.*; public class TweetBot { private List tweets; private int index; public TweetBot(List tweets){ if(tweets.size() < 1){ throw new IllegalArgumentException("need contain at least one tweet!"); } this.tweets = new ArrayList<>(tweets); } public int numTweets(){ return tweets.size(); } public void addTweet(String tweet) { tweets.add(tweet); if (tweets.size() == 1) { index = 0; } } public String nextTweet() { if (index == tweets.size()) { index = 0; } String tweet = tweets.get(index); index++; return tweet; } public void removeTweet(String tweet) { int tweetIndex = tweets.indexOf(tweet); if (tweetIndex == -1) { // Tweet was not found in the list, so the state of the TweetBot should be unchanged return; } tweets.remove(tweetIndex); if (index > tweetIndex) { // If the removed tweet was before the current index, shift the index back by 1 index--; } else if (index == tweets.size()) { // If the current index is now out of bounds, set it to the last index in the list index = tweets.size() - 1; } } public void reset(){ index = 0; } } This is my TwitterTrends method: import java.util.*; import java.io.*; public class TwitterTrends { // TODO: Your Code Here private TweetBot bot; private List tweets; public TwitterTrends(TweetBot bot){ this.bot = bot; } public String getMostFrequentWord() { Map frequencies = new HashMap<>(); int numTweets = bot.numTweets(); bot.reset(); // Reset the index of the TweetBot object // Count the frequencies of all words in all tweets for (int i = 0; i < numTweets; i++) { String tweet = bot.nextTweet(); String word = ""; for (int j = 0; j < tweet.length(); j++) { char c = tweet.charAt(j); if (Character.isLetter(c)) { word += c; } else if (!word.isEmpty()) { String lowerCaseWord = word.toLowerCase(); if (frequencies.containsKey(lowerCaseWord)) { frequencies.put(lowerCaseWord, frequencies.get(lowerCaseWord) + 1); } else { frequencies.put(lowerCaseWord, 1); } word = ""; } } if (!word.isEmpty()) { String lowerCaseWord = word.toLowerCase(); if (frequencies.containsKey(lowerCaseWord)) { frequencies.put(lowerCaseWord, frequencies.get(lowerCaseWord) + 1); } else { frequencies.put(lowerCaseWord, 1); } } } String mostFrequentWord = ""; int maxFrequency = 0; // Find the most frequent word for (String word : frequencies.keySet()) { int frequency = frequencies.get(word); if (frequency > maxFrequency) { mostFrequentWord = word; maxFrequency = frequency; } } return mostFrequentWord; } }
This is my Test cases: "hi.txt" but the most frequent word I got is "is" instead of "Lawrence".
plz debug and fix it, show the screenshot of the output and code when its finish
I am Lawrence
Lawrence is a student
Who is Lawrence
He is Lawrence
here is my main method:
import java.util.*;
import java.io.*;
public class TwitterMain {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("hi.txt")); // Make Scanner over tweet file
List<String> tweets = new ArrayList<>();
while (input.hasNextLine()) { // Add each tweet in file to List
tweets.add(input.nextLine());
}
TweetBot bot = new TweetBot(tweets); // Create TweetBot object with list of tweets
TwitterTrends trends = new TwitterTrends(bot); // Create TwitterTrends object
// TODO: Call and display results from getMostFrequentWord and your
// creative method here
String mostFrequentWord = trends.getMostFrequentWord();
System.out.println("The most frequent word is: " + mostFrequentWord);
}
}
Here is my TweetBot method:
import java.util.*;
import java.io.*;
public class TweetBot {
private List<String> tweets;
private int index;
public TweetBot(List<String> tweets){
if(tweets.size() < 1){
throw new IllegalArgumentException("need contain at least one tweet!");
}
this.tweets = new ArrayList<>(tweets);
}
public int numTweets(){
return tweets.size();
}
public void addTweet(String tweet) {
tweets.add(tweet);
if (tweets.size() == 1) {
index = 0;
}
}
public String nextTweet() {
if (index == tweets.size()) {
index = 0;
}
String tweet = tweets.get(index);
index++;
return tweet;
}
public void removeTweet(String tweet) {
int tweetIndex = tweets.indexOf(tweet);
if (tweetIndex == -1) {
// Tweet was not found in the list, so the state of the TweetBot should be unchanged
return;
}
tweets.remove(tweetIndex);
if (index > tweetIndex) {
// If the removed tweet was before the current index, shift the index back by 1
index--;
} else if (index == tweets.size()) {
// If the current index is now out of bounds, set it to the last index in the list
index = tweets.size() - 1;
}
}
public void reset(){
index = 0;
}
}
This is my TwitterTrends method:
import java.util.*;
import java.io.*;
public class TwitterTrends {
// TODO: Your Code Here
private TweetBot bot;
private List<String> tweets;
public TwitterTrends(TweetBot bot){
this.bot = bot;
}
public String getMostFrequentWord() {
Map<String, Integer> frequencies = new HashMap<>();
int numTweets = bot.numTweets();
bot.reset(); // Reset the index of the TweetBot object
// Count the frequencies of all words in all tweets
for (int i = 0; i < numTweets; i++) {
String tweet = bot.nextTweet();
String word = "";
for (int j = 0; j < tweet.length(); j++) {
char c = tweet.charAt(j);
if (Character.isLetter(c)) {
word += c;
} else if (!word.isEmpty()) {
String lowerCaseWord = word.toLowerCase();
if (frequencies.containsKey(lowerCaseWord)) {
frequencies.put(lowerCaseWord, frequencies.get(lowerCaseWord) + 1);
} else {
frequencies.put(lowerCaseWord, 1);
}
word = "";
}
}
if (!word.isEmpty()) {
String lowerCaseWord = word.toLowerCase();
if (frequencies.containsKey(lowerCaseWord)) {
frequencies.put(lowerCaseWord, frequencies.get(lowerCaseWord) + 1);
} else {
frequencies.put(lowerCaseWord, 1);
}
}
}
String mostFrequentWord = "";
int maxFrequency = 0;
// Find the most frequent word
for (String word : frequencies.keySet()) {
int frequency = frequencies.get(word);
if (frequency > maxFrequency) {
mostFrequentWord = word;
maxFrequency = frequency;
}
}
return mostFrequentWord;
}
}
Solution:
The issue is that the implementation of getMostFrequentWord()
method is not considering the stop words like "is" and "a" which have a high frequency in the text, but do not contribute to the topic of the text.
One way to fix this is to use a stop words list and exclude them from the frequency count. Here's an updated implementation of the TwitterTrends
class:
Trending now
This is a popular solution!
Step by step
Solved in 3 steps