환경 설정

!apt-get update
!apt-get install g++ openjdk-8-jdk
# 2. JPype 설치
!pip install JPype1
# RHINO 설치
!pip install rhinoMorph

 

형태소 분석기 설치

import rhinoMorph
rn = rhinoMorph.startRhino()
#예문 분석
text = "한글 테스트 글을 남겨주세요"
sample_data = rhinoMorph.onlyMorph_list(rn,text)
print('sample data :', sample_data)

sample data : ['한글', '테스트', '글', '을', '남기', '어', '주', '시', '어요']

 

실질 형태소만, 동사 어말어미 제외

# 사용 2 : 실질형태소만, 동사의 어말어미는 제외
# 실제적인 의미가 있는 거 : 
text_analyzed = rhinoMorph.onlyMorph_list(rn, text, pos=['NNG', 'NNP','NP','VV','VA','XR','IC','MM','MAG','MAJ'])
print('\n 2.형태소 분석 결과 :', text_analyzed)

2.형태소 분석 결과 : ['한글', '테스트', '글', '남기']

동사 어말어미 포함

# 사용 3 : 실질형태소만, 동사의 어말어미는 포함
text_analyzed = rhinoMorph.onlyMorph_list(rn, text, pos=['NNG', 'NNP','NP','VV','VA','XR','IC','MM','MAG','MAJ'], eomi = True)
print('\n 3.형태소 분석 결과 :', text_analyzed)

3.형태소 분석 결과 : ['한글', '테스트', '글', '남기다']

 

전체 형태소, 품사정보 가져오기

# 사용 4 
morphs, poses = rhinoMorph.wholeResult_list(rn, text)
print('\n4. 분석 결과\n morphs :', morphs)
print('poses : ',poses)

4. 분석 결과 morphs : ['한글', '테스트', '글', '을', '남기', '어', '주', '시', '어요']

poses : ['NNG', 'NNG', 'NNG', 'JKO', 'VV', 'EC', 'VX', 'EP', 'EF']

 

 

# 원문 어절 정보 같이 가져오기 
text_analyzed = rhinoMorph.wholeResult_text(rn,text)
print('\n5. 형태소 분석 결과 :\n',text_analyzed)

5. 형태소 분석 결과 :

한글  한글/NNG

테스트  테스트/NNG

글을  글/NNG + 을/JKO

남겨주세요   남기/VV + 어/EC + 주/VX + 시/EP + 어요/EF

 

연결된 명사 결합

# 6,7 한 어절에서 연결된 명사를 하나의 명사로 결함
# onlyMorph_list 와 wholeResult_list에서 사용 가능
text_analyzed = rhinoMorph.wholeResult_list(rn, text, pos=['NNG', 'NNP','NP','VV','VA','XR','IC','MM','MAG','MAJ'], combineN = True)
print('형태소 분석 결과 ; \n',text_analyzed)


7. 형태소 분석 결과
morphs :  ['한글', '테스트', '글', '을', '남기', '어', '주', '시', '어요']
poses :  ['NNG', 'NNG', 'NNG', 'JKO', 'VV', 'EC', 'VX', 'EP', 'EF']

 

# 사용 8,9 어근 + 하 형태를 하나의 동사로 출력
# wrVv 아규먼트가 담당하며, 기본값은 False로서 둘을 분리하여 출력함
# 분리된 어근이 명사인 경우, 명사로 출력한다. 
# onlyMorph_list, wholeResult_list , WholeResult_text 등 함수에서 모두 사용 가능

text_analyzed = rhinoMorph.wholeResult_list(rn, '사랑합니다')
print('\n8. 형태소 분석 결과 : ', text_analyzed)

text_analyzed = rhinoMorph.wholeResult_list(rn, '사랑합니다', xrVv = True)
print('\n9. 형태소 분석 결과 : ', text_analyzed)


8. 형태소 분석 결과 :  (['사랑', '하', 'ㅂ니다'], ['XR', 'XSV', 'EF'])

9. 형태소 분석 결과 :  (['사랑하', 'ㅂ니다'], ['VV', 'EF'])

+ Recent posts