斯坦福CoreNLP服务器-如何从Java访问结果

斯坦福CoreNLP服务器-如何从Java访问结果,java,android,stanford-nlp,Java,Android,Stanford Nlp,我创建了CoreNLP服务器,可以使用wget命令访问它: wget --post-data 'It is a nice day, isn't it?' 'http://192.168.1.30:9000/?properties={"annotators": "openie", "outputFormat": "json"}' -O res.txt Wget将结果保存到res.txt文件中 我想用java做同样的事情。 我正在尝试使用带有POST模式的HttpUrlConnection,但它

我创建了CoreNLP服务器,可以使用wget命令访问它:

 wget --post-data 'It is a nice day, isn't it?' 'http://192.168.1.30:9000/?properties={"annotators": "openie", "outputFormat": "json"}' -O res.txt
Wget将结果保存到res.txt文件中

我想用java做同样的事情。 我正在尝试使用带有POST模式的HttpUrlConnection,但它返回我FileNotFoundException

我的代码:

class JSON {
static String encode(HashMap<String, String> hm) {
    StringBuilder sb = new StringBuilder("?");
    try {
        for (Map.Entry<String, String> entry : hm.entrySet()) {
            String par = URLEncoder.encode(entry.getKey(), "utf-8");
            String val = URLEncoder.encode(entry.getValue(), "utf-8");
            String link = par + "=" + val;
            sb.append(link + "&");
        }
    } catch (Exception e) { e.printStackTrace(); }
    return sb.toString();
}

static String POST(String s_url, HashMap<String, String> params) {
    StringBuilder sb = new StringBuilder();
    String s_url_params = encode(params).replaceFirst("\\?", "");
    try {
        URL url = new URL(s_url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        //Request header
        connection.setRequestMethod("POST");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");

        //Send post request
        connection.setDoOutput(true);
        DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
        dos.writeBytes(s_url_params);
        dos.flush(); dos.close();

        Log.i("JSONGet", "Sending 'POST' request to '" + s_url+"'");

        //Response - input
        BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String line;
        while ((line=bf.readLine())!=null) {
            sb.append(line);
        }
        bf.close();
        Log.i("JSONGet", "Done.");

    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}
@Test
public void useAppContext() throws Exception {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    HashMap<String, String> params = new HashMap<>();
    params.put("data", "It is a nice day, isn't it?");
    String json = JSON.POST("http://192.168.1.30:9000/?properties={\"annotators\": \"openie\", \"outputFormat\": \"json\"}", params);

    Log.e("Results", "Res: "+json);
}
classjson{
静态字符串编码(HashMap hm){
StringBuilder sb=新的StringBuilder(“?”);
试一试{
对于(Map.Entry:hm.entrySet()){
字符串PAR=URLNECONDECTION.EnCODE(条目.GETKEY),“UTF-8”;
字符串val=URLEncoder.encode(entry.getValue(),“utf-8”);
字符串链接= PAL+“=”+ VALL;
某人附加(链接+“&”);
}
}catch(异常e){e.printStackTrace();}
使某人返回字符串();
}
静态字符串POST(字符串s_url、HashMap参数){
StringBuilder sb=新的StringBuilder();
字符串s\u url\u params=encode(params).replaceFirst(“\\?”,”);
试一试{
URL URL=新URL(s_URL);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//请求头
connection.setRequestMethod(“POST”);
setRequestProperty(“用户代理”、“Mozilla/5.0”);
//发送邮寄请求
connection.setDoOutput(真);
DataOutputStream dos=新的DataOutputStream(connection.getOutputStream());
dos.writeBytes(s_url_参数);
dos.flush();dos.close();
Log.i(“JSONGet”,“向“+s_url+””)发送“POST”请求”;
//响应-输入
BufferedReader bf=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
弦线;
而((line=bf.readLine())!=null){
某人附加(行);
}
bf.close();
Log.i(“JSONGet”,“Done.”);
}捕获(例外e){
e、 printStackTrace();
}
使某人返回字符串();
}
@试验
public void useAppContext()引发异常{
//正在测试的应用程序的上下文。
Context appContext=InstrumentationRegistry.getTargetContext();
HashMap params=新的HashMap();
put(“数据”,“今天天气不错,不是吗?”);
字符串json=json.POST(“http://192.168.1.30:9000/?properties={\'annotators\':\'openie\',\'outputFormat\':\'json\'},参数);
Log.e(“结果”,“结果:”+json);
}

因此,请提前感谢!

斯坦福CoreNLP中有用于创建客户机的Java代码

请参见此处的完整文档页面:

您特别想阅读标题为“Java客户端”的部分


是的,我看到了,但:1.我不知道如何只安装斯坦福CoreNLP客户端库。2.如果我使用它,我将不得不购买CoreNLP或共享我的开源代码,因为商业使用CoreNLP(我的项目是商业性的)将许可证更改为付费。我解决了问题。只需对链接进行编码:urlcoder.encode();
// creates a StanfordCoreNLP object with POS tagging, lemmatization, NER, parsing, and coreference resolution
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
StanfordCoreNLPClient pipeline = new StanfordCoreNLPClient(props, "http://localhost", 9000, 2);
// read some text in the text variable
String text = ... // Add your text here!
// create an empty Annotation just with the given text
Annotation document = new Annotation(text);
// run all Annotators on this text
pipeline.annotate(document);