java - Twitter4j:在主题标签中搜索关键字

标签 java twitter filter twitter4j hashtag

我正在使用 Twitter4j 开发应用程序。 我正在尝试导入带有特定主题标签的推文(例如:天气) 然后,我想通过搜索关键字对带有该主题标签的推文进行分类。

例如: 导入的一些推文可能是

- OMG, I hate this rain #weather
- This sunshine makes me feel so happy #weather
- Such strange #weather! One moment it rains, the next the sun shines. Confusing!
- Rain makes me sad #weather
- I love the sunshine! #weather

然后,我想将这些推文归类为:

- hate, Confusing, sad,... are negative 
- happy, love,... are positive

PositiveTweets 将是:

- This sunshine makes me feel so happy #weather
- I love the sunshine! #weather

NegativeTweets 将是:

- OMG, I hate this rain #weather
- Such strange #weather! One moment it rains, the next the sun shines. Confusing!
- Rain makes me sad #weather

因此,NegativeTweets=3PositiveTweets=2

任何人都可以帮助我解决这个问题或指出类似的事情吗?

最佳答案

您可以查询#weather 主题标签,然后根据推文是否包含您为好天气或坏天气指定的任何关键字将推文分成单独的列表。

public static void main(String[] args) throws TwitterException {
    List<Tweet> goodWeather = new ArrayList<Tweet>();
    List<Tweet> badWeather = new ArrayList<Tweet>();

    Twitter twitter = new TwitterFactory().getInstance();
    System.out.println("Fetching Weather Data...");

    // get the 1000 most recent tweets tagged #weather
    for (int page = 1; page <= 10; page++) {
        Query query = new Query("#weather");
        query.setRpp(100); // 100 results per page
        query.setPage(page);
        QueryResult qr = twitter.search(query);
        List<Tweet> qrTweets = qr.getTweets();

        // break out if there are no more tweets
        if(qrTweets.size() == 0) break;

        // separate tweets into good and bad bins
        for(Tweet t : qrTweets) {
            if (t.getText().toLowerCase().contains("happy") ||
                t.getText().toLowerCase().contains("love")) {
                goodWeather.add(t);
            }

            if (t.getText().toLowerCase().contains("sad") ||
                t.getText().toLowerCase().contains("hate")) {
                badWeather.add(t);
            }
        }
    }

    System.out.println("Good Weather: " + goodWeather.size());
    for (Tweet good : goodWeather) {
        System.out.println(good.getCreatedAt() + ": " + good.getText());
    }

    System.out.println("\nBad Weather: " + badWeather.size());
    for (Tweet bad : badWeather) {
        System.out.println(bad.getCreatedAt() + ": " + bad.getText());
    }
}

关于java - Twitter4j:在主题标签中搜索关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11189990/

相关文章:

java - 如何在 HBase 表中查找在特定列中具有特定值的所有行

java - 泛型类到相同泛型类型的泛型数组

java - 嵌入式 Neo4j 是如何工作的?

java - UrlImageViewHelper 使用 ImageView 大小调整图像大小

iPhone/iOS - 如何使用 "ShareKit"仅发布到 Facebook 或仅发布到 Twitter

python - Twitter API - Python - 150 个请求/小时,即使在经过身份验证后

hadoop - Storm Twitter 访问 key 不正确或丢失

java - 将 Maven WAR 插件与配置文件结合使用

python - 对象上的 SQLalchemy 过滤器

filter - 自动点击智能过滤栏中的 "GO"