프로그래밍/자연어처리

한국어 뉴스 데이터로 딥러닝 시작하기- 6. doc2vec으로 문사 유사도 측정하기

kugancity 2017. 9. 11. 18:05
반응형






한국어 뉴스 데이터로 딥러닝 시작하기 



6. doc2vec으로 문사 유사도 측정하기 



과거 관련 포스팅 리스트 


1. 한국어 위키 덤프 다운로드 받기 바로가기  

2. 위키 덤프 데이터 파싱하기 바로가기

3. 위키 데이터 한국어 형태소 태깅하기 바로가기

4. doc2vec 모델 훈련하기

5. 한국어 word2vec 데모 만들기



최근에 관심이 있는 프로젝트에서 

부동산 관련 뉴스들만 자동으로 필터링 해야 하는데  

doc2vec에서 있는 n_similarity 함수 기능이 생각나서 간만에 

doc2vec API 문서 로 가보았습니다. 






 


???? 

Deprecated 되고 다른 모듈로 기능이 옮겨졌네요 ㅠㅠ

 넘 간만에 doc2vec을 사용했더니 ㄷㄷ


새로운 함수 설명으로 이동해봅니다. 




https://github.com/RaRe-Technologies/gensim/blob/master/gensim/test/test_doc2vec.py



관련 질의 에서 가져온 자세한 함수 설명 은 아래와 같습니다. 


def n_similarity(self, ds1, ds2):
        v1 
= [self[doc] for doc in ds1]
        v2 
= [self[doc] for doc in ds2]
        
return dot(matutils.unitvec(array(v1).mean(axis=0)), matutils.unitvec(array(v2).mean(axis=0)))



The 1st number, using model.n_similarity(), may be interesting for other reasons. It operates on model directly, whose comparison methods are all inherited from class Word2Vec. So, it actually uses only word-vectors. It averages together the vectors in each word-list, unit-normalizes them, then takes their cosine-similarity. This average-of-words document-vector is also often useful, but isn't what's calculated by PV-Doc2Vec.

 

n_similarity 계산법 


단어 리스트에 있는 모든 단어 벡터의 mean을 계산

 -> unit-normalzation -> cosine-similary 계산하기 



블로그에 있던 예시: 


Set A: (two words)

Vector A1 (-1,0,1)

Vector A2 (0,1,1)


Set B: (one word)

Vector B1 (-0.5,0.5,1)


mean of A1 and A2: A3 = (-0.5,0.5,1)


= > Cosine Distance of A3 and B1: dot(A3,B1) = 1




요약하면 n_similarity 함수는 word2vec 클래스에서 파생되어 

단어 셋에 있는 단어들의 유사도는 비교할 수는 있으나 

문서 벡터의 유사도를 비교하는 것은 아니네요. 

그래도 간접적으로 문서의 유사도를

단어 단위로 판단하는  지표는 될 것 같습니다. 


일단 지금은 이 함수 한번 써보고 

model.docvecs.similarity_unseen_docs 함수도 써봐서 

결과를 비교해봐야겠습니다. 





참고로 해당 기능은 word2vec/ doc2vec 데모 사이트에 추가 되었습니다. 

(http://stockprediction.co.kr/word2vec/)


두 문장을 입력하면 mecab으로 형태소 분석을 한 후 n_similarity 함수를 사용합니다. 



아래는 관련 예시들입니다. 



가장 단어가 유사한 예시 





역사(장소 언급) vs 부동산






역사(장소 언급) vs 주식 




그래도 그럭저럭 결과가 나오는 것 같네요. 


관련 코드들 역시 github에 commit해 두었으니 

참고하세요 



doc2vec-api github 바로가기 







728x90
반응형