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'

目前还未找到解决办法



 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值