Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从System.out.println获取JSON_Java_Python_Json_Sentiment Analysis_Alchemyapi - Fatal编程技术网

Java 从System.out.println获取JSON

Java 从System.out.println获取JSON,java,python,json,sentiment-analysis,alchemyapi,Java,Python,Json,Sentiment Analysis,Alchemyapi,我正在尝试做情绪分析,并在谷歌可视化上投射价值观 我正在使用java程序调用此python脚本 代码片段(用于AlchemyAPI) 我编写了一个java程序来调用python脚本。 import java.io.*; public class twitmain { public String twittersentiment(String[] args) throws IOException { // set up the command and parameter

我正在尝试做情绪分析,并在谷歌可视化上投射价值观

我正在使用java程序调用此python脚本

代码片段(用于AlchemyAPI)

我编写了一个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;
    }
}
问题(问题):

我如何将积极的、消极的、中立的信息刮到Google Visualization上?(即制作JSON?

任何帮助都将非常感谢。

天啊,我才意识到你在问另一个问题。用Java编写解析应用程序。 无论如何,想法是一样的,但语言会有所不同。 但这也意味着您可以访问python应用程序的源代码,因此您可以在那里挖掘,并且可以将结果对象作为
JSON
对象转储到控制台中

python的原始答案: 您应该识别行的类型并解析它们,然后自己构造
JSON
对象

对于每一行

import re

json_obj = {}
pattern = "^(\w+): (\d) \((\d{2}\.\d{2}%)\)$"
match = re.match(pattern, line)

if match:
    prop_obj = { "value": match[2], "percent": match[3] }
    json_obj[match[1]] = prop_obj
这将改变路线:

Positive: 3 (60.00%)
进入

进一步考虑这个想法,解析规则应该是
模式
-
提取器方法
s作为键值的字典

var parse_rules = {
    "^(\w+): (\d) \((\d{2}\.\d{2}%)\)$": 
         def (matches): 
            return { match[1]: { "value": match[2], "percent": match[3] }}
    , ...
}
对于每个
,您将根据解析规则进行测试,如果找到匹配项,则执行方法,并将方法的结果合并到
JSON
result对象中

这是大量的工作(取决于java应用程序的复杂性,但是如果java应用程序无法修改,我会这样做)


为什么要从Java调用Python脚本?为什么不让所有的脚本都是Python,这样你就可以简单地返回情绪计数?我正在用Java做我的项目,我正在尝试调用Python脚本。好的,我这里有我的Python脚本
https://github.com/AlchemyAPI/alchemyapi-twitter-python
。如何转储“sco”关于python中JSON的re’和‘情感’?有什么建议吗?老兄,我告诉过你你很棒;)非常感谢!你救了我的命!我会在测试后给你回复!!
Positive: 3 (60.00%)
{
     Positive: {
         value: "3"
         percent: "60.00%"
     }
}
var parse_rules = {
    "^(\w+): (\d) \((\d{2}\.\d{2}%)\)$": 
         def (matches): 
            return { match[1]: { "value": match[2], "percent": match[3] }}
    , ...
}