NLTK 和 PYTHON 语法

NLTK and PYTHON GRAMMAR

我的系统上已经安装了 python 2.7 和 nltk 求求你了,我怎样才能让我的 python 程序使用下面的 cfg 将词性附加到我的句子中??

N -> 'ọnẹkẹlẹ'|'igbẹlẹ'|'ọma-ọnẹkẹlẹ' 
Pr -> 'Omi'|'uwẹ'|'awa'|'ama'|'oñwu'|'I'
Dart -> 'lẹ'
Adj -> 'kẹkẹ'|'nya'|'kpa' 
Adv -> 'ọgboolo'|'nyọnyọ'|'lile'
Ord -> 'ejodudu'|'ẹkeji'|'ẹkẹta'
Card -> 'ka'|'meji'|'mẹta' 
Quant -> 'wewe'|'gwẹẹ'|'Uchẹkibọ'|'uchẹkibọ'
Dem -> 'dẹi'|'i'|'lẹi'
Poss -> 'mi'|'wa'|'wẹ'|'ñw'|'ma'

您可以从 NLTK book, chapter 8 查看此示例代码。

import nltk

grammar1 = nltk.CFG.fromstring("""
S -> NP VP
VP -> V NP | V NP PP
PP -> P NP
V -> "saw" | "ate" | "walked"
NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
Det -> "a" | "an" | "the" | "my"
N -> "man" | "dog" | "cat" | "telescope" | "park"
P -> "in" | "on" | "by" | "with"
""")

sent = "Mary saw Bob".split()
rd_parser = nltk.RecursiveDescentParser(grammar1)
for tree in rd_parser.parse(sent):
    print(tree)

输出:

(S (NP Mary) (VP (V saw) (NP Bob)))