Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석 by junn

View this thread on steempeak.com
· @junn · (edited)
$43.34
Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석
<img class="alignnone size-full wp-image-2038" src="https://junn.in/wp-content/uploads/2018/01/screen-16-1.png" alt="" width="574" height="230" />

'일본어를 분석하라'

&nbsp;

내가 왜 이걸 하고 있지? 라고 생각하고 있을 때면 이미 하고 있다..

Mediteam 사이트의 '관련글 찾기' 알고리즘을 TF-IDF를 이용해 구하고 이를 Cosine similarity를 이용해서 유서성을 구현하여 실제 사이트에 올려보았다. (아래 포스팅 참조)

참고:

https://junn.in/archives/1923

https://junn.in/archives/1978

----------------------

그렇게해서 완성한 결과 아래와 상태처럼 출력해낼 수 있었다.

(2018.1.5 현재, 일부 포스트에서만 보임, 조만간 바뀔 예정)<img class="alignnone size-full wp-image-2035" src="https://junn.in/wp-content/uploads/2018/01/screen-19.png" alt="" width="712" height="469" />

-----------------

그러나 이 형태에는 <strong>치명적인 문제</strong>가 있는데,  n개의 문서가 있을 때, 하나의 문서가 추가될 때 아무리 조금 계산해도 n+1 의 cosine similarity를 구해야하며, 전체적으로 갱신하려면 n의 제곱의 해당하는 복잡도가 생기게 된다. 또한 문서가 늘어날수록 IDF에서 단어의 수가 증가하기 때문에 연산이 점차 늘어나게 된다.

그래서 생각해낸 아이디어가 바로 이것. (이건 이미 지난 포스팅의 konlpy 로 하면된다.)

<img class="alignnone size-full wp-image-2039" src="https://junn.in/wp-content/uploads/2018/01/screen-1.png" alt="" width="336" height="55" />

본문의 단어의 빈도를 계산하고,

<img class="alignnone size-full wp-image-2040" src="https://junn.in/wp-content/uploads/2018/01/screen-20.png" alt="" width="419" height="241" />

사이트 내부의 구글신에게 자문을 구하면

<img class="alignnone size-large wp-image-2041" src="https://junn.in/wp-content/uploads/2018/01/screen-19-1.png" alt="" width="584" height="509" />

이렇게 글을 보여준다! 다음엔 Selenium을 이용해 위에서 부터 3개정도의 글만 파싱해내면 끝.

그러면 글이 추가될때마다 바로바로 관련글이 검색되면서, 하루에 한번 전체 문서를 갱신해주면 다 최신업데이트가 간단하게 이루어질 수 있다.

&nbsp;

그러나 이제 다음 미션은, 한글이 아닌 일본어과 중국어는 어떻게 할 것이냐?

일본에서 만든 <strong>형태소 분석기 mecab</strong>이라는 라이브러리가 있다. 참고로 이 라이브러리는 nodeJS용으로도 있는 것 같다.

