안녕하세요, "생각의 웹"입니다.


한글 윈도우즈 8.1 64bit 버전에서 nltk 설치 관련해 이전 포스팅(http://webofthink.tistory.com/59)을 작성한 바 있으나

pycharm 설치 이후에도 nltk 관련 다양한 문제가 있어 해결 방안(이라기 보다 제가 해결 했던 방법)을 공유하고자 이렇게 글을 시작합니다.



먼저 시스템 > 시스템 속성 > 환경 변수에 다음과 같은 사용자 변수를 추가해 줍니다.


HOME

PYTHONHOME


이 변수에는 한글이 포함되지 않은 임의의 존재하는 경로를 입력합니다.



이후 nltk 설치 이후 정상 설치 여부를 확인하기 위해서는 python console 에서 (pycharm의 Tools > Python Console을 추천합니다.) 

다음과 같은 명령어를 수행하면 됩니다.



import nltk

저 같은 경우는 상기 명령 수행 이후 DecodingError가 발생했는데 에러가 발생한 위치를 보면 python 설치 경로\Lib\site-packages\nltk\downloader.py의 아래 줄이 문제를 발생시킵니다.


933  return os.path.join(homedir, 'nltk_data')


문제의 원인은 제 home 경로에 해당하는 위치에 한글이 포함되어 있고 windows 8.1의 경우 이 경로를 임의로 바꿀 수 없기 때문입니다.

따라서 문제를 발생시키는 homedir 변수의 값에 한글 경로가 포함되지 않는 경로를 할당해서 우회합니다.


933  homedir = os.environ['PYTHONHOME']
934  return os.path.join(homedir, 'nltk_data')



이로써 (저의 경우) nltk import 가 정상적으로 수행되었습니다. 이후 관련 package룰 다운로드 받기 위해 아래와 같이 download() 함수를 수행하면 새로운 다운로드 창에서 관련 package들을 다운로드 받을 수 있습니다.


import nltk
nltk.download()


몇 번에 걸쳐 python 설치/제거 작업을 반복하며 얻은 노하우니 만큼 유용하게 쓰였으면 합니다.

이상입니다.


감사합니다.

행복한 하루 되세요!


안녕하세요, "생각의 웹"입니다.


이번에는 갖은 시행 착오 끝에 설치를 완료한 NLTK를 활용해 IoT로 검색해 얻은 600 건의 트윗의 내용을 

word cloud로 만든 사례를 소개하고자 합니다.





이를 위해 python의 tweeter module을 이용해 아래와 같이 코딩했습니다.


# coding=UTF-8

import os.path
import json
import sys
import nltk

import collecttweets
import jsonreader

from collections import Counter
from prettytable import PrettyTable

##
# Get tweets from JSON dump file or twitter API
#
def get_tweets() :
	file_path = "tweets.json"
	if os.path.exists(file_path) :
		return jsonreader.read(file_path)
	else:
		return collecttweets.search_tweets_by_hash_tag('IoT', 5, 100)

##
# Tokenize all tweet messages
#
def tokenize(statuses) :
    status_texts = [ status['text']
        for status in statuses ]

    tokens = []
    for s in status_texts:
        tokens += nltk.tokenize.word_tokenize(s.lower())
    return tokens

##
# Get stemmed list
#
def get_stemmed_list(tokens) :
    from nltk.corpus import stopwords
    stop_words = stopwords.words('english') + ['.', ',', '--', '\'s', '?', ')', '(', ':', '\'', '\'re', '"',
        '-', '}', '{', u'—', 'rt', 'http', 't', 'co', '@', '#', '/', u'…',
        u'#', u';',  u'amp', u't', u'co', u']', u'[', u'`', u'`', u'&', u'|', u'\u265b', u"''", u'$', u'//', u'/'
        u'via',  u'...', u'!', u'``', u'http']

    from nltk.stem import PorterStemmer
    stemmer = PorterStemmer()
    stemmed = []
    for token in tokens:
        # try to decode token
        try:
            decoded = token.decode('utf8')
            #print decoded
        except UnicodeError:
            decoded = token

        if decoded is '' or decoded in stop_words:
            continue
        stem = stemmer.stem(decoded)
        #print stem
        # Skip a few text. I don't know why stopwords are not working :(
        #skip t.co things
        if stem.find(u't.co') > 0:
            continue
        #skip http things
        elif stem.find(u'http') >= 0:
            #print stem
            continue
        else:
            stemmed.append(stem)
    return stemmed

def write_file(filename, lines) :
    f = file(filename, 'w')
    for word in lines:
        try:
            f.write(word.encode('utf-8') + '\n')
        except UnicodeEncodeError, e:
            print 'Encoding error ' + word + '\n'
    f.close()

# Simple test
statuses = get_tweets()
tokens = tokenize(statuses)
print tokens
stemmed = get_stemmed_list(tokens)
print stemmed
write_file('word.txt', stemmed)



간략히 설명하면 

1) twitter API로 관심주제 (저 같은 경우는 IoT)로 tweet를 수집한다. 

