nltk 句子结构分析

1. 一些语法困境:

groucho_grammar = nltk.parse_cfg("""
                                S -> NP VP
                                PP -> P NP
                                NP -> Det N | Det N PP | 'I'
                                VP -> V NP | VP PP
                                Det -> 'an' | 'my'
                                N -> 'elephant' | 'pajamas'
                                V -> 'shot'
                                P -> 'in'
                                """)
AttributeError:'nltk' has no attribute 'parse_cfg'
nltk最新的版本parse_cfg功能改为了nltk.CFG.fromstring()

AttributeError:'ChartParser' object has no attribute 'nbest_parse'
nltk最新的版本parser没有了nbest_parse()方法,可直接使用parse

from nltk import CFG
groucho_grammar = CFG.fromstring("""
                                S -> NP VP
                                PP -> P NP
                                NP -> Det N | Det N PP | 'I'
                                VP -> V NP | VP PP
                                Det -> 'an' | 'my'
                                N -> 'elephant' | 'pajamas'
                                V -> 'shot'
                                P -> 'in'
                                """)
sent = 'I shot an elephant in my pajamas'
sent = nltk.word_tokenize(sent)
parser = nltk.ChartParser(groucho_grammar)
#trees = parser.nbest_parse(sent)
# AttributeError:'ChartParser' object has no attribute 'nbest_parse'
trees = parser.parse(sent)
for tree in trees:
    print(tree)
 
 
(S
  (NP I)
  (VP
    (VP (V shot) (NP (Det an) (N elephant)))
    (PP (P in) (NP (Det my) (N pajamas)))))
(S
  (NP I)
  (VP
    (V shot)
    (NP (Det an) (N elephant) (PP (P in) (NP (Det my) (N pajamas))))))


 2. 上下文无关法 
 

grammar1 = CFG.fromstring("""
                                S -> NP VP
                                VP -> V NP | VP 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)))
编写自己的文法

#编写自己的文法
grammar1 = nltk.data.load('file:mygrammar.cfg')
rd_parser = nltk.RecursiveDescentParser(grammar1)
for tree in rd_parser.parse(sent):
    print(tree)
句法结构中的递归:如果出现在产生式的左侧的文法类型也出现在右侧,那么这个文法被认为是递归的

3. 依存关系和依存文法

groucho_dep_grammar = nltk.parse_dependency_grammar("""
                                                'shot' -> 'I' | 'elephant' | 'in'
                                                'elephant' -> 'an' | 'in'
                                                'pajamas' -> 'my'
                                                """)
pdp = nltk.ProjectiveDependencyParser(groucho_dep_grammar)
sent = 'I shot an elephant in my pajamas'.split()
for tree in pdp.parse(sent):
    print(tree)
AttributeError: module 'nltk' has no attribute 'parse_dependency_grammar'
目前还未找到解决办法




猜你喜欢

转载自blog.csdn.net/zhuzuwei/article/details/79040102