【问题标题】:AttributeError: 'tuple' object has no attribute 'lower' sentiwordAttributeError: 'tuple' 对象没有属性 'lower' sentiword
【发布时间】:2020-05-05 17:29:32
【问题描述】:

我尝试使用此代码使用 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'

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

【问题讨论】:

    标签: python senti-wordnet


    【解决方案1】:

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

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

    也许,试试:

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

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

    【讨论】:

    • 感谢您向我解释,元组错误很清楚,但是我在if 'NN'... 行出现错误,错误是ValueError: not enough values to unpack (expected 2, got 1) 代码真的错了吗?
    • 这来自您的 tagged 列表 - 子列表的大小不一致。我添加的最后一段代码应该放在tagged=np.array(df['tagged_texts']) 之后
    猜你喜欢
    • 2020-10-06
    • 2016-04-15
    • 2016-01-30
    • 2020-12-08
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    相关资源
    最近更新 更多