2017-09-09 7 views
1

私は現在、word2vecモデルで次のようにユニグラムを使用しています。word2vecのbigramsとtrigramsを取得するGensim

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 

しかし、私は重要なbigramsとtrigramsを私のデータセットで見逃してしまいます。

E.g., 
"team work" -> I am currently getting it as "team", "work" 
"New York" -> I am currently getting it as "New", "York" 

したがって、重要なバイグラムやトリグラムなどをデータセットに取り込んで、word2vecモデルに入力したいと考えています。

私はwordvecの新機能であり、それを行う方法に苦労しています。私を助けてください。

+0

いくつかのコードとより良い例を示します。表示している例では、最初の行に入力したデータは反映されません – AK47

+0

完了!質問が更新されました。この問題を解決するために私を助けてください。 –

答えて

2

ドキュメント

>>> 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'] 

で指摘したようにトライグラムを得るためにというように、あなたが既に持っているバイグラムモデルを使用する必要があります動作しますあなたがバイグラムを得るためにgensimのクラスPhrasesを使用する必要がありますまず第一に、フレーズをもう一度適用するなどの操作を行います。 例:

trigram_model = Phrases(bigram_sentences) 

はまた、それを使用する方法について説明し良いノートパソコンやビデオがある.... the notebookthe video

それの最も重要な部分は、実際の生活の文章でそれを使用する方法でありますこれは、次のとおりです。

// 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) 

希望

これはあなたを助けますが、次回は私たちにあなたが使用しているかについてより多くの情報を与え、など

P.S:これで編集したので、バイグラムを分割するために何もしていないので、ニューヨークのような言葉をバイグラムとして得るためにフレーズを使用する必要があります。

+0

貴重なご回答ありがとうございます。しかし、私はbigram = Phraser(フレーズ)を使用します。それは未定義の名前フレーザーとフレーズを言う。インポートする必要はありますか? –

+1

@Volkaはい、あなたはそれらをインポートする必要があります、それはgensimのモデルにあります、私はgensimドキュメントが時々混乱していることを知っています – nitheism

+0

@nitheism https://stackoverflow.com/questions/46137572の答えを知っていれば教えてください/エラーを抽出するフレーズ使用-gensim –

-1
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_) 
+0

@ user8566323以下からインポートする必要があります gensim.modelsからインポートフレーズ gensim.models.phrasesからインポートフレーザー – brb

関連する問題