python自然语言处理-学习笔记(二)

2 篇文章 0 订阅
2 篇文章 0 订阅

     在第二章中,主要介绍了各个预料库的使用,这里不再赘述,对于预料库的操作,之前书中都提到过。这里只说一下一个问题,在inaugural预料库中,测试输出条件分布图的时候,他的代码里有个问题,我按照书中写的方法,得到的结果如下:

>>> cfd = nltk.ConditionalFreqDist(\
... (target, file[:4])\
... for fileid in inaugural.fileids()\
... for w in inaugural.words(fileid)\
... for target in ['america','citizen']\
... if w.lower().startswith(target))
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/usr/local/lib/python2.7/dist-packages/nltk-3.0a4-py2.7.egg/nltk/probability.py", line 1695, in __init__
    for (cond, sample) in cond_samples:
  File "<stdin>", line 6, in <genexpr>
TypeError: 'type' object has no attribute '__getitem__'
      其实,在操作之前就对函数里的首项中的file有疑问,以为可能会有默认的设置,结果出错了,在这里将file改成fileid接可以了,代码如下:

>>> cfd = nltk.ConditionalFreqDist(\
... (target, fileid[:4])\
... for fileid in inaugural.fileids()\
... for w in inaugural.words(fileid)\
... for target in ['america','citizen']\
... if w.lower().startswith(target))
>>> 
      在wordnet那一小结里面,所有的synset对象属性的调用,其实都是方法的调用(不知道这么说对不对???),也就是说书中的wn.synset('car.n.01').examples要写成wn.synset('car.n.01').examples()才对正确的,整一节都是类似的问题,有很多,在这里就不一一列举了,大家如果遇到下面这句话:TypeError: 'instancemethod' object is not iterable,就在你的变量的属性改成方法就行,也就是'.'后面的变量加‘()’让它变成方法





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值