带有逻辑的Twitter API [英] Twitter API with a Logic applied to it

查看:98
本文介绍了带有逻辑的Twitter API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java编程的初学者,我需要完成一项作业,需要从Twitter API获取帖子并使用Eclipse Java Photon实现逻辑.

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.

我设法使用Twitter 4j API来获取Twitter帖子,但是我对如何计算最近5条推文中每条推文的平均单词感到困惑.

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 ?

推荐答案

您可以获取tweet内容,然后将其拆分为单词.之后,您可以通过对单词进行计数并将其除以tweet计数来获得平均单词计数.

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);
    }

}

这篇关于带有逻辑的Twitter API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