有没有更简单的方法让K最佳解析?
答案 0 :(得分:2)
一般来说,你通过LexicalizedParser
对象做事,这是一个“语法”,提供所有这些东西(语法,词典,索引等)。
从命令行,以下内容将起作用:
java -mx500m -cp "*" edu.stanford.nlp.parser.lexparser.LexicalizedParser -printPCFGkBest 20 edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz data/testsent.txt
在API级别,您需要获得LexicalizedParserQuery
个对象。获得LexicalizedParser lp
后(如ParserDemo.java
),您可以执行以下操作:
LexicalizedParser lp = ... // Load / train a model
LexicalizedParserQuery lpq = lp.parserQuery();
lpq.parse(sentence);
List<ScoredObject<Tree>> kBest = lpq.getKBestPCFGParses(20);
LexicalizedParserQuery
等同于java正则表达式Matcher
。
注意:目前,kBest解析仅适用于PCFG非因式语法。
答案 1 :(得分:0)
这是我根据克里斯托弗·曼宁(Christopher Manning)的上述回答实现的一种变通方法,假设您希望使用Python。用于CoreNLP的Python包装器未实现“ K最佳分析树”,因此可以选择使用终端命令
java -mx500m -cp "*" edu.stanford.nlp.parser.lexparser.LexicalizedParser -printPCFGkBest 20 edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz data/testsent.txt
请注意,您需要将Stanford CoreNLP和所有JAR文件下载到一个目录中,并安装必备的Python库(请参阅import语句)
import os
import subprocess
import nltk
from nltk.tree import ParentedTree
ip_sent = "a quick brown fox jumps over the lazy dog."
data_path = "<Your path>/stanford-corenlp-full-2018-10-05/data/testsent.txt" # Change the path of working directory to this data_path
with open(data_path, "w") as file:
file.write(ip_sent) # Write to the file specified; the text in this file is fed into the LexicalParser
os.chdir("/home/user/Sidney/Vignesh's VQA/SpElementEx/extLib/stanford-corenlp-full-2018-10-05") # Change the working directory to the path where the JAR files are stored
terminal_op = subprocess.check_output('java -mx500m -cp "*" edu.stanford.nlp.parser.lexparser.LexicalizedParser -printPCFGkBest 5 edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz data/testsent.txt', shell = True) # Run the command via the terminal and capture the output in the form of bytecode
op_string = terminal_op.decode('utf-8') # Convert to string object
parse_set = re.split("# Parse [0-9] with score -[0-9][0-9].[0-9]+\n", op_string) # Split the output based on the specified pattern
print(parse_set)
# Print the parse trees in a pretty_print format
for i in parse_set:
parsetree = ParentedTree.fromstring(i)
print(type(parsetree))
parsetree.pretty_print()
希望这会有所帮助。