文章按关键词打分算法



我正在寻找一种算法,可以根据加权关键字为文章打分。

所以假设我有以下文章:

在石油和天然气行业日益萎缩的情况下,经济焦虑引发了对未来的棘手问题。这也在塑造一场参议院竞选,在这场竞选中,一名民主党人正在一个长期由共和党人主导的州寻求连任。

我有以下关键词,具有给定的权重(-100到100)的重要性:

  • 经济(50)
  • 参议院(70)
  • 共和党人(-100)
  • 民主党(100)

这意味着我希望一篇关于经济、参议院和民主党的文章得分高,但一篇只关于共和党人的文章得分低。一个简单的解决方案似乎只是添加文章中出现的关键字的值。但事实上,一篇文章的排名应该仍然很低,它的单词是民主党的5倍,共和党的1倍。

我的问题是:对于这个问题,是否有高效有效算法?

如果我理解得对,你可以通过注释你已经在一集中得分的单词来完成。Python中的示例:

article = """Economic anxiety amid a dwindling oil and gas industry is raising
             difficult questions about the future. It is also shaping a Senate
             race in which a Democrat is seeking re-election in a state long
             dominated by Republicans."""
keyword_score = {'economic': 50,
                 'senate': 70,
                 'republicans': -100,
                 'democrats': 100}
seen_keywords = set()
score = 0
for word in article.split():
    word = word.lower()
    if word in keyword_score and word not in seen_keywords:
        score += keyword_score[word]
        seen_keywords.add(word)
print(score)

这样一来,单词就不会得分两次。

最新更新