2014-12-24 3 views
2

gensim word2vec를 사용하려고합니다. 갈색 코퍼스에 기반한 모델을 훈련 할 수 없습니다. 여기 내 코드가있다.브라운 코퍼스에서 gensim을 어떻게 훈련해야합니까?

from gensim import models 

model = models.Word2Vec([sentence for sentence in models.word2vec.BrownCorpus("E:\\nltk_data\\")],workers=4) 
model.save("E:\\data.bin") 

nltk.download()을 사용하여 nltk_data를 다운로드했습니다. 아래 오류가 나타납니다.

C:\Python27\lib\site-packages\gensim-0.10.1-py2.7.egg\gensim\models\word2vec.py:401: UserWarning: Cython compilation failed, training will be slow. Do you have Cython installed? `pip install cython` 
    warnings.warn("Cython compilation failed, training will be slow. Do you have Cython installed? `pip install cython`") 
Traceback (most recent call last): 
    File "E:\eclipse_workspace\Python_files\Test\Test.py", line 8, in <module> 
    model = models.Word2Vec([sentence for sentence in models.word2vec.BrownCorpus("E:\\nltk_data\\")],workers=4) 
    File "C:\Python27\lib\site-packages\gensim-0.10.1-py2.7.egg\gensim\models\word2vec.py", line 276, in __init__ 
    self.train(sentences) 
    File "C:\Python27\lib\site-packages\gensim-0.10.1-py2.7.egg\gensim\models\word2vec.py", line 407, in train 
    raise RuntimeError("you must first build vocabulary before training the model") 
RuntimeError: you must first build vocabulary before training the model 

내가 뭘 잘못하고 있니?

답변

8

어쩌면 문장을 잘못 작성했을 수도 있습니다.
시도해보십시오, 저에게 도움이됩니다.

import gensim 
import logging 
from nltk.corpus import brown  

logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) 
sentences = brown.sents() 
model = gensim.models.Word2Vec(sentences, min_count=1) 
model.save('/tmp/brown_model') 

로깅 부분

필요가 없습니다, 당신이 필요를 소유하고 당신은 Word2Vec()에 PARAMS을 변경할 수 있습니다.

1

nltk_data 디렉토리가 아닌 전체 디렉토리 경로가 필요합니다. 내 시스템에 그것은 다음과 같습니다

from os.path import expanduser, join 
from gensim.models.word2vec import BrownCorpus, Word2Vec 

dirname = expanduser(join('~', 'nltk_data', 'corpora', 'brown')) 
model = Word2Vec(BrownCorpus(dirname)) 

model.similar_by_word('house/nn') 

을 제공합니다 : NLTK에서 브라운 코퍼스는 POS-태그와 함께 제공

[(u'room/nn', 0.9538693428039551), (u'door/nn', 0.9475813508033752), ... 

참고. Gensim BrownCorpus 클래스는 알파벳이 아닌 토큰을 무시하지만 그렇지 않으면 POS 태그를 유지합니다. nltk.corpus.brown.sents()을 사용하면 POS 태그가없는 문장을 얻을 수 있습니다.