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


이번 포스팅에서는 facebook의 graph API(https://developers.facebook.com/tools/explorer/)를 이용해 특정 fan page에 게시된 feed 글들을 word cloud로 만들어 보았습니다.




분석 대상 fan page: https://www.facebook.com/VisionMobile

 

참고 삼아 이 회사는 모바일 앱 및 IoT 관련 산업 및 개발자 동향을 분석해 인사이트 리포트를 제공하는 영국 회사로 깔끔한 시각화 (visualization) 과 핵심을 관통하는 분석으로 평소 많은 도움을 받고 있습니다.


코드 설명에 앞서, facebook의 경우 주기적으로 access token를 발급 받아서 유효한 값을 query parameter로 전송해야만 정상적으로 결과를 얻을 수 있습니다.

자세한 사항은 doc (https://developers.facebook.com/docs/graph-api?locale=ko_KR) 를 참고하시기 바랍니다.


1. feed data를 가져오는 class 만들기 

facebook의 특정 page에서 feeds를 가져오기 위해 PsyCharm 혹은 pip 로 'requests', 'urllib3' 를 설치 후 다음과 같이 구현했습니다.


import requests
import json
import facebook
import urllib3
import urllib3.contrib.pyopenssl # due to https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning
urllib3.contrib.pyopenssl.inject_into_urllib3()
urllib3.disable_warnings()

class FacebookRequester:
    base_url = 'https://graph.facebook.com/'
    version = 'v2.3'
    content = []
    def __init__(self, token):
        self.access_token = token
        self.page = 'me'
    def setPage(self, page):
        self.page = page
    def getFeeds(self, limit, times):
        fields = 'feed.limit(%s)' % limit
        url = '%s/%s/%s?fields=%s&access_token=%s' % \
              (self.base_url, self.version, self.page, fields, self.access_token)
        counter = 0
        print url
        while counter < times:
            response = requests.get(url).json()
            print response

            try:
                content = response['feed']['data']
                paging = response['feed']['paging']
            except KeyError: # in case of retrieving by next page
                content = response['data']
                paging = response['paging']

            #print content
            #print paging

            self.content = self.content + content
            next_page_url = paging['next']
            if not next_page_url:
                break
            else :
                counter += 1
                url = next_page_url
                #print url
        return self.content

상기 코드에서 유심히 볼 부분은 urllib3 입니다. 

이 라이브러리가 없으면 python 2.* 에서 https 로 전송 시도 시 TLS 보안 관련 결함이 있기 때문에 insecure platform warning 과 함께 전송이 되지 않습니다. 따라서 관련 가이드 (https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning) 에 따라 필요한 warning skip code가 추가되었습니다.


두 번째로 facebook은 한번의 요청으로 가져올 수 있는 데이터 양이 정해져 있기 때문에 그 다음 데이터를 가져올 수 있도록 paging property의 next attribute에 다음 요청의 URL 를 제공합니다. 따라서 다음 요청 시에는 이 값을 사용하도록 구현했습니다.

2. feed 가져와서 word 추출하기 


import os.path
import json
import sys
import nltk

import jsonreader

from prettytable import PrettyTable
from fb_requester import FacebookRequester

##
# Get feeds using facebook graph API
#
def get_feeds():

    ACCESS_TOKEN = ''
    requester = FacebookRequester(ACCESS_TOKEN)
    requester.setPage('VisionMobile')
    feeds = requester.getFeeds(100, 5)
    return feeds


##
# Tokenize all feed messages
#
def tokenize(feeds):

    feed_texts = []
    for feed in feeds:
        try:
            feed_message = feed['description'] # message attribute contains URL but description doesn't
            feed_texts.append(feed_message)

        except KeyError:
            #print feed # if feed has no message field
            continue

    tokens = []
    for s in feed_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', '%', 'co', '@', '#', '/', u'…',
                                               u'#', u';', u'amp', u't', u'co', u']', u'[', u'`', u'`', u'&', u'|',
                                               u'\u265b', u"''", 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 facebook things
        if stem.find(u'facebook.com') > 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
feeds = get_feeds()
tokens = tokenize(feeds)
print tokens
stemmed = get_stemmed_list(tokens)
print stemmed
write_file('feed-word.txt', stemmed)


이 코드는 앞서 포스팅한 tweet으로 word cloud 만들기 코드와 거의 유사하며 tweeter와 facebook의 JSON shema 차이 부분만 변경되었으니 참고해 보시기 바랍니다.


이처럼 양대 SNS 서비스인 tweeter 와 facebook에서 추출한 비정형 데이터에서 문자열 추출을 통해 분석하는 예제를 수행해 보았습니다.

이제는 다양한 이유로 충분한 데이터를 모으려면 제약이 심한 관계로 아주 작은 데이터를 가지고 한 실습에 불과 하지만 이와 같은 방식으로 데이터를 모으고 분석할 수 있는 기반을 경험했다는 점에서 의의를 두고 싶습니다.


이상입니다.


감사합니다.

행복한 하루 되시길!

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


한글 윈도우즈 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 설치/제거 작업을 반복하며 얻은 노하우니 만큼 유용하게 쓰였으면 합니다.

이상입니다.


감사합니다.

행복한 하루 되세요!


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




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