有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从Java运行带有自定义模块导入的Python脚本

我想从Java的python模块触发一些命令的执行。我的脚本执行时没有错误消息,但没有效果。我使用的Java代码如下所示:

ProcessBuilder pb = new ProcessBuilder("python", "nerDemoTP/python/spacy_test.py");
pb.redirectOutput(Redirect.INHERIT);
Process p;
try {
p = pb.start();
p.waitFor();
} catch (IOException e1) {
...     } 

如果我的工作成功了。py文件只包含print('helloworld'),然后我在Eclipse控制台中看到它。但是我的python脚本应该做什么:

from SpacyNER import SpacyNERExtractor
ner_extractor = SpacyNERExtractor('medium', 'sentences_nyt_200.txt')
annotated_df, entities_dict = ner_extractor.generate_extractions()
ner_extractor.store_predictions('test.csv')
print('spacy extractions done!')

我的SpacyNER模块定义如下:

import pandas as pd
import numpy as np
import spacy
import en_core_web_md
import en_core_web_lg
import operator
import time
import re

class SpacyNERExtractor:

def __init__(self, model, text_file):
    if model == 'medium':
        self.spacy_model = en_core_web_md.load()
    else:
        self.spacy_model = en_core_web_lg.load()
    self.sent_df = pd.read_csv(text_file, sep='\n', header=None, error_bad_lines = False)
    self.sent_df.columns=['sentence']

def set_new_sent_df(self, df_new):
    self.sent_df = df_new


def store_predictions(self, file_name):
    self.df_predictions_spacy.to_csv(file_name,sep="#", index=False)
...

它必须与自定义模块的导入有关,但我不知道如何处理这个问题。另外,当我从命令行执行脚本时,即python3。6 spacy_试验。是的,它起作用了。 感谢您的帮助


共 (0) 个答案