python自然语言处理第八章

1.使用符合语句规则的子串表接收器。
 

def init_wfst(tokens,grammar):
numtokens=len(tokens)
wwfst=[[None for i in range(numtokens+1)] for j in range(numtokens+1)]
for i in range(numtokens):
productions=grammar.productions(rhs=tokens[i])
wfst[i][i+1]=productions[0].lhs()
return wfst

def comoplete_wfst(wfst,tokens,grammar,trace=False):
index=dict((p.rhs(),p.lhs() )for p in grammar.productions())
numtokens=len(tokens)
for span in range(2,numtokens+1):
for start in range(numtokens+1-span):
end=start+span
for mid in range(start+1,end):
nt1,nt2=wfst[start][mid],wfst[mid][end]
if nt1 and nt2 and (nt1,nt2) in index:
wfst[start][end]=index[(nt1,nt2)]
if trace:
print "[%s] %3s [%s] %3s [%s]==> [%s] %3s [%s]" %\
(start,nt1,nt2,end start,index[(nt1,nt2)] end)
return wfst

def display(wfst,tokens):
print '\n WFST'+' '.join([("%-4d"% i) for i in range(1,len(wfst))])
for i in range(len(wfst)-1):
print "%d " %i,
for j in range(1,len(wffst)):
print "%-4s" % (wfst[i][j] or '.')
print

tokens="I shoot an elephant in my pajamas".split()
wfst0=init_wfst(tokens,groucho_grammar)
display(wfst0,tokens)

2.搜索树库找出句子的补语。
 

def filter(tree):
child_nodes=[child.node for child in tree
if isinstance(child,nltk.Tree)]
return (tree.node=='VP') and ('S' in child_nodes)

from nltk.corpus import treebank
[subtree for tree in treebank.parsed_sents()
for subtree in tree.subtrees(fileter)]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值