2012-06-25 72 views
3

我正在使用Twitter4j开发应用程序。 我试图用某个hashtag(例如:weather)导入tweets 然后,我想通过搜索关键词将tweets与该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

谁能帮我这个或点我对类似的东西?

回答

5

您可以查询#weather hashtag,然后根据它们是否包含您为好或坏天气指定的任何关键字将这些tweet分为单独的列表。

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

我想你想要做的是Sentiment Analysis看你怎么检索鸣叫的许多是积极的,有多少是负面的,对不对?一个好的开端是查看SentiWordNet它有很多单词已经存储了它们的极性,它们是一个单词的正面还是反面,它只是一个包含所有这些数据的文本文件。您需要解析它并将数据存储在某个数据结构中。一旦你完成了所有这些,你只需扫描推文并匹配单词并获得分数,然后标记推文。它不像听起来那么难,先搜索SentiWordNet。我相信这是更好的方法,因为它会帮助你长期运行:)

希望这对我有帮助