nltk CFG,定义语法规则

时间:2018-02-27 21:05:01

标签: nlp nltk context-free-grammar

已编辑我需要解析以下句子:“列出飞往丹佛的航班座位”。下面的代码应该工作,但不是。问题在于nltk.CFG.fromstring命令,我相信其他功能都有效。有什么想法如何在这里正确定义语法规则?

import nltk
from nltk.corpus import treebank



# here we define a grammar


import nltk
from nltk.corpus import treebank

grammar = nltk.CFG.fromstring("""

S -> NP VP | VP |IVP

IVP -> IV NP NP PP | IV NP NP

NP -> NNS | Det Nom | 'Denver'

Nom -> Nom N | Nom PP | N

VP -> V NP PP | V PP  | V NP | V | TO VP

PP -> IN NP | PRP NP


Det ->'the'
IN -> 'on'
TO -> 'to'
PRP ->'me'
N ->'flight'
NNS -> 'seats'
V ->  'List'
 """)



# here we let nltk construct a chart parser from our grammar
parser = nltk.ChartParser(grammar)

# input: a list of words
# returns all the parses of a sentence
def allParses(sentenceList):
    return parser.parse(sentenceList)

# input: a list of parse trees
# prints all the parse trees
def printParses(parses):
    for tree in parses:
        print(tree)

# input: a sentence as a string or as a list of words
# prints a sentence, then parses it and prints all the parse trees
def processSentence(sentence):
    sentenceList = sentence
    if isinstance(sentence,str):
        sentenceList = sentence.split(' ')
    print('Original sentence: ' + ' '.join(sentenceList))
    printParses(allParses(sentenceList))

def mainScript():
    processSentence('List me the seats on the flight to Denver')

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

"列表"也可以是名词:N - > '列表'
也许还在箭头和单词之间加上空格:
N - >'航班'应该是N - > '飞行' | '列表'
"座位"也可以是一个动词。例如:"她座位"或者"他座位"
规则也不完整。例如,IV没有规则 - > ??