【问题标题】:How to detect if 2 news articles have the same topic? (Python language-comparison)如何检测两篇新闻文章是否具有相同的主题? (Python语言比较)
【发布时间】:2010-04-05 18:52:38
【问题描述】:

我正在寻找有关推荐方法的想法。

我正在尝试从一些特定网站的文章中抓取一些标题和正文,类似于 Google 对 Google 新闻所做的操作。

问题出在不同的网站上,他们可能有相同主题的文章,措辞略有不同。

谁能指出我需要知道什么才能编写一个比较算法来自动检测相似的文章?目前是否有任何库可以用于文本比较并返回某种类型的相似度评级?

非常感谢。

我使用 Python。

【问题讨论】:

标签: python nlp comparison


【解决方案1】:

我认为最简单的方法是使用 HuggingFace 库中的 SentenceSimilarity 模型,例如 using this model

首先你必须

pip install sentence_transformers

那么代码就很简单了,你可以在提供的链接中看到:

from sentence_transformers import SentenceTransformer
import numpy as np

sentences = ["Text number 1", "Text number 2"]
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v1')
embeddings = model.encode(sentences)
np.dot(embeddings[0], embeddings[1], out=None)

点积的结果将是两个字符串之间的相似度得分。基本上,1 表示它们是相同的,-1 表示它们是相反的(更多细节请看here)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2016-09-15
    相关资源
    最近更新 更多