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

来源:互联网 发布:小米4支持4g网络吗 编辑:程序博客网 时间:2024/05/22 10:41

     在第二章中,主要介绍了各个预料库的使用,这里不再赘述,对于预料库的操作,之前书中都提到过。这里只说一下一个问题,在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,就在你的变量的属性改成方法就行,也就是'.'后面的变量加‘()’让它变成方法





0 0