【问题标题】:print bigrams learned with gensim使用 gensim 学习的打印二元组
【发布时间】:2018-12-09 16:36:00
【问题描述】:

我想使用 gensim 从语料库中学习二元组,然后打印所学的二元组。我还没有看到这样做的例子。 帮助表示赞赏

from gensim.models import Phrases
documents = ["the mayor of new york was there", "human computer interaction and machine learning has now become a trending research area","human computer interaction is interesting","human computer interaction is a pretty interesting subject", "human computer interaction is a great and new subject", "machine learning can be useful sometimes","new york mayor was present", "I love machine learning because it is a new subject area", "human computer interaction helps people to get user friendly applications"]
sentence_stream = [doc.split(" ") for doc in documents]

bigram = Phrases(sentence_stream)

# how can I print all bigrams learned and just the bigrams, including "new_york" and "human computer" ?enter code here

【问题讨论】:

    标签: python gensim n-gram topic-modeling phrase


    【解决方案1】:

    如果您使用前面提到的 Phrases 类训练模型并在不保留模型的情况下打印二元组,则 OP 的答案将起作用。当您保存模型然后在将来再次加载它时,它将不起作用。保存后加载模型时,需要使用Phraser类,如下所示:

    from gensim.models.phrases import Phraser
    

    然后加载模型:

    bigram_model = Phraser.load('../../whatever_bigram_model')
    

    然后,如果您确实使用以下方法作为提到的 OP 的答案,即

    OP 的回答

    import operator
    sorted(
        {k:v for k,v in bigram_model.vocab.items() if b'_' in k if v>=bigram_model.min_count}.items(),
        key=operator.itemgetter(1),
        reverse=True)
    

    您将收到一条错误消息:

    AttributeError: 'Phraser' object has no attribute 'vocab'
    

    解决方案

    解决方法如下:

    for bigram in bigram_model.phrasegrams.keys():
        print(bigram)
    

    输出:

    (b'word1', b'word2')
    (b'word3', b'word4')
    

    此解决方案适用于两种情况,对于持久和非持久模型,在 OP 给出的示例中,我的解决方案的修改版本是:

    for ngrams, _ in bigram.vocab.items():
        unicode_ngrams = ngrams.decode('utf-8')
        if '_' in unicode_ngrams:
            print(unicode_ngrams)
    

    给予:

    the_mayor
    mayor_of
    of_new
    new_york
    york_was
    was_there
    human_computer
    computer_interaction
    interaction_and
    and_machine
    machine_learning
    learning_has
    has_now
    now_become
    

    输出中还有更多内容,但我将其截断,以保证答案的长度

    我希望我的回答有助于增加清晰度。

    【讨论】:

      【解决方案2】:
      import operator
      sorted(
          {k:v for k,v in bigram.vocab.items() if b'_' in k if v>=bigram.min_count}.items(),
          key=operator.itemgetter(1),
          reverse=True)
      

      【讨论】:

      • 我相信这可能会打印出所有观察到的出现次数超过min_count 次的二元组,不一定是那些将被组合成短语的二元组。看看export_phrases() 方法(github.com/RaRe-Technologies/gensim/blob/…),尤其是它如何与pseudocorpus() 内的pseudocorpus() 一起使用(github.com/RaRe-Technologies/gensim/blob/…)。这可以只表达在正常操作中创建的二元组。
      • @gojomo 我发现伪语料令人困惑,你能发布更完整的答案吗?
      • 您可以只运行链接的确切行 - for bigram, score in phrases_model.export_phrases(corpus, self.delimiter, as_tuples=True) - 循环遍历将从语料库提升为短语的双元组。您可以使用原始语料库。你可以使用.pseudocorpus() 从所有看到的二元组中制作一个假的语料库——从而表达模型可能提供的所有短语。如果您需要更多指导,请描述您尝试过的方法、看到的结果以及与您的预期有何不同。
      • @gojomo 让我感到困惑的是对任何语料库的需求根本。也就是说,语料库已被用于学习二元组。现在我想提取那些学到的二元组。为什么我需要另一个语料库来做到这一点?
      • Phrases 类仅对语料库进行一次遍历,编译潜在短语组合的统计数据。然后,每当出现新文本时,它就可以组合二元组了。 (它甚至不维护仅包含“可组合”二元组的紧凑列表,因为以后可以调整threshold 并更改混合。)如果您想要一个实际二元组的列表,在当前参数下,您需要迭代结束并测试所有可能。一种方法是重复原始训练语料库。另一种方法是创建一个包含所有可能的假的类似语料库的列表(又名pseudocorpus())。
      猜你喜欢
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 2022-06-15
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      相关资源
      最近更新 更多