使用Java检索python脚本的输出 - java

我想运行一个基于语音识别的Python脚本,并使用Java检索脚本输出。

我设法轻松地调用了脚本并运行了它。它完美地工作。但是我不明白为什么我不能用Java恢复输出print

这是脚本python:

import aiml
import os
import time, sys
import pyttsx
import warnings

# Initialisation of the different mode
# If no specification, Jarvis will run as a text Personnal
mode = "text"
if len(sys.argv) > 1:
    if sys.argv[1] == "--voice" or sys.argv[1] == "voice":
        import speech_recognition as sr
        mode = "voice"

# Jarvis speaking part
def offline_speak(jarvis_speech):
    engine = pyttsx.init()
    engine.say(jarvis_speech)
    engine.runAndWait()

# Jarvis listenning part
def listen():
    # Jarvis listen the environnemnt to capture the voice
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Talk to JARVIS: ")
        # Jarvis is listenning
        audio = r.listen(source)
    try:
        # Print and return what Jarvis heard
        print ("test")
        print r.recognize_google(audio, language='fr-FR')
        return r.recognize_google(audio, language='fr-FR')
    except sr.UnknownValueError:
        # If Jarvis doesn't know the sentence or the word you said
        offline_speak("Je n'ai pas compris ce que vous avez dit, pouvez vous repeter s'il vous plait ?")
        print ("test")
        # Return what he heard
        return(listen())
    except sr.RequestError as e:
        # Jarvis didn't understand what you said
        print("Could not request results from Speech Recognition service; {0}".format(e))


# Jarvis running part
while True:
    if mode == "voice":
        response = listen()
        print ("test")
    else:
        response = raw_input("Talk to JARVIS : ")

    offline_speak(response)
    print ("test")

这是我的java类:

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PythonCaller {

    private final String pythonPrjPath;
    private final String scriptName;
    private final String args;


    public PythonCaller(String scriptName, String args) {
        this.scriptName = scriptName;
        this.pythonPrjPath = argTreatment();
        this.args = args;
    }


    public void call() throws Exception {
        try {

            List<String> commands = new LinkedList<>();
            commands.add("python");
            commands.add(pythonPrjPath);
            commands.add(args);

            ProcessBuilder pb = new ProcessBuilder(commands);
            Process p = pb.start();

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

            // read the output from the command
            String s;
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

        } catch (Exception e) {
            throw e;
        }
    }


    private String argTreatment() {
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource(scriptName).getFile());
        StringBuilder resPpythonPrjPath = new StringBuilder(file.getAbsolutePath());
        StringBuilder sb = new StringBuilder(resPpythonPrjPath.subSequence(0,90));
        return sb.toString();
    }



    public static void main(String[] args) {

        String tabArgs = "voice";
        PythonCaller pc = new PythonCaller("listener.py", tabArgs);
        try {
            pc.call();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

谢谢你的帮助

参考方案

我无法解决我的问题,所以我决定避免它。我不读取print,而是将要检索的数据写入文本文件,然后从Java读取文本文件。

Java-搜索字符串数组中的字符串 - java

在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…

Java RegEx中的单词边界\ b - java

我在使用\b作为Java Regex中的单词定界符时遇到困难。对于text = "/* sql statement */ INSERT INTO someTable"; Pattern.compile("(?i)\binsert\b");找不到匹配项Pattern insPtrn = Pattern.compile(&…

Java:找到特定字符并获取子字符串 - java

我有一个字符串4.9.14_05_29_16_21,我只需要获取4.9。数字各不相同,所以我不能简单地获得此char数组的前三个元素。我必须找到最正确的.并将其子字符串化直到那里。我来自Python,因此我将展示Python的实现方法。def foobar(some_string): location = some_string.rfind('.&…

Java Double与BigDecimal - java

我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…

如何使用正则表达式匹配以相反顺序排列的后两个字符为前两个字符的任何字符串 - java

Closed. This question needs to be more focused。它当前不接受答案。                                                                                                                            …