【问题标题】:Calculate tf-idf in Gensim for my vocabulary在 Gensim 中为我的词汇量计算 tf-idf
【发布时间】:2018-03-21 05:51:27
【问题描述】:

我有一组单词(n-gram),我需要在其中计算 tf-idf 值。这些话是;

myvocabulary = ['tim tam', 'jam', 'fresh milk', 'chocolates', 'biscuit pudding']

我的语料库如下所示。

corpus = {1: "making chocolates biscuit pudding easy first get your favourite biscuit chocolates", 2: "tim tam drink new recipe that yummy and tasty more thicker than typical milkshake that uses normal chocolates", 3: "making chocolates drink different way using fresh milk egg"}

我目前正在使用 sklearn 在myvocabulary 中获取我的 n-gram 的 tf-idf 值,如下所示。

tfidf = TfidfVectorizer(vocabulary = myvocabulary, ngram_range = (1,3))
tfs = tfidf.fit_transform(corpus.values())

但是,我有兴趣在 Gensim 中做同样的事情。对于我在 Gensim 中遇到的所有示例;

  1. 仅使用一元组(我也希望它用于二元组和三元组)
  2. 计算所有单词(我只想计算myvocabulary中的单词)

因此,请帮助我了解如何在 Gensim 中完成上述两件事。

【问题讨论】:

    标签: python gensim tf-idf


    【解决方案1】:

    在gensim中,对于字典,你应该使用gensim.corpora.Dictionary类,看examples

    很遗憾,我们一般不支持 ngram,只支持带有 Phrases 类的单词的二元组

    【讨论】:

    • 你能告诉我如何使用二元组计算 tf-idf 吗?目前二元组对我来说已经足够了:)
    • 1.处理您的文本 2. 适合短语 + 将其应用于文本(使用二元组给出新文本) 3. 使用 TfidfModel 计算 tfidf
    • 这就是我的问题。如何将我要计算 tf-idf 的选定二元组输入到 tfidfmodel :D
    • 查看短语 radimrehurek.com/gensim/models/phrases.html 的示例。您将短语应用于您的数据集 -> 使用二元组获取新数据集 -> 将其用作 tfidf 的输入
    • 非常感谢 :) 我知道如何使用短语。但是计算 tf-idf 是我现在面临的问题。如果你愿意,我可以分享我的代码。请帮帮我:)
    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2016-06-29
    • 2021-01-24
    • 1970-01-01
    • 2016-08-07
    • 2023-02-07
    相关资源
    最近更新 更多