2) tweet의 내용을 token으로 쪼개 모은다.

3) stemmer로 token를 분석해 불필요한 단어들을 제거한다. 

4) 남은 word들을 파일로 출력한다. 만들어진 word.txt를 아래 사이트에서 word cloud로 만들면 앞서 그림과 같이 예쁜 그림을 얻을 수 있습니다. 

(silverlight로 제작된 사이트니 IE에서 접속하는 걸 추천합니다 ;-) ) 

http://www.tagxedo.com/app.html 


이상입니다. 


감사합니다.

안녕하세요. "생각의 웹"입니다.




python 2.7.x 버전으로 twitter open API로 가져온 data를 자연어 처리 (Natural Language Processing - 이하 NLP)로 가공해 보여주는 프로젝트 수행 중 

제 windows 8.1 64bit 버전에서 관련 라이브러리 설치 중 발생한 문제로 

골머리 앓았던 사례와 그 해결 방법을 공유하고자 이렇게 글을 적습니다.


먼저 환결 설정에 필요한 도구들과 라이브러리는 다음과 같습니다.


python-2.7.msi (32 bit)

numpy-1.9.2-win32-superpack-python2.7.exe

nltk-3.0.1.win32.exe

PyYAML-3.11.win32-py2.7.exe

ez_setup.py

pycharm-community-4.0.5.exe


(주의!) NLTK 의 호환성 문제로 인해 64bit OS라도 32bit 버전을 설치해야만 합니다.


설치 순서는 다음과 같습니다.

1) python-2.7.msi 설치

2) ez_setup.py 파일을 python 설치 경로(e.g. C:\Python27)에 복사 후 terminal (cmd.exe)에서 'python ez_setup.py' 수행

3) Scripts 폴더로 이동해 'easy_install pip' 수행

4) 관리자 권한으로 numpy-1.9.2-win32-superpack-python2.7.exe 설치

5) 관리자 권한으로 nltk-3.0.1.win32.exe 설치

6) 관리자 권한으로 PyYAML-3.11.win32-py2.7.exe 설치


설치가 모두 정상 완료된 후 pip를 통해 python 모듈을 설치할 때 발생하며 다음과 같은 인코딩 에러가 출력됩니다.


UnicodeEncodeError: 'charmap' codec can't encode characters ...   


안타깝게도 이 메세지를 기반으로 다양한 검색을 통해 해결방법을 모색했지만 결국 해결책을 찾을 수 없었습니다.


그러던 중 지인 중 하나가 python IDE로 pycharm를 추천하길래 혹시나 하는 마음에 pycharm IDE을 설치하게 되었고 

이 도구 안에서도 모듈 설치 기능을 제공함을 알게 되었습니다.


7) pycharm-community-4.0.5.exe 설치


pycharm 설치가 완료되고 수행하면 설치된 python 버전과 python 프로젝트를 만들 수 있는 시작화면이 나옵니다.

임의의 프로젝트를 생성하여 작업환경으로 진입합니다.


pycharm은 Intellij IDE 기반으로 만들어진 IDE라 eclipse IDE와는 메뉴 구성이 달라 어색할 수도 있습니다.

모듈을 추가하기 위해서는 File > Settings 메뉴로 진입합니다.



좌측 메뉴 항목에서 Project: ... / Project Interpreter 를 선택하면 설치된 패키지들이 나열됩니다.

우측 + - 버튼을 통해 패키지를 추가, 삭제 할 수 있는데 이렇게 설치하게 되면 terminal에서 발생하는 에러 없이 패키지 설치가 가능합니다.




사족으로 pip 설치 시 발생하는 원인은 모듈이 설치되는 경로에 한글 경로가 포함되기 때문인 것으로 보입니다.

혹시나 이런 문제로 인해 저와 같은 삽질하시는 분들께 도움이 되길 바라는 마음으로 공유합니다.


감사합니다.



+ Recent posts