文本处理

数据挖掘 nlp 文本 谷歌
2022-03-12 17:40:02

我是分析集群文本的新手,我正在使用 Goodreads API 来获取书籍概要。我的目标是对类似的书籍进行分组,例如:

  • 政治
  • 音乐
  • 传记等...

虽然 Goodreads 提供流派,但我想使用概要并为此使用文本。假设我会得到 N 本书的概要,如下所示:

<description>
<![CDATA[
<b>Alternate cover edition can be found <a href="https://www.goodreads.com/book/show/10249685-dune" rel="nofollow">here</a>. </b> and <a href="https://www.goodreads.com/book/show/11273438-dune" rel="nofollow">here</a><br /><br />Here is the novel that will be forever considered a triumph of the imagination. Set on the desert planet Arrakis, <b>Dune</b> is the story of the boy Paul Atreides, who would become the mysterious man known as Muad'Dib. He would avenge the traitorous plot against his noble family--and would bring to fruition humankind's most ancient and unattainable dream.<br />A stunning blend of adventure and mysticism, environmentalism and politics, Dune won the first Nebula Award, shared the Hugo Award, and formed the basis of what it undoubtedly the grandest epic in science fiction.
]]>
</description>

我读过 cosinesimilarity 和新的 google NLP但我想从这个开始:

  • 表示书籍描述(特征,通常是带有 TF-IDF 的词袋)
  • 计算两本书之间的相似度(余弦相似度)

问题:

  • 在所有书籍之间创建余弦相似度矩阵的最有效算法是什么(N)
  • 如何根据以上内容将书籍聚集在一起

任何其他想法都会很棒。

1个回答

由于您将使用 TF-IDF 表示,因此您已经有了一个特征矩阵。要计算所有向量之间的余弦相似度,您可以使用:

from sklearn.metrics.pairwise import cosine_similarity
similarity = cosine_similarity(tfidfmat)
#tfidfmat is your TF-IDF matrix

#使用numpy数组

要开始聚类,您可以使用 K-means 算法开始,并使用余弦相似度作为距离度量。这是scikit-learn 本身关于聚类文档的一个示例。

进一步尝试:如果您发现上述方法不符合您的预期,请查看 word2vec 和 doc2vec,而不是使用 tfidf,它是一种词袋方法,而是使用词向量表示。是一个很好的博客,解释了这个概念。