0

我正在尝试从同一组 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
4

3 回答 3

3

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

里面的数字12num_features=12从哪里来的?它应该num_features=len(dictionary)用于 BOW 向量或num_features=lsi.num_topicsLSI 向量。

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

查看http://radimrehurek.com/gensim/tutorial.html上的 gensim 教程,它更详细地介绍了这些步骤并附有评论。

于 2014-03-11T23:06:00.880 回答
0

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

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

因此,您可以减少不相关的条款。

于 2015-10-16T07:03:51.260 回答
-1

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

于 2014-04-28T02:25:11.553 回答