def subproc(pid, tasks, results, exits, lockTask): setting.runningMode = "normal" setting.load(["file_translation_input_tree","file_translation_input_dep","file_translation_output","size_cube_pruning"]) decoder = GentileDecoder() while True: lockTask.acquire() if not tasks.empty(): task = tasks.get() else: task = None lockTask.release() if not task: exits.put(pid) return tid, lineTree, lineDep = task hyps = decoder.translateNBest(lineTree, lineDep) result = hyps[0].getTranslation() output = result + "\n" msgStream = StringIO.StringIO() hyps[0].trace(stream=msgStream) print >> msgStream, "[%d]" % tid , result msg = msgStream.getvalue() results.put((tid, output, msg))
def subproc(pid, tasks, results, exits, lockTask): setting.runningMode = "mert" setting.load(["file_translation_input_tree","file_translation_input_dep","file_translation_output"]) decoder = GentileDecoder() while True: lockTask.acquire() if not tasks.empty(): task = tasks.get() else: task = None lockTask.release() if not task: exits.put(pid) return tid, lineTree, lineDep = task hyps = decoder.translateNBest(lineTree, lineDep) output = "" for hyp in hyps: line_output = " ||| ".join([str(tid),hyp.getTranslation(), " ".join([str(n) for n in hyp.getLambdas()]) ]) output += line_output + "\n" msg = "[%d] Got %d | %s" % (tid,len(hyps),hyps[0].getTranslation()) results.put((tid, output, msg))
print "[Gentile]", "Interactive Mode" while True: sentence = raw_input("[INPUT]") sentence = sentence.strip() _, pathText = tempfile.mkstemp() _, pathCFG = tempfile.mkstemp() _, pathDep = tempfile.mkstemp() open(pathText, "w").write(sentence) print "[Gentile] Parsing CFG Tree ..." os.system("cat %s | python /home/raphael/research/preprocess-en/preprocess-line.py | /home/raphael/apps/berkley/parse.sh > %s" % (pathText, pathCFG)) print "[Gentile] Parsing DEP Tree ..." os.system("/home/raphael/apps/stanford-parser/dep.berkeley.sh %s > %s" % (pathCFG, pathDep)) cfg = open(pathCFG).read().strip() dep = open(pathDep).read().strip() hyps = decoder.translateNBest(cfg, dep) hyps[0].trace() if len(hyps)==0: print "[%d]" % i , "Translation Failed!!!" foutput.write("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n") continue result = hyps[0].getTranslation() print "[TRANSLATION]", result
elif arg_length == 2: # abraham.py config.yaml setting.runningMode = "normal" setting.load(["file_translation_input_tree","file_translation_input_dep","file_translation_output","size_cube_pruning"]) linesDep = open(setting.file_translation_input_dep).read().split("\n\n") linesTree = open(setting.file_translation_input_tree).readlines() decoder = GentileDecoder() foutput = open(setting.file_translation_output, "w") print "[Abraham]","translate %d sentences..." % (len(linesTree)) for i in range(len(linesTree)): lineTree = linesTree[i].strip() lineDep = linesDep[i].strip() hyps = decoder.translateNBest(lineTree, lineDep) hyps[0].trace() if len(hyps)==0: print "[%d]" % i , "Translation Failed!!!" foutput.write("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n") continue result = hyps[0].getTranslation() print "[%d]" % i , result foutput.write(result+"\n") foutput.close() elif arg_length == 3: # abraham.py config.yaml [cmd] command = sys.argv[1] if command == "mert":