这两个使用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']

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...