簡體   English   中英

向語法規則添加連詞-NLTK解析為python中的語法樹

[英]Add conjunction to grammar rule - NLTK parse into syntax tree in python

假設我們有以下不完整的語法規則:

grammar2 = nltk.parse_cfg("""
S -> NP VP
NP -> Det N
VP -> V NP
PN -> 'David' 
Det -> 'the'
N -> 'man' 
V -> 'saw' | 'helped' 
Pro -> 'him'
Conj -> 'and'
""")

我想為以下句子創建語法樹:

sent = ['the', 'man', 'saw', 'David', 'and', 'helped', 'him']
parser = nltk.ChartParser(grammar2)
trees = parser.nbest_parse(sent)
for tree in trees:
    print tree

但是我不知道如何添加連詞and語法?

遞歸工作。 這應該工作。

S -> NP VP 
VP -> V NP | V NP Conj VP
NP -> Det N | PN | Pro
PN -> 'David' 
Det -> 'the'
N -> 'man' 
V -> 'saw' | 'helped' 
Pro -> 'him'
Conj -> 'and'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM