【发布时间】:2018-01-08 21:32:51
【问题描述】:
我一直在使用pyStatParser 库从文本中生成上下文无关语法,如下所示:
import stat_parser
p = stat_parser.Parser()
grammar = set()
for sentence in sent_tokenize("sometext"):
grammar.update(set(p.parse(sentence).productions()))
print(grammar)
输入:
"The quick brown fox jumps over the lazy dog"
输出
[NP+S -> NP VP, NP -> DT NN, DT -> 'the', NN -> 'quick', VP -> VB NP, VB -> 'brown', NP -> NP PP, NP -> JJ NN, JJ -> 'fox', NN -> 'jumps', PP -> IN NP, IN -> 'over', NP -> DT JJ NN, DT -> 'the', JJ -> 'lazy', NN -> 'dog']
从这个语法中,很容易推导出随机句子。 但是,这非常耗时,所以我查看了其他库,发现 Spacy。不过,我找不到像以前使用以前的库那样创建 CFG 的方法。
问题:我可以在 spacy 中创建 CFG 吗?如果没有,我如何使用这个库来分析文本的语法,然后从这个语法中推导出一个新句子?
【问题讨论】: