我正在尝试进行情绪分析并在 Google Visualization 上投射价值观。
我正在使用我的 java 程序调用这个 python 脚本
代码片段(用于 AlchemyAPI)
https://github.com/AlchemyAPI/alchemyapi-twitter-python
我写了一个java程序来调用python脚本。
import java.io.*;
public class twitmain {
public String twittersentiment(String[] args) throws IOException {
// set up the command and parameter
String pythonScriptPath = "/twitter/analyze.py"; // I'm calling AlchemyAPI
String[] cmd = new String[2 + args.length];
cmd[0] = "C:\\Python27\\python.exe";
cmd[1] = pythonScriptPath;
for (int i = 0; i < args.length; i++) {
cmd[i + 2] = args[i];
}
// create runtime to execute external command
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
// retrieve output from python script
BufferedReader bfr = new BufferedReader(new InputStreamReader(
pr.getInputStream()));
String line = ""; int i=0;
while ((line = bfr.readLine()) != null) {
System.out.println(line);
}
return line;
}
}
输出: 我收到如下推文和最终统计数据:
##########################################################
# The Tweets #
##########################################################
@uDiZnoGouD
Date: Mon Apr 07 05:07:19 +0000 2014
To enjoy in case you win!
To help you sulk in case you loose!
#IndiavsSriLanka #T20final http://t.co/hRAsIa19zD
Document Sentiment: positive (Score: 0.261738)
##########################################################
# The Stats #
##########################################################
Document-Level Sentiment:
Positive: 3 (60.00%)
Negative: 1 (20.00%)
Neutral: 1 (20.00%)
Total: 5 (100.00%)
问题(问题):
我如何只抓取正面、负面、中性并将其发送到 Google 可视化?(即制作一个JSON?)
任何帮助将不胜感激。