我正在学习NLTK(使用Python),并且具有以下小语法:
nouns_grammar_a ="""
%start Statement[number=?n]
Statement[number=?n] -> NounPhrase[number=?n]
# simple nouns:
Noun[number = singular, type = noun] -> 'elephant' | 'pajamas' |'dog' |'man'|'park' |'gun' | 'everything'
Noun[number = plural, type = noun] -> 'elephants' | 'pajamas' |'dogs' |'men'|'parks' |'guns'
Noun[number = singular, type = name] -> "John" | "Mary" | "Bob" | "Adam"
#noun phrases:
#NounPhrase[number =?n] -> Determiner[number =?n] Noun[number =?n]
# plural without an article:
NounPhrase[number = plural] -> Noun[number =plural, type = noun]
NounPhrase[number =?n] -> Pronoun[number =?n, form = nominative]
NounPhrase[number =?n] -> Pronoun[number =?n, form = demonstracive]
NounPhrase[number =?n] -> Noun[number = ?n, type = name]
"""
然后是代码:
my_grammar = nltk.grammar.FeatureGrammar.fromstring(nouns_grammar_a)
parser = nltk.ChartParser(my_grammar)
#text=' Hello Adam, how are you? I hope everything is going well. today is a good day, see you dude.'
text =' Adam'
tokens = nltk.tokenize.word_tokenize(text)
for tree in parser.parse(tokens):
print(tree)
我的输出错误:
(Statement[number=?n]
(NounPhrase[number=?n] (Noun[number='singular', type='name'] Adam)))
(Statement[number=?n]
(NounPhrase[number='plural']
(Noun[number='singular', type='name'] Adam)))
看起来功能语法的主要思想在这里不起作用-请参阅number =?n值。它们在规则的两边都没有相等,第二棵树来自对复数名词应用规则,这不可能是亚当名词是单数的原因。