그리고 nodeJS용으로 mecab을 이용해 한글분석기를 구현한 mecab-ya(<a href="http://think.golbin.net/post/142517991186" target="_blank" rel="noopener">http://think.golbin.net/post/142517991186</a>) 라는 라이브러리도 있다.

mecab은 일종의 껍데기로 보면 되고, 그 안에 사전(Dictionary)이 있어 언어에 따른 형태소 분석기가 가능하다. 일본에서 만들어서 한국어, 중국어도 가능한 것이다.

&nbsp;

그러나 그 과정이 순탄치만은 않았는데 일본어를 테스트하기 위해 mecab 라이브러리를 설치하여 실행하였더니, 다음과 같은 에러가 발생한다.

<img class="alignnone size-full wp-image-2036" src="https://junn.in/wp-content/uploads/2018/01/screen-14-1.png" alt="" width="371" height="244" />

오랜 시간 씨름한 끝에, 이유를 찾았는데 그것은 바로 mecab 라이브러리가 기본적으로 euc-jp 인코딩을 사용하기 때문.

즉 utf-8로 input이 들어가도 출력이 euc-jp로 되어버리니 글자가 깨져서 나와버리는 것이다. 이 출력값을 그대로 파이썬에서 가져오기 때문에 후처리 자체가 불가능 한 것이었다.

이제 원인은 알았고 해결하기 위해 mecab 사전을 utf-8로 바꿔 설치하는 방법을 찾았다. Reference에 있는 mecab-jp 항목들을 보면 된다. (일본어를 읽을 줄 몰라 크롬으로 번역해서 봄)

그러나...하라는대로 해도 맥에서 설치시 에러가 나서 결국 포기.

하지만 이 과정에서 알게 된 것은 우분투 리눅스에서는 이미 utf-8 인코딩을 위해 만들어진 dictionary가 존재한다.

<code># sudo apt-get install mecab</code>

이 경우 기본 단어장으로  /var/lib/mecab/dic/juman 을 제공한다(경로는 시스템에 따라 달라질 듯)

<code># mecab</code> 으로 실행한다. 종료는 Ctrl + D

<img class="alignnone size-full wp-image-2042" src="https://junn.in/wp-content/uploads/2018/01/screen-21.png" alt="" width="431" height="239" />

그러나 같은 이유로 깨져서 나온다.

이제 utf-8 단어장을 설치

<code># sudo apt-get install mecab-ipadic-utf8</code>

출력 과정 중에

update-alternatives: using /var/lib/mecab/dic/ipadic to provide /var/lib/mecab/dic/debian (mecab-dictionary)

라고..기본을 ipadic을 쓴다는 이야기다.

이제는 제대로 출력된다. (종료는 Ctrl +D)

<img class="alignnone size-full wp-image-2038" src="https://junn.in/wp-content/uploads/2018/01/screen-16-1.png" alt="" width="574" height="230" />

이번엔 그럼 한글 단어장을 설치해보자. 물론 Konlpy를 써도 된다. 어차피 영어는 NTLK를 쓸 예정이니, 한글을 Konlpy도 좋고, mecab도 좋을 것 같다.

한글 단어장만 설치. 패키지로 없어서 직접 진행해야 한다.

<code># wget https://bitbucket.org/eunjeon/mecab-ko-dic/downloads/mecab-ko-dic-2.0.3-20170922.tar.gz</code>

<code># tar xvzf ./mecab-ko-dic-2.0.3-20170922.tar.gz</code>


<code># ./configure, # make, #sudo make install</code>

중간에 출력문을 보니 이건 또 설치가 다른 곳에 된다 (/usr/local/lib/mecab/dic/mecab-ko-dic)

경로가 엉망진창..잘 모르고 진행하면 이렇게 되는가 싶다.

&nbsp;

mecab에서 사전을 바꾸려면 -d 라는 옵션을 붙인다.

<code>#mecab -d /usr/local/lib/mecab/dic/mecab-ko-dic</code>

이렇게 하면 아래와 같이 한글이 잘 분석되어 나온다.

<img class="alignnone size-large wp-image-2037" src="https://junn.in/wp-content/uploads/2018/01/screen-18.png" alt="" width="593" height="157" />

중국어도 사전을 찾으면 되는데, 잘 안찾아진다. 그리고 중국어 문서가 없어서 일단 skip.

&nbsp;

다음번에는

(1) 셀레니움을 이용한 검색결과 파싱을 시도해보고,

(2) 문서가 각각 한글/영어/일본어 중 어떤 언어로 이루어졌는지 파악하는 단계

(3) 자주 나오는 단어로 DB를 구성해서, 관련글 검색

하는 과정을 진행해보겠다.

&nbsp;

Refereces :

1&gt; Mecab-JP:
http://brewinstall.org/Install-nkf-on-Mac-with-Brew/
http://nymemo.com/sakura/258/
https://qiita.com/junpooooow/items/0a7d13addc0acad10606

http://brewinstall.org/Install-nkf-on-Mac-with-Brew/    (euc-jp -&gt; utf8)
http://nymemo.com/sakura/258/
https://qiita.com/junpooooow/items/0a7d13addc0acad10606
http://yatta47.hateblo.jp/entry/2015/12/13/140934

2&gt; Mecab Korean :
http://guruble.com/mecab-%ED%95%9C%EA%B8%80-%ED%98%95%ED%83%9C%EC%86%8C-%EB%B6%84%EC%84%9D%EA%B8%B0-%ED%94%8C%EB%9F%AC%EA%B7%B8%EC%9D%B8-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0/
https://github.com/jxpress/pymecab/blob/master/README.md
https://gist.github.com/totakke/10260476
https://charsyam.wordpress.com/2014/07/14/%EC%9E%85-%EA%B0%9C%EB%B0%9C-mecab-%EC%84%A4%EC%B9%98-with-%EC%9D%80%EC%A0%84%ED%95%9C%EB%8B%A2-mac/

https://bitbucket.org/eunjeon/mecab-ko
http://guruble.com/mecab-%ED%95%9C%EA%B8%80-%ED%98%95%ED%83%9C%EC%86%8C-%EB%B6%84%EC%84%9D%EA%B8%B0-%ED%94%8C%EB%9F%AC%EA%B7%B8%EC%9D%B8-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0/

3&gt; Selenium & ChromeDriver :

https://chromedriver.storage.googleapis.com/index.html
https://cjh5414.github.io/python-selenium-chrome-exampleanderror/
https://beomi.github.io/2017/02/27/HowToMakeWebCrawler-With-Selenium/

==== 2018.01.09 ====
한글 사전 설치시 mecab의 기본 사전이 한글 사전으로 변할 수 있다. 
이 경우 일본사전은 아래 주소
mecab -d /var/lib/mecab/dic/ipadic-utf8
한글 사전은
mecab -d /usr/local/lib/mecab/dic/mecab-ko-dic
이런 주소가 될 수 있겠다.
=====================
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id23,680,651
authorjunn
permlinkmediteam-us-mecab-python-library
categorykr
json_metadata"{"app": "steemit/0.1", "format": "markdown", "links": ["https://junn.in/archives/1923", "https://junn.in/archives/1978", "http://think.golbin.net/post/142517991186", "http://brewinstall.org/Install-nkf-on-Mac-with-Brew/", "http://nymemo.com/sakura/258/", "https://qiita.com/junpooooow/items/0a7d13addc0acad10606", "http://yatta47.hateblo.jp/entry/2015/12/13/140934", "http://guruble.com/mecab-%ED%95%9C%EA%B8%80-%ED%98%95%ED%83%9C%EC%86%8C-%EB%B6%84%EC%84%9D%EA%B8%B0-%ED%94%8C%EB%9F%AC%EA%B7%B8%EC%9D%B8-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0/", "https://github.com/jxpress/pymecab/blob/master/README.md", "https://gist.github.com/totakke/10260476", "https://charsyam.wordpress.com/2014/07/14/%EC%9E%85-%EA%B0%9C%EB%B0%9C-mecab-%EC%84%A4%EC%B9%98-with-%EC%9D%80%EC%A0%84%ED%95%9C%EB%8B%A2-mac/", "https://bitbucket.org/eunjeon/mecab-ko", "https://chromedriver.storage.googleapis.com/index.html", "https://cjh5414.github.io/python-selenium-chrome-exampleanderror/", "https://beomi.github.io/2017/02/27/HowToMakeWebCrawler-With-Selenium/"], "image": ["https://junn.in/wp-content/uploads/2018/01/screen-16-1.png"], "tags": ["kr", "kr-dev", "mecab", "language", "analysis"]}"
created2018-01-05 06:27:18
last_update2018-01-08 23:57:24
depth0
children8
net_rshares4,402,118,260,557
last_payout2018-01-12 06:27:18
cashout_time1969-12-31 23:59:59
total_payout_value36.703 SBD
curator_payout_value6.636 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length5,721
author_reputation10,051,299,692,256
root_title"Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (29)
@nhj12311 ·
저도 cobot에서 거래소 정보를 얻기 위해서 셀리니엄을 사용해보았습니다. 나중에 형태소 관련된 일을 할일이 있으면 도움이 많이 될것 같습니다~
properties (22)
post_id23,683,237
authornhj12311
permlinkre-junn-mediteam-us-mecab-python-library-20180105t064621063z
categorykr
json_metadata"{"app": "steemit/0.1", "tags": ["kr"]}"
created2018-01-05 06:46:18
last_update2018-01-05 06:46:18
depth1
children2
net_rshares0
last_payout2018-01-12 06:46:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length80
author_reputation24,421,806,043,412
root_title"Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@junn ·
네, 저도 사실 원리만 알지 실제로 사용해본 일은 많지 않았어서 이번에 제대로 예제를 만들어놓으려고 합니다. 결국 ㅅ이런저런 삽질들이 나중에 도움이 되는 일들이 생기더라고요 ㅎ
properties (22)
post_id23,684,672
authorjunn
permlinkre-nhj12311-re-junn-mediteam-us-mecab-python-library-20180105t065649846z
categorykr
json_metadata"{"app": "steemit/0.1", "tags": ["kr"]}"
created2018-01-05 06:56:51
last_update2018-01-05 06:56:51
depth2
children1
net_rshares0
last_payout2018-01-12 06:56:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length98
author_reputation10,051,299,692,256
root_title"Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@nhj12311 ·
네 맞습니다. 몇시간이나 삽질을 해서 몸으로 체득한것만이 오롯이 자신의 것이 되는것 같습니다. ㅎㅎㅎ
properties (22)
post_id23,701,622
authornhj12311
permlinkre-junn-re-nhj12311-re-junn-mediteam-us-mecab-python-library-20180105t085549357z
categorykr
json_metadata"{"app": "steemit/0.1", "tags": ["kr"]}"
created2018-01-05 08:55:48
last_update2018-01-05 08:55:48
depth3
children0
net_rshares0
last_payout2018-01-12 08:55:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length56
author_reputation24,421,806,043,412
root_title"Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@yanca ·
이건 뭐지...라고 하면서 손은 이미 따봉을 누르고 말았습니다. 따봉!
properties (22)
post_id23,695,364
authoryanca
permlinkre-junn-mediteam-us-mecab-python-library-20180105t081317552z
categorykr
json_metadata"{"app": "steemkr/0.1", "tags": ["kr"]}"
created2018-01-05 08:13:15
last_update2018-01-05 08:13:15
depth1
children1
net_rshares0
last_payout2018-01-12 08:13:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length39
author_reputation2,392,703,522,150
root_title"Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@junn ·
네 저도 진행하면서 이건 대체 뭐지..내가 왜 이걸..끊임없이 대뇌였습니다ㅜ
properties (22)
post_id24,113,393
authorjunn
permlinkre-yanca-re-junn-mediteam-us-mecab-python-library-20180107t082407070z
categorykr
json_metadata"{"app": "steemit/0.1", "tags": ["kr"]}"
created2018-01-07 08:24:06
last_update2018-01-07 08:24:06
depth2
children0
net_rshares0
last_payout2018-01-14 08:24:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length42
author_reputation10,051,299,692,256
root_title"Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@dprk ·
그냥 가만히 있다 가지요 ㅠㅠ
👍  
properties (23)
post_id23,698,048
authordprk
permlinkre-junn-mediteam-us-mecab-python-library-20180105t083147318z
categorykr
json_metadata"{"app": "steemit/0.1", "tags": ["kr"]}"
created2018-01-05 08:31:48
last_update2018-01-05 08:31:48
depth1
children1
net_rshares0
last_payout2018-01-12 08:31:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length16
author_reputation0
root_title"Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@junn ·
이런 삽질이 누군가에게는 도움이 될 수 있을지 몰라 흔적을 남긴것 뿐입니다 ㅜ
properties (22)
post_id24,113,458
authorjunn
permlinkre-dprk-re-junn-mediteam-us-mecab-python-library-20180107t082444340z
categorykr
json_metadata"{"app": "steemit/0.1", "tags": ["kr"]}"
created2018-01-07 08:24:45
last_update2018-01-07 08:24:45
depth2
children0
net_rshares0
last_payout2018-01-14 08:24:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length43
author_reputation10,051,299,692,256
root_title"Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@lljss ·
$1.07
내용 잘 봤습니다~~~
👍  ,
properties (23)
post_id23,711,927
authorlljss
permlinkre-junn-mediteam-us-mecab-python-library-20180105t100520717z
categorykr
json_metadata"{"app": "steemit/0.1", "tags": ["kr"]}"
created2018-01-05 10:05:21
last_update2018-01-05 10:05:21
depth1
children0
net_rshares108,905,835,173
last_payout2018-01-12 10:05:21
cashout_time1969-12-31 23:59:59
total_payout_value1.062 SBD
curator_payout_value0.005 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length12
author_reputation206,273,978,188
root_title"Mediteam.us 개발 - Mecab (Python library)를 이용한 일본어, 한글 형태소 분석"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (2)