这两个使用VADER的代码块有什么区别?

我有两个代码块,它们都使用相同的VADER函数。

VADER功能

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyser = SentimentIntensityAnalyzer()

def vader(item):
    return analyser.polarity_scores(item)['compound']

解析器的初稿,用于获取与某些属性匹配的列表的拼接。

def sentiment1(attribute):
    if attributes[0] in attribute:
        idx = attribute.index(attributes[0])
        try:
            three_before = attribute[idx-3:idx]
        except IndexError:
            three_before = attribute[:idx]
        try:    
            three_after = attribute[idx:idx+3]
        except IndexError:
            three_after = attribute[idx:]
        three_before.append(attributes[0])
        three_before.extend(three_after)
        return vader(' '.join(three_before))

第二个代码块与第一个代码块相同。

def sentiment1(item):
    if attributes[0] in item:
        att_index = item.index(attributes[0])
        lower_bound = max(att_index - 2,0)
        upper_bound = min(att_index + 2,len(item))
        text = item[lower_bound:upper_bound]
        return vader(' '.join(text))

而且,他们都在引用此归因列表。

attributes = ['head','beer','taste']

第二个定义块看似输出正确的值,而第一个定义块在映射到一系列列表时未输出。第二个定义做什么而第一个没有呢?

foxconn_fy 回答:这两个使用VADER的代码块有什么区别?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1457330.html

大家都在问