我正在尝试将参数从Java GUI传递给python算法。我已经尝试了许多方法,但是我不断收到无法识别的参数作为错误。我正在使用Process Builder将python代码集成到我的Java中。我需要将fav_movie从Java发送到python。
这是python代码的一部分。我是python初学者,所以如果您能告诉我是什么原因导致此错误,这将是有帮助的
def _fuzzy_matching(self, hashmap, fav_movie):
"""
return the closest match via fuzzy ratio.
If no match found, return None
Parameters
----------
hashmap: dict, map movie title name to index of the movie in data
fav_movie: str, name of user input movie
Return
------
index of the closest match
"""
match_tuple = []
# get match
for title, idx in hashmap.items():
ratio = fuzz.ratio(title.lower(), fav_movie.lower())
if ratio >= 60:
match_tuple.append((title, idx, ratio))
# sort
match_tuple = sorted(match_tuple, key=lambda x: x[2])[::-1]
if not match_tuple:
print('Oops! No match is found')
else:
print('Found possible matches in our database: '
'{0}\n'.format([x[0] for x in match_tuple]))
return match_tuple[0][1]
def parse_args():
parser = argparse.ArgumentParser(
prog="Movie Recommender",
description="Run KNN Movie Recommender")
parser.add_argument('--path', nargs='?', default='C:\java programs\qcri 3\ml-latest-small',
help='input data path')
parser.add_argument('--movies_filename', nargs='?', default='movies.csv',
help='provide movies filename')
parser.add_argument('--ratings_filename', nargs='?', default='ratings.csv',
help='provide ratings filename')
parser.add_argument('--movie_name', nargs='?', default='',
help='provide your favourite movie name')
parser.add_argument('--top_n', type=int, default=10,
help='top n movie recommendations')
return parser.parse_args()
if __name__ == '__main__':
# get args
args = parse_args()
data_path = args.path
movies_filename = args.movies_filename
ratings_filename = args.ratings_filename
movie_name = args.movie_name
top_n = args.top_n
# initial recommender system
recommender = KnnRecommender(
os.path.join(data_path, movies_filename),
os.path.join(data_path, ratings_filename))
# set params
recommender.set_filter_params(50, 50)
recommender.set_model_params(20, 'brute', 'cosine', -1)
# make recommendations
recommender.make_recommendations(movie_name, top_n)
这是显示的错误:
usage: Movie Recommender [-h] [--path [PATH]]
[--movies_filename [MOVIES_FILENAME]]
[--ratings_filename [RATINGS_FILENAME]]
[--movie_name [MOVIE_NAME]] [--top_n TOP_N]
Movie Recommender: error: unrecognized arguments: toy
这是访问python的Java代码:
public static void main(String args[]) throws ScriptException, InterruptedException
{
System.out.println("enter movie name");
Scanner s= new Scanner(System.in);
String name= s.nextLine();
ProcessBuilder pb= new ProcessBuilder("python","recomold.py",name);
System.out.println("running file");
Process process = null;
try {
process = pb.start();
inheritIO(process.getInputStream(), System.out);
inheritIO(process.getErrorStream(), System.err);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int err= process.waitFor();
System.out.println("any errors?"+(err==0 ? "no" : "yes ")+err);
try {
System.out.println("python output "+ output(process.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void inheritIO(InputStream src, PrintStream dest) {
new Thread(new Runnable() {
public void run() {
Scanner sc = new Scanner(src);
while (sc.hasNextLine()) {
dest.println(sc.nextLine());
}
}
}).start();
}
private static String output(InputStream inputStream) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try{
br= new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
}
finally
{
br.close();
}
return sb.toString();
}
}
答案 0 :(得分:1)
您的Python脚本似乎不接受非选项参数作为正确的输入。根据Python程序的帮助文本,您提供的任何参数都必须采用选项/值对形式,例如:
--<option_name> <option_value>
但是您只是传递了“玩具”,没有选项说明符。因此,也许您想要的是Java代码中的类似内容:
ProcessBuilder pb= new ProcessBuilder("python","recomold.py","--movie-name",name);