NLTK和PYTHON GRAMMAR

时间:2017-12-06 20:38:39

标签: python nltk context-free-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'

1 个答案:

答案 0 :(得分:0)

您可以从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)))