我正在使用 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=3
和 PositiveTweets=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/