NLTK (自然语言工具包)允许您使用nltk.FCFG.fromstring([grammar string here]).解析FCFG语法,FCFG语法格式规范在哪里?我在谷歌上搜索到死了,但我只找到了这。
*即语法语言规范
发布于 2016-03-12 23:54:20
从演示中:
>>> from nltk import CFG
>>> grammar = CFG.fromstring("""
... S -> NP VP
... PP -> P NP
... NP -> Det N | NP PP
... VP -> V NP | VP PP
... Det -> 'a' | 'the'
... N -> 'dog' | 'cat'
... V -> 'chased' | 'sat'
... P -> 'on' | 'in'
... """)从字符串编写语法的语法应该是这样工作的:
->的LHS上只能有一个非终端。|分隔。发布于 2018-11-26 13:48:50
问题是问FCFG (特征语法),而不是普通的CFG。
我认为您可以在非终端中添加方括号,并在括号中添加一个功能名称、一个等号和一个值。该值可以是变量(以问号开头)、终端符号(用于简单值),也可以是新的功能结构。我在互联网(http://www.nltk.org/howto/featgram.html)上找到了这个例子,它正在我的笔记本电脑上工作。
from nltk import grammar, parse
g = """
% start DP
DP[AGR=?a] -> D[AGR=?a] N[AGR=?a]
D[AGR=[NUM='sg', PERS=3]] -> 'this' | 'that'
D[AGR=[NUM='pl', PERS=3]] -> 'these' | 'those'
D[AGR=[NUM='pl', PERS=1]] -> 'we'
D[AGR=[PERS=2]] -> 'you'
N[AGR=[NUM='sg', GND='m']] -> 'boy'
N[AGR=[NUM='pl', GND='m']] -> 'boys'
N[AGR=[NUM='sg', GND='f']] -> 'girl'
N[AGR=[NUM='pl', GND='f']] -> 'girls'
N[AGR=[NUM='sg']] -> 'student'
N[AGR=[NUM='pl']] -> 'students'
"""
grammar = grammar.FeatureGrammar.fromstring(g)
tokens = 'these girls'.split()
parser = parse.FeatureEarleyChartParser(grammar)
trees = parser.parse(tokens)
for tree in trees:
tree.draw()
print(tree)似乎不重要的特征终端符号是否被引用。
发布于 2020-08-03 08:29:08
Wartena是对的:问题确实是FCFG:基于特征的上下文-免费Gramars。检查一下这个https://nltk.org/book/ch09.html,这里是他的FCFG中的一些光:
https://stackoverflow.com/questions/35963350
复制相似问题