【问题标题】:Create bigrams using NLTK from a corpus with multiple lines使用多行语料库中的 NLTK 创建二元组
【发布时间】:2016-08-11 21:14:25
【问题描述】:

我正在尝试从具有多行的语料库中生成二元组。 Bigrams 是跨换行符创建的,这是一个问题,因为每一行都代表它自己的上下文,并且与后续行无关。这会导致语义不正确的二元组。

语料库

Reeves Acrylfarbe 75Ml Ultramarin 
Acrylfarbe Deep Peach 
Reeves Acrylfarbe 75Ml Grasgrün 
Acrylfarbe Antique Go 

有问题的二元组示例

'Ultramarin Acrylfarbe'、'Grasgrün Acrylfarbe'

这是我正在使用的代码:

finder = BigramCollocationFinder.from_words(word_tokenize(corpus))
bigrams = finder.nbest(bigram_measures.likelihood_ratio, 100)

如何省略跨越两行的二元组?

【问题讨论】:

    标签: python nltk


    【解决方案1】:

    我相信这样的事情应该可行:

    finder = nltk.BigramCollocationFinder.from_documents([
        nltk.word_tokenize(x) for x in corpus.split('\n')])
    bigrams = finder.nbest(bigram_measures.likelihood_ratio, 100)
    

    【讨论】:

      【解决方案2】:

      我会在 '\n' 上使用split 来获取行列表,然后分别处理每一行并合并二元组列表

      【讨论】:

      • 有同样的想法,但我不知道如何使用返回 nltk.collocations.BigramCollocationFinder 对象的 BigramCollocationFinder 来做到这一点
      • 我假设您的bigram_measuresnltk.collocations.BigramAssocMeasures 的一个实例。 bigrams 只是一个元组/对的列表。因此,您对其进行初始化,然后使用 extend 将每一行中的新二元组添加到其中
      猜你喜欢
      • 2015-07-10
      • 2011-05-15
      • 1970-01-01
      • 2015-08-02
      • 2011-06-27
      • 2016-03-17
      • 2017-03-09
      相关资源
      最近更新 更多