java - 如何连接Python聊天机器人和Java聊天室

标签 java python artificial-intelligence chatbot

我使用 Python 编写聊天机器人程序。当它收到消息时,它会计算要说什么并返回消息。

我的 friend 用Java写了一个聊天室。这是一个常见的聊天室,但是当人类发送消息时,会将其发送给聊天机器人。

如何连接它们?它们在同一台电脑上运行并且不使用互联网。

最佳答案

您可以使用运行时类来完成此操作。示例代码:

public String sendMessage(String message) throws IOException {
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("python /Users/user/bot.py " + message);

    BufferedReader stdInput = new BufferedReader(new
            InputStreamReader(proc.getInputStream()));

    BufferedReader stdError = new BufferedReader(new
            InputStreamReader(proc.getErrorStream()));

    // read the output from the command
    String s = null;
    StringBuilder answer = new StringBuilder();
    while ((s = stdInput.readLine()) != null) {
        answer.append(s);
    }

    return answer.toString();
}

关于java - 如何连接Python聊天机器人和Java聊天室,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45295734/

相关文章:

java - CREATE TABLE 的 SQL 语法错误

java - JScrollPaneLayout怎么设置和JTable一样?

python - 获取列表中给定项目的计数(不使用 numpy)

python - 根据 Pandas 中的当前值填充序列先前值

python - 为乒乓球游戏的神经网络寻找合适的参数

performance - 如何计算A-star算法的运行时间

java - 如何根据json数组长度在对话框中添加动态复选框?

java - 如何使用单个 replaceAll 查找 Java 中两个字符串的公共(public)字符?

python - NLP 生成按列中的值分组的并置三元组数据框

matlab - 神经网络输出 :Scaling the output range