2017-11-15 21 views
1

genf를 사용하여 다음과 같이 tf-idf 값을 계산합니다.python에서 gensim의 tf-idf 값을 얻는 방법

texts = [['human', 'interface', 'computer'], 
['survey', 'user', 'computer', 'system', 'response', 'time'], 
['eps', 'user', 'interface', 'system'], 
['system', 'human', 'system', 'eps'], 
['user', 'response', 'time'], 
['trees'], 
['graph', 'trees'], 
['graph', 'minors', 'trees'], 
['graph', 'minors', 'survey']] 

dictionary = corpora.Dictionary(texts) 
corpus = [dictionary.doc2bow(text) for text in texts] 
tfidf = models.TfidfModel(corpus) 

이제 tf-idf 값이 가장 높은 3 단어를 얻고 싶습니다. 제발 도와주세요!

답변

0

조금만 검색하면 원하는대로 보입니다. 가장 읽을 수있는 것은 아니지만 작동 할 수도 있습니다.

top_3 = [t[0] for t in 
     sorted([(word, i, j) for j, text in enumerate(texts) for i, word in enumerate(text)], 
       key=lambda t: tfidf[t[2]][t[1]])[:3]] 

는 I는 텍스트에서 단어를 받아 그 열을 추적 (I로) 폼 (word, i, j) 터플와 (j)를 열. 그런 다음 해당 단어의 값을 기준으로 단어를 정렬합니다 (tfidf). 그런 다음 상위 3 개를 사용하여 ([:3]) t[0] for t in ...으로 터플에서 단어를 가져옵니다.

이것은 임의의 수의 단어를 순서대로 저장하도록 쉽게 수정할 수 있습니다.