【问题标题】:Document Similarity Gensim文档相似度 Gensim
【发布时间】:2014-03-09 14:25:43
【问题描述】:

我正在尝试从同一组 10,000 个文档中获取 10,000 个文档列表的相关文档。我正在使用两种算法进行测试:gensim lsi 和 gensim 相似度。两者都给出了可怕的结果。我该如何改进它?

from gensim import corpora, models, similarities
from nltk.corpus import stopwords
import re

def cleanword(word):
    return re.sub(r'\W+', '', word).strip()

def create_corpus(documents):

    # remove common words and tokenize
    stoplist = stopwords.words('english')
    stoplist.append('')
    texts = [[cleanword(word) for word in document.lower().split() if cleanword(word) not in stoplist]
             for document in documents]

    # remove words that appear only once
    all_tokens = sum(texts, [])
    tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1)

    texts = [[word for word in text if word not in tokens_once] for text in texts]

    dictionary = corpora.Dictionary(texts)
    corp = [dictionary.doc2bow(text) for text in texts]

def create_lsi(documents):

    corp = create_corpus(documents)
    # extract 400 LSI topics; use the default one-pass algorithm
    lsi = models.lsimodel.LsiModel(corpus=corp, id2word=dictionary, num_topics=400)
    # print the most contributing words (both positively and negatively) for each of the first ten topics
    lsi.print_topics(10)

def create_sim_index(documents):
    corp = create_corpus(documents)
    index = similarities.Similarity('/tmp/tst', corp, num_features=12)
    return index

【问题讨论】:

  • 首先,您不能对纯粹的无监督统计方法(例如 LSI 或 LDA)抱有太多期望。尝试tf-idf、余弦相似度、更强的停用词列表、其他聚类方法(例如k-means)
  • 不,方法很好。这只是复制粘贴代码的热点问题@alvas :)
  • @Radim 可以与 Solr/ElasticSearch 一起使用 gensim 吗?

标签: python nlp gensim


【解决方案1】:

您似乎根本没有使用create_lsi()?您只需打印创建的 LSI 模型,然后忘记它。

num_features=12 中的数字 12 是从哪里来的?对于 BOW 向量应该是 num_features=len(dictionary),对于 LSI 向量应该是 num_features=lsi.num_topics

在 LSI 之前添加 TF-IDF 转换。

查看http://radimrehurek.com/gensim/tutorial.html 的 gensim 教程,它更详细地介绍了这些步骤并使用 cmets。

【讨论】:

    【解决方案2】:

    LSI 用于大型文本数据语料库。我们可以使用奇异值分解在缩减空间中形成具有相关项的矩阵。在 gensim 包中,您可以通过仅返回前 n 个词条来获取语义相似的词条。

    lsimodel.print_topic(10, topn=5) 其中 10 是主题数,5 表示每个主题的前五个术语。

    因此您可以减少不相关的术语。

    【讨论】:

      【解决方案3】:

      您需要使用其他机器学习算法,例如:具有余弦相似度的聚类(k-means)

      【讨论】:

        猜你喜欢
        • 2018-02-19
        • 2014-04-21
        • 2015-10-10
        • 2014-02-25
        • 2019-07-02
        • 2015-07-02
        • 1970-01-01
        • 1970-01-01
        • 2020-07-30
        相关资源
        最近更新 更多