【问题标题】:Classification based on List of Words Python基于 Python 的词表分类
【发布时间】:2014-08-03 08:32:06
【问题描述】:

我想对文本进行分类:正面、负面或中性。我有一个正面和负面的单词列表。我想用 Python 来做。这里我用这种代码来描述

如果在 positive_words 中找到文本中的单词,则 counterpos = pos_count[i] += 1

如果文本中的单词在negative_words中找到然后 counterneg = neg_count[i] += 1

totalcount = pos_count + neg_count

if(len(totalcount) > 0): 存储在正数据库中 elif (len(totalcount)

store in neutral database

这是一般的想法,我的编码能力是空的。 我存储在 mongodb 中,所以我存储的部分没有问题。我仍然无法进行分类。 有人可以帮帮我吗。

【问题讨论】:

标签: python mongodb classification words


【解决方案1】:

除了字符串比较和控制流语句之外,list comprehension 在这里也很有用。

text = "seeking help on possible homework task"
raw_words = text.split(" ")

positive_words = ['seeking','help']
negative_words = ['homework']

positive_score = len([word for word in raw_words if word in positive_words])
negative_score = len([word for word in raw_words if word in negative_words])

total_score = positive_score - negative_score

这将导致total_score 的值为1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2018-02-16
    • 2021-03-09
    • 2018-01-07
    • 2014-09-19
    • 1970-01-01
    • 2014-10-03
    相关资源
    最近更新 更多