[英]AttributeError: 'function' object has no attribute 'lower'
I am trying to use sentiwordnet text file "SentiWordNet_3.0.0_20130122.txt". 我正在尝试使用sentiwordnet文本文件“ SentiWordNet_3.0.0_20130122.txt”。 When I am importing the sentiwordnet.py file and attempting to run it I am getting the error as follows: 当我导入sentiwordnet.py文件并尝试运行它时,出现以下错误:
The error occured as: 错误发生为:
-------------------------------------------------------------------------- AttributeError
Traceback (most recent call last)
<ipython-input-13-6e4eb476b4b2> in <module>()
----> 1 happy = cv.senti_synsets('happy', 'a')
C:\Users\Desktop\fp\sentiwordnet.pyc in senti_synsets(self, string, pos)
65 synset_list = wn.synsets(string, pos)
66 for synset in synset_list:
---> 67 sentis.append(self.senti_synset(synset.name))
68 sentis = filter(lambda x : x, sentis)
69 return sentis
C:\Users\Desktop\fp\sentiwordnet.pyc in senti_synset(self, *vals)
52 return SentiSynset(pos_score, neg_score, synset)
53 else:
---> 54 synset = wn.synset(vals[0])
55 pos = synset.pos
56 offset = synset.offset
C:\Users\Anaconda2\lib\site-packages\nltk\corpus\reader\wordnet.pyc in synset(self, name)
1227 def synset(self, name):
1228 # split name into lemma, part of speech and synset number
---> 1229 lemma, pos, synset_index_str = name.lower().rsplit('.', 2)
1230 synset_index = int(synset_index_str) - 1
1231
AttributeError: 'function' object has no attribute 'lower'
My code: 我的代码:
import sentiwordnet as snw
SWN_FILENAME = "SentiWordNet_3.0.0_20130122.txt"
cv = snw.SentiWordNetCorpusReader(SWN_FILENAME)
happy = cv.senti_synsets('happy', 'a')
print happy
Could you try out the following code: 您可以尝试以下代码:
import nltk.corpus.reader.sentiwordnet as snw
SWN_FILENAME = "SentiWordNet_3.0.0_20130122.txt"
cv = snw.SentiWordNetCorpusReader('', [SWN_FILENAME])
happy = cv.senti_synsets('happy', 'a')
print(happy)
I think the sentiwordnet.py file you're using does not match to the nltk package you have installed. 我认为您使用的sentiwordnet.py文件与您已安装的nltk软件包不匹配。 I was able to reproduce your error if I used such a mismatched pair. 如果使用了这样的不匹配对,则可以重现您的错误。 The code above uses the SentiWordNetCorpusReader which comes with the nltk package (see NLTK sources ); 上面的代码使用了nltk软件包随附的SentiWordNetCorpusReader(请参阅NLTK来源 ); however, the constructor for the reader wants as first argument a 'root' (I do not know nltk and have no clue what this might be ;-)), as the second it wants a list of file-ids (I think they mean filenames, however the code seems to be flexible enough to just accept also a filename as string). 但是,读者的构造函数希望将“ root”作为第一个参数(我不知道nltk也不知道这可能是什么;-)),而第二个则需要一个文件ID列表(我认为它们的意思是文件名,但是代码似乎足够灵活,可以只接受文件名作为字符串)。
You could also use another package version of nltk which matches your sentiwordnet.py file, I guess. 我猜您也可以使用与您的sentiwordnet.py文件匹配的另一个nltk软件包版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.