2017-03-23 2 views
0

this article을 읽은 후, 나는 내 자신의 모델을 훈련하기 시작합니다. 문제는 작성자가 Word2Vecsentences이 무엇인지 분명하게하지 않는다는 것입니다.gensim을 사용하여 Wikipedia 페이지에서 Word2Vec 모델을 교육하는 방법은 무엇입니까?

이 기록 된 바 내가 위키피디아 페이지에서 텍스트를 다운로드 기사, 그리고 나는 그것에서 문장의 목록을합니다

:

sentences = [word for word in wikipage.content.split('.')] 

따라서, 예를 들어, sentences[0]처럼 보이는

model = Word2Vec(sentences, min_count=2, size=50, window=10, workers=4) 

그러나 사전 :

'Machine learning is the subfield of computer science that gives computers the ability to learn without being explicitly programmed' 

그때 나는이 목록이있는 모델을 학습하려고 문자로 구성된 모델입니다! 예를 들어, model.wv.vocab.keys()의 출력은 다음과 같습니다.

dict_keys([',', 'q', 'D', 'B', 'p', 't', 'o', '(', ')', '0', 'V', ':', 'j', 's', 'R', '{', 'g', '-', 'y', 'c', '9', 'I', '}', '1', 'M', ';', '`', '\n', 'i', 'r', 'a', 'm', '–', 'v', 'N', 'h', '/', 'P', 'F', '8', '"', '’', 'W', 'T', 'u', 'U', '?', ' ', 'n', '2', '=', 'w', 'C', 'O', '6', '&', 'd', '4', 'S', 'J', 'E', 'b', 'L', '$', 'l', 'e', 'H', '≈', 'f', 'A', "'", 'x', '\\', 'K', 'G', '3', '%', 'k', 'z']) 

무엇이 잘못 되었나요? 미리 감사드립니다!

>>> import wikipedia 
>>> from nltk import sent_tokenize, word_tokenize 
>>> page = wikipedia.page('machine learning') 
>>> sentences = [word_tokenize(sent) for sent in sent_tokenize(page.content)] 
>>> sentences[0] 
['Machine', 'learning', 'is', 'the', 'subfield', 'of', 'computer', 'science', 'that', 'gives', 'computers', 'the', 'ability', 'to', 'learn', 'without', 'being', 'explicitly', 'programmed', '.'] 

을 그리고 그것을 먹이 :

답변

5

Word2Vec 모델 객체에 입력 nltk의 토큰 기능을 사용하여 단어의 목록의 목록이 될 수

>>> from gensim.models import Word2Vec 
>>> model = Word2Vec(sentences, min_count=2, size=50, window=10, 
>>> list(model.wv.vocab.keys())[:10] 
['sparsely', '(', 'methods', 'their', 'typically', 'information', 'assessment', 'False', 'often', 'problems'] 

그러나 일반적으로, 예를 들어, 단어의 생성자를 포함하는 생성자 (문장)는 다음과 같이 작동합니다.

>>> from gensim.utils import tokenize 
>>> paragraphs = map(tokenize, page.content.split('\n')) # paragraphs 
>>> model = Word2Vec(paragraphs, min_count=2, size=50, window=10, workers=4) 
>>> list(model.wv.vocab.keys())[:10] 
['sparsely', 'methods', 'their', 'typically', 'information', 'assessment', 'False', 'often', 'problems', 'symptoms'] 
+0

''sent_tokenize (page.content) '에 대한 이해도에서 구두점을 효과적으로 효과적으로 제거 할 수있는 방법이 있습니까? – Amir

+0

'gensim.utils.tokenize'를 사용하십시오. 'word_tokenize' 대신에. '[gensim.utils.tokenize (보낸 사람) for sent_tokenize (page.content)] ' – alvas

+0

감사합니다! 그러나, 내가 배운 것처럼,'tokenize '의 결과는'list' ('sent (send)')에''(''gensim.utils.tokenize (sent))'포장되어야합니다. – Amir

관련 문제