作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
已成功获取超过 100 条推文,但现在我无法将这些推文存储在 .csv 文件中?尝试过文件处理类,那么如何存储推文?
public class SentimentAnalysisWithCount {
DoccatModel model;
static int positive = 0;
static int negative = 0;
public static void main(String[] args) throws IOException, TwitterException {
String line = "";
SentimentAnalysisWithCount twitterCategorizer = new SentimentAnalysisWithCount();
twitterCategorizer.trainModel();
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("--------------------------------------------------")
.setOAuthConsumerSecret("--------------------------------------------------")
.setOAuthAccessToken("--------------------------------------------------")
.setOAuthAccessTokenSecret("--------------------------------------------------");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Query query = new Query("udta punjab");
QueryResult result = twitter.search(query);
int result1 = 0;
for (Status status : result.getTweets()) {
result1 = twitterCategorizer.classifyNewTweet(status.getText());
if (result1 == 1) {
positive++;
} else {
negative++;
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\User\\Desktop\\results.csv"));
bw.write("Positive Tweets," + positive);
bw.newLine();
bw.write("Negative Tweets," + negative);
bw.close();
}
public void trainModel() {
InputStream dataIn = null;
try {
dataIn = new FileInputStream("C:\\Users\\User\\Downloads\\tweets.txt");
ObjectStream lineStream = new PlainTextByLineStream(dataIn, "UTF-8");
ObjectStream sampleStream = new DocumentSampleStream(lineStream);
// Specifies the minimum number of times a feature must be seen
int cutoff = 2;
int trainingIterations = 30;
model = DocumentCategorizerME.train("en", sampleStream, cutoff,
trainingIterations);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dataIn != null) {
try {
dataIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public int classifyNewTweet(String tweet) throws IOException {
DocumentCategorizerME myCategorizer = new DocumentCategorizerME(model);
double[] outcomes = myCategorizer.categorize(tweet);
String category = myCategorizer.getBestCategory(outcomes);
System.out.print("-----------------------------------------------------\nTWEET :" + tweet + " ===> ");
if (category.equalsIgnoreCase("1")) {
System.out.println(" POSITIVE ");
return 1;
} else {
System.out.println(" NEGATIVE ");
return 0;
}
}
}
在此代码中,控制台上显示的推文应存储在 .csv 文件中
最佳答案
请从 Stackoverflow 中删除您的 API key 。您不应公开发布它们。
可以将推文存储在 CSV 中,您只需通过调整书面输出来增强您发布的代码片段。以下代码片段应该给出有关如何在 Java 8 中实现它的想法:
try(BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\User\\Desktop\\results.csv"))) {
int positive = 0;
int negative = 0;
StringBuilder sb = new StringBuilder();
for (Status status : result.getTweets()) {
String tweetText = status.getText();
long tweetId = status.getId();
int classificationResult = twitterCategorizer.classifyNewTweet(tweetText);
if (classificationResult == 1) {
positive++;
} else {
negative++;
}
sb.append("ID=").append(tweetId).append(",TEXT=").append(tweetText).append(",classificationResult=").append(classificationResult);
String csvText = sb.toString();
bw.write(csvText);
bw.newLine();
sb.delete(0,csvText);
}
bw.write("##### SUMMARY #####")
bw.write("Positive Tweets," + positive);
bw.newLine();
bw.write("Negative Tweets," + negative);
bw.close();
}catch(IOException e) {
//TODO Exception Handling
}
results.csv
看起来像:
ID=25125125,TEXT=some fancy text here,classificationResult=1
ID=25146734725,TEXT=some fancy text1 here,classificationResult=0
ID=25127575125,TEXT=some fancy text2 here,classificationResult=1
ID=251258979125,TEXT=some fancy text3 here,classificationResult=0
ID=25125867125,TEXT=some fancy text4 here,classificationResult=1
##### SUMMARY #####
Positive Tweets,3
Negative Tweets,2
关于java - 是否可以将推文存储在 csv 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42573710/
我想开发一个 Skype 机器人,它将用户名作为输入,并根据用户输入以相反的字符大小写表示hello username。简而言之,如果用户输入他的名字 james,我的机器人会回复他为 Hello J
我是一名优秀的程序员,十分优秀!