2017-09-27 1 views
0

다음과 같이 word2vec 모델을 구축 중입니다.왜 Gensim word2vec에서 한 글자 어휘를 구합니까?

from gensim.models import word2vec, 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, min_count=1, delimiter=b' ') 
trigram = Phrases(bigram[sentence_stream], min_count=1, delimiter=b' ') 

for sent in sentence_stream: 
    bigrams_ = bigram[sent] 
    trigrams_ = trigram[bigram[sent]] 

    print(bigrams_) 
    print(trigrams_) 


# Set values for various parameters 
num_features = 10 # Word vector dimensionality      
min_word_count = 1 # Minimum word count       
num_workers = 4  # Number of threads to run in parallel 
context = 5   # Context window size                      
downsampling = 1e-3 # Downsample setting for frequent words 


model = word2vec.Word2Vec(trigrams_, workers=num_workers, \ 
      size=num_features, min_count = min_word_count, \ 
      window = context, sample = downsampling) 

vocab = list(model.wv.vocab.keys()) 
print(vocab[:10]) 

그러나 모델의 어휘에 대한 출력은 다음과 같이 단일 문자입니다.

['h', 'u', 'm', 'a', 'n', ' ', 'c', 'o', 'p', 't'] 

bigrams와 trigrams가 올바르게 표시됩니다. 따라서 코드를 잘못 작성한 부분을 혼동합니다. 문제가 무엇인지 알려주세요.

+2

'trigrams_'는 내부 목록이 문장을 나타내는 중첩 목록이어야합니다. 여기에 문자열 목록이있는 것 같습니다. – Kasramvd

+0

아, 그거 해결 :) –

답변

0

내 문제가 해결되었습니다. 나는 목록을 word2vec 모델에 다음과 같이 전달해야한다.

trigram_sentences_project = [] 


bigram = Phrases(sentence_stream, min_count=1, delimiter=b' ') 
trigram = Phrases(bigram[sentence_stream], min_count=1, delimiter=b' ') 


for sent in sentence_stream: 
    #bigrams_ = [b for b in bigram[sent] if b.count(' ') == 1] 
    #trigrams_ = [t for t in trigram[bigram[sent]] if t.count(' ') == 2] 
    bigrams_ = bigram[sent] 
    trigrams_ = trigram[bigram[sent]] 
    trigram_sentences_project.append(trigrams_) 
관련 문제