在word2vec Gensim中获取bigrams和trigrams

17

我目前在我的word2vec模型中使用的是单个字词(unigrams)如下所示。

def review_to_sentences( review, tokenizer, remove_stopwords=False ):
    #Returns a list of sentences, where each sentence is a list of words
    #
    #NLTK tokenizer to split the paragraph into sentences
    raw_sentences = tokenizer.tokenize(review.strip())

    sentences = []
    for raw_sentence in raw_sentences:
        # If a sentence is empty, skip it
        if len(raw_sentence) > 0:
            # Otherwise, call review_to_wordlist to get a list of words
            sentences.append( review_to_wordlist( raw_sentence, \
              remove_stopwords ))
    #
    # Return the list of sentences (each sentence is a list of words,
    # so this returns a list of lists
    return sentences

但是,这样会错过数据集中重要的二元组和三元组。

E.g.,
"team work" -> I am currently getting it as "team", "work"
"New York" -> I am currently getting it as "New", "York"
因此,我想捕捉我的数据集中的重要二元组、三元组等,并将其输入到我的word2vec模型中。 我对wordvec很陌生,并且正在努力学习如何操作。请帮帮我。

1
提供一些代码和更好的示例。您展示的示例并不反映您在第一行中提供的数据。 - AK47
1
完成了!已更新问题。请帮我解决这个问题。 - user8566323
3个回答

23

首先,您应该使用gensim类Phrases来获取成对出现的词语(bigrams),其工作方式如文档中所述。

>>> bigram = Phraser(phrases)
>>> sent = [u'the', u'mayor', u'of', u'new', u'york', u'was', u'there']
>>> print(bigram[sent])
[u'the', u'mayor', u'of', u'new_york', u'was', u'there']

要获取三元组等内容,您应该使用已有的二元模型并再次应用Phrases等技术。例如:

trigram_model = Phrases(bigram_sentences)

此外,有一本很好的笔记本和视频,介绍了如何使用它... 笔记本视频。其中最重要的部分是如何在实际句子中使用它,具体如下:
// to create the bigrams
bigram_model = Phrases(unigram_sentences)

// apply the trained model to a sentence
 for unigram_sentence in unigram_sentences:                
            bigram_sentence = u' '.join(bigram_model[unigram_sentence])

// get a trigram model out of the bigram
trigram_model = Phrases(bigram_sentences)

希望这对你有所帮助,但下次请提供更多关于你使用的信息等。
附注:现在你已经编辑了它,但你只是将其拆分而没有采用短语的方式来获取像“纽约”这样的双词组。

谢谢您宝贵的回答。但是当我使用bigram = Phraser(phrases)时,它会显示未定义名称Phraser和phrases。我需要导入它们吗? - user8566323
4
@Volka,是的,你需要导入它们,这些模型在gensim中,我知道gensim的文档有时很令人困惑。 - nitheism
@nitheism 请告诉我您是否知道这个问题的答案 https://dev59.com/vaXja4cB1Zd3GeqPLAva - user8510273
通常在创建n-gram字典后,删除停用词和词干处理是很好的做法。 - Amir Imani

12
from gensim.models import Phrases

from gensim.models.phrases import Phraser

documents = [
  "the mayor of new york was there", 
  "machine learning can be useful sometimes",
  "new york mayor was present"
  ]

sentence_stream = [doc.split(" ") for doc in documents]
print(sentence_stream)

bigram = Phrases(sentence_stream, min_count=1, threshold=2, delimiter=b' ')

bigram_phraser = Phraser(bigram)


print(bigram_phraser)

for sent in sentence_stream:
    tokens_ = bigram_phraser[sent]

    print(tokens_)

@user8566323 你需要导入以下内容: from gensim.models import Phrases from gensim.models.phrases import Phraser - BrB
1
了解Phrases和Phraser的输出以及bigram和bigram_phraser的外观会很不错。 Word2Vec使用sg = 1,skip gram = 1进行负采样和窗口处理,如何处理? - devssh
当我运行您上面的代码时,它能够正常运行直到print(sentence_stream),但是当我执行bigram = Phrases(sentence_stream, min_count=1, threshold=2, delimiter=b' ')时,出现以下错误:TypeError:序列中的第0项:预期为类似字节的对象,但发现了str。 - seakyourpeak
1
@seakyourpeak:那不是我的代码(我只是修正了一个拼写错误),但我记得你可以用简单的' '替换b''分隔符。我不确定这段代码是否适用于Python 2.x,所以我没有改动它... - tgrandje
我用___替换了分隔符,然后它就可以工作了。基本上,分隔符打开使用提供的分隔符将单词连接起来形成短语(例如bigram或trigram)。 - seakyourpeak

3

你应该寻找Phrases和Phraser这两个词

bigram = gensim.models.Phrases(data_words, min_count=1, threshold=10) # higher threshold fewer phrases.
trigram = gensim.models.Phrases(bigram[data_words], threshold=100) 

一旦您完成添加词汇的工作,就可以使用Phraser来实现更快的访问和更有效的内存使用。这不是强制性的,但很有用。

bigram_mod = gensim.models.phrases.Phraser(bigram)
trigram_mod = gensim.models.phrases.Phraser(trigram)

感谢您。

如何在训练和测试数据中使用它?我想使用训练数据来学习短语,然后将其转换为测试数据。我该怎么做? - user_12

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接