- #1
JessicaHelena
- 188
- 3
- Homework Statement
- This method queries the tweets of a particular user's handle.
Find the most common word in the user's tweets and also return the frequency.
- Relevant Equations
- N/A
So in the class TJTwitter, I have the following defined:
and in the same class, I have a few methods that are meant to help me achieve the goal of the problem.
1. I have a splitIntoWords() method that takes each status and splits them into individual words, which are then stored in List terms.
2. I have an iteratorToList method that turns an iterator into a List:
3. I have the removeCommonEnglishWords(), which is supposed to remove common English words from the list of terms. (I am given a file called commonWords.txt, which is in the same folder as this java file). The method should also not throw an exception:
However, this, when run, does not alter the terms at all. But when I uncomment the last line (terms = listToIterator(termIt);), I get an empty term, I think. What am I doing wrong?
(When I check the terms List at the start of removeCommonEnglishWords(), the terms List is not empty.)
Java:
private Twitter twitter;
private PrintStream consolePrint;
private List<Status> statuses;
private List<String> terms;
private String popularWord;
private int frequencyMax;
and in the same class, I have a few methods that are meant to help me achieve the goal of the problem.
1. I have a splitIntoWords() method that takes each status and splits them into individual words, which are then stored in List terms.
2. I have an iteratorToList method that turns an iterator into a List:
Java:
181 public static <String> List<String> iteratorToList(Iterator<String> iterator)
182 {
183 List<String> list = new ArrayList<>();
184 iterator.forEachRemaining(list::add);
185 return list;
186 }
3. I have the removeCommonEnglishWords(), which is supposed to remove common English words from the list of terms. (I am given a file called commonWords.txt, which is in the same folder as this java file). The method should also not throw an exception:
Java:
194 @SuppressWarnings("unchecked")
195 private void removeCommonEnglishWords()
196 {
197 Scanner sc = null;
198
199 try
200 {
201 sc = new Scanner(new File("commonWords.txt"));
202 }
203 catch(Exception e)
204 {
205 System.out.println("The file is not found");
206 }
207
208 List<String> commonWords = new ArrayList<String>();
209 int count = 0;
210 while(sc.hasNextLine())
211 {
212 count++;
213 commonWords.add(sc.nextLine());
214 }
215
216 Iterator<String> termIt = terms.iterator();
217 while(termIt.hasNext())
218 {
219 String term = termIt.next();
220 for(String word : commonWords)
221 if(term.equalsIgnoreCase(word))
222 termIt.remove();
223 }
224 //terms = iteratorToList(termIt);
225 }
However, this, when run, does not alter the terms at all. But when I uncomment the last line (terms = listToIterator(termIt);), I get an empty term, I think. What am I doing wrong?
(When I check the terms List at the start of removeCommonEnglishWords(), the terms List is not empty.)
Last edited by a moderator: