1

我尝试使用此代码来计算使用 sentiword 的情绪,我尝试了标记化和 pos 标记它可以工作,但是当我尝试这个时,我收到错误消息

print("Array..............\n\n")
tagged=np.array(df['tagged_texts'])
print(tagged)
pos=neg=obj=count=0
for word, tag in tagged:
    ss_set = None
    if 'NN' in tag and swn.senti_synsets(word):
        ss_set = list(swn.senti_synsets(word))[0]
    elif 'VB' in tag and swn.senti_synsets(word):
        ss_set = list(swn.senti_synsets(word))[0]
    elif 'JJ' in tag and swn.senti_synsets(word):
         ss_set = list(swn.senti_synsets(word))[0]
    elif 'RB' in tag and swn.senti_synsets(word):
         ss_set = list(swn.senti_synsets(word))[0]
    if ss_set:
        pos=pos+synset.pos_score()
        neg=neg+synset.neg_score()
        obj=obj+synset.obj_score()
        count+=1

我收到错误

    Array..............


[list([('no', 'DT'), ('coment', 'NN')])
 list([('fast', 'RB'), ('respon', 'NN')]) list([('giood', 'NN')]) ...
 list([('excelent', 'NN')]) list([('givemore', 'NN'), ('promo', 'NN')])
 list([('thankss', 'NN'), ('gojekkk', 'NN')])]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-45-bb3df6752c85> in <module>
      5 for word, tag in tagged:
      6     ss_set = None
----> 7     if 'NN' in tag and swn.senti_synsets(word):
      8         ss_set = list(swn.senti_synsets(word))[0]
      9     elif 'VB' in tag and swn.senti_synsets(word):

~\Anaconda3\lib\site-packages\nltk\corpus\reader\sentiwordnet.py in senti_synsets(self, string, pos)
     94 
     95         sentis = []
---> 96         synset_list = wn.synsets(string, pos)
     97         for synset in synset_list:
     98             sentis.append(self.senti_synset(synset.name()))

~\Anaconda3\lib\site-packages\nltk\corpus\reader\wordnet.py in synsets(self, lemma, pos, lang, check_exceptions)
   1573         of that language will be returned.
   1574         """
-> 1575         lemma = lemma.lower()
   1576 
   1577         if lang == 'eng':

AttributeError: 'tuple' 对象没有属性 'lower'

谁能帮我理解这段代码哪里出错了?谢谢你

4

1 回答 1

1

tagged 以元组对的列表开始。所以,单词和标签都是元组。例如 word 和 tag 的第一个值将是

word : ('no', 'DT') 
tag : ('coment', 'NN')

也许,尝试:

temp = []
    for x in tagged: 
        for y in x: 
            temp.append(y) 
tagged = temp

在运行循环之前 - 假设您要遍历主循环中的元组。

于 2020-05-05T17:46:06.257 回答