0

I am a beginner in Java programming and I have an assignment where I need to get posts from the Twitter API and implement a logic using Eclipse Java Photon.

I managed to get the Twitter posts using Twitter 4j API but I am stuck of how to calculate the average words per tweet for the last 5 tweets.

Can anyone help me understand how to do this please ?

1 Answers1

0

You can get tweet contents and then split them into words. After that you can get average word count by counting the words and dividing it to tweet count.

This code may help you to achieve that logic:


public class Solution {
    public static void main(String args[]) {
        Solution.printAverageWordCount();
    }

    public static void printAverageWordCount(){
        Query query = new Query("");
        query.setCount(100);
        query.setResultType(Query.RECENT);

        int statusLimit = 5;
        int wordCount = 0;
        int tweetCount;

        do {
            QueryResult result = twitter.search(query);
            statuses = result.getTweets();
            System.out.prinln("Fetch " + statuses.size() + " statuses. Completed in: " + result.getCompletedIn());

            tweetCount = 0;

            for (Status status : statuses) {
                wordCount += status.getText().split("\\s+").length;
                tweetCount++;
                if(tweetCount >= statusLimit) {
                    break;
                }
            }

            query = result.nextQuery();
        } while (tweetCount < statusLimit);


        // calculate average

        double average = wordCount/(double)tweetCount;
        System.out.println(average);
    }

}
kadir
  • 589
  • 1
  • 7
  • 29
  • Do you have an idea of how can I output this method please with System.out.println and where do I need to call it ? – Jisimni Josh Apr 16 '20 at 15:02
  • You can return the `average` variable from the function and then print it out where you call this function – kadir Apr 16 '20 at 19:12
  • Exception in thread "main" 400:The request was invalid. An accompanying error message will explain why. This is the status code will be returned during version 1.0 rate limiting(https://dev.twitter.com/pages/rate-limiting). In API v1.1, a request without authentication is considered invalid and you will get this response. message - Query parameters are missing. code - 25 – Jisimni Josh Apr 17 '20 at 08:12
  • I have encountered with the error above. For the Logger class I am using these import org.slf4j.Logger; import org.slf4j.LoggerFactory; and this static Logger logger = LoggerFactory.getLogger(FirstTwitterApp.class); but I am having this error... SLF4J: No SLF4J providers were found. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details. Last question, do I need to implement the ConfigurationBuilder in the main method ? – Jisimni Josh Apr 17 '20 at 15:23
  • Actually you don't have use logger, instead you can use System.out.println to print same message to the console – kadir Apr 17 '20 at 17:50
  • Updated again @AyrtonMicallef – kadir Apr 17 '20 at 17:51
  • Thanks a lot, you were really helpful. – Jisimni Josh Apr 18 '20 at 15:50
  • glad to help you – kadir Apr 18 '20 at 16:02