Python and NLTK: Jak analizować gramatykę zdań?
Mam ten kod, który powinien pokazywać syntaktyczną strukturę zdania zgodnie z określoną gramatyką. Jednak zwraca pustą []. Czego mi brakuje lub co robię źle?
import nltk
grammar = nltk.parse_cfg("""
S -> NP VP
PP -> P NP
NP -> Det N | Det N PP
VP -> V NP | VP PP
N -> 'Kim' | 'Dana' | 'everyone'
V -> 'arrived' | 'left' |'cheered'
P -> 'or' | 'and'
""")
def main():
sent = "Kim arrived or Dana left and everyone cheered".split()
parser = nltk.ChartParser(grammar)
trees = parser.nbest_parse(sent)
for tree in trees:
print tree
if __name__ == '__main__':
main()