Twitter API finding the most common word in a tweet

In summary, the class TJTwitter has a private Twitter variable, a PrintStream variable, two List variables, a String variable, and an int variable. It also contains three methods, splitIntoWords(), iteratorToList(), and removeCommonEnglishWords(). The splitIntoWords() method splits each status into individual words and stores them in the List terms. The iteratorToList() method turns an iterator into a List. The removeCommonEnglishWords() method is meant to remove common English words from the terms List by using a file called commonWords.txt. However, when run, it does not alter the terms List. When the last line, terms = listToIterator(termIt), is uncommented, the terms List becomes empty. The issue may be
  • #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:
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:
Physics news on Phys.org
  • #2
Please look up the syntax for the IF statement (as used in lns. 221-223).
 

1. What is the Twitter API?

The Twitter API (Application Programming Interface) is a set of tools and protocols that allow developers to access and interact with Twitter's data and functionality.

2. How can I use the Twitter API to find the most common word in a tweet?

To find the most common word in a tweet using the Twitter API, you can use the GET search/tweets endpoint and specify the text of the tweet you want to analyze. Then, you can use a programming language or tool to parse and analyze the text to determine the most common word.

3. Are there any limitations to using the Twitter API to find the most common word in a tweet?

Yes, there are some limitations when using the Twitter API. The most common limitation is the rate limit, which restricts the number of requests you can make in a certain time period. Additionally, the Twitter API does not provide access to all tweets, so your results may not be 100% accurate.

4. Can I use the Twitter API to find the most common word in a tweet from any user?

Yes, you can use the Twitter API to find the most common word in a tweet from any public user account. However, if the user has a private account, you will not be able to access their tweets using the API.

5. Are there any alternatives to using the Twitter API to find the most common word in a tweet?

Yes, there are some alternatives to using the Twitter API. You can use other programming languages or tools to scrape and analyze Twitter data. However, using the Twitter API is the most efficient and reliable method for accessing and analyzing tweets.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
8K
Back
Top