python - 有没有办法提高 nltk.sentiment.vader 情感分析的性能?

标签 python performance data-manipulation sentiment-analysis vader

我的文字来源于一个社交网络,所以你可以想象它的本质,我认为文字是我想象中的干净和最小的;执行以下 sanitizer 后:

  • 没有网址,没有用户名
  • 没有标点符号,没有重音符号
  • 没有数字
  • 没有停用词(我想 vader 还是这样做了)

我认为运行时是线性的,我不打算进行任何并行化,因为更改可用代码需要付出大量努力, 例如,对于大约 1000 条从 ~50 kb 到 ~150 kb 字节的文本,它需要大约

在我的机器上运行时间约为 10 分钟。

是否有更好的方法来提供算法以加快 cooking 时间? 代码与 SentimentIntensityAnalyzer 旨在工作的一样简单,这里是主要部分

sid = SentimentIntensityAnalyzer()

c.execute("select body, creation_date, group_id from posts where (substring(lower(body) from (%s))=(%s)) and language=\'en\' order by creation _ date DESC (s,s,)")
conn.commit()
if(c.rowcount>0):
                dump_fetched = c.fetchall()

textsSql=pd.DataFrame(dump_fetched,columns=['body','created_at', 'group_id'])
del dump_fetched
gc.collect()
texts = textsSql['body'].values
# here, some data manipulation: steps listed above
polarity_ = [sid.polarity_scores(s)['compound'] for s in texts]

最佳答案

/1。您不需要删除停用词,nltk+vader 已经这样做了。

/2。您无需删除标点符号,因为除了处理开销外,这也会影响 vader 的极性计算。所以,继续使用标点符号吧。

    >>> txt = "this is superb!"
    >>> s.polarity_scores(txt)
    {'neg': 0.0, 'neu': 0.313, 'pos': 0.687, 'compound': 0.6588}
    >>> txt = "this is superb"
    >>> s.polarity_scores(txt)
    {'neg': 0.0, 'neu': 0.328, 'pos': 0.672, 'compound': 0.6249}

/3.你也应该引入句子标记化,因为它会提高准确性,然后根据句子计算一个段落的平均极性。这里的例子:https://github.com/cjhutto/vaderSentiment/blob/master/vaderSentiment/vaderSentiment.py#L517

/4。极性计算彼此完全独立,可以使用 multiprocessing pool对于小尺寸,比如 10,可以很好地提高速度。

polarity_ = [sid.polarity_scores(s)['compound'] for s in texts]

关于python - 有没有办法提高 nltk.sentiment.vader 情感分析的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45296897/

相关文章:

C# 泛型类的性能

R:矩阵乘法错误(不一致的参数)

R:使用 car::recode 函数重新编码变量时出错

python - 我如何从 sqlite3_table WHERE column_name IN 变量元组中进行选择?

python - 使用 Scrapy DjangoItem 但它不会保存项目

python - 使用 xlwt 优化 xls 文件中的添加行

performance - 比较运算符性能(>、>=、<、<=)

python - vscode - 从.py文件所在的当前文件夹读取文件

javascript - 优化 jquery mobile 和 javascript

python - Dict一键对Dics列表进行分组