barilog

エンジニアリング見習い雑記

mecabによるtokenize中に起こるUnicodeDecodeErrorの解決

import MeCab
#mecab = MeCab.Tagger(' -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd')
mecab = MeCab.Tagger('mecabrc -u expert.dic')

import re

from bs4 import BeautifulSoup

from urllib.request import urlopen

# ストップワードを定義
def make_stopwords():
#英語のストップワード
from nltk.corpus import stopwords
nltk_stopwords = stopwords.words('english')
#日本語のストップワード
slothlib_path = 'http://svn.sourceforge.jp/svnroot/slothlib/CSharp/Version1/SlothLib/NLP/Filter/StopWord/word/Japanese.txt'
slothlib_file = urlopen(slothlib_path)
slothlib_stopwords = [line.decode("utf-8").strip() for line in slothlib_file]
slothlib_stopwords = [ss for ss in slothlib_stopwords if not ss=='']

#その他の独自のストップワード
additional_stopwords = ['rt', '%', 'の', 'ん', '0']
#連結
stopwords = nltk_stopwords + slothlib_stopwords + additional_stopwords

return stopwords


def clean_text(text):
replaced_text = text
#replaced_text = '\n'.join(s.strip() for s in text.splitlines()[2:] if s != '') # skip header by [2:]
replaced_text = replaced_text.lower()
replaced_text = re.sub(r'[【】]', ' ', replaced_text) # 【】の除去
replaced_text = re.sub(r'[()()]', ' ', replaced_text) # ()の除去
replaced_text = re.sub(r'[[]\[\]]', ' ', replaced_text) # []の除去
replaced_text = re.sub(r'[「」]', ' ', replaced_text) # 「」の除去
replaced_text = re.sub(r'[@@]\w+', '', replaced_text) # メンションの除去
replaced_text = re.sub(r'^RT', '', replaced_text) # 文頭にRTがあれば除去
replaced_text = re.sub(r'\d+\.?\d*|\.\d+', '0', replaced_text) # 数値があれば全て0に変換
# replaced_text = re.sub(r'https?:\/\/.*?[\r\n ]', '', replaced_text) # URLの除去
replaced_text = re.sub(r"(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+\$,%#]+)", "" , replaced_text) #URLの除去
replaced_text = re.sub(r' ', ' ', replaced_text) # 全角空白の除去
return replaced_text


def clean_html_tags(html_text):
soup = BeautifulSoup(html_text, 'html.parser')
cleaned_text = soup.get_text()
cleaned_text = ''.join(cleaned_text.splitlines())
return cleaned_text


def clean_html_and_js_tags(html_text):
soup = BeautifulSoup(html_text, 'html.parser')
[x.extract() for x in soup.findAll(['script', 'style'])]
cleaned_text = soup.get_text()
cleaned_text = ''.join(cleaned_text.splitlines())
return cleaned_text


def clean_url(html_text):
"""
\S+ matches all non-whitespace characters (the end of the url)
:param html_text:
:return:
"""
clean_text = re.sub(r'http\S+', '', html_text)
return clean_text


def clean_code(html_text):
"""Qiitaのコードを取り除きます
:param html_text:
:return:
"""
soup = BeautifulSoup(html_text, 'html.parser')
[x.extract() for x in soup.findAll(class_="code-frame")]
cleaned_text = soup.get_text()
cleaned_text = ''.join(cleaned_text.splitlines())
return cleaned_text

def extract_hashtag(tweet):
out = tweet
no_hashtag_text = ''
hashtag_list =

pattern = r'[##]([\w一-龠ぁ-んァ-ヴーa-z]+)'

for hashtag in ((re.findall(pattern , tweet))):
#ハッシュタグ
hashtag_list.append(hashtag)
r = r'[##]%s' % hashtag
out = re.sub(r, '', out)
no_hashtag_text = out
return no_hashtag_text, hashtag_list

 


def tokenize(text):
'''
とりあえず形態素解析して名詞だけ取り出す感じにしてる
'''
node = mecab.parseToNode(text)
while node:
if node.feature.split(',')[0] == '名詞':
yield node.surface.lower()
node = node.next


def get_words(contents):
'''
記事群のdictについて、形態素解析してリストにして返す
'''
ret =
for k, content in contents.items():


#ハッシュタグの削除
content = extract_hashtag(content)[0]

#不要な文字列の削除
content = clean_text(content)

ret.append(get_words_main(content))
return ret


def get_words_main(content):
'''
一つの記事を形態素解析して返す
ストップワード以外の単語
'''

return [token for token in tokenize(content) if token not in stop_words]


#ライブラリのパスを設定
import sys
sys.path.append("/home/ubuntu/anaconda3/lib/python3.6/site-packages")


df = TweetItem.df.loc[:,['tweet_id','text']]
stop_words = make_stopwords()
words = get_words({row[1]['tweet_id'] : row[1]['text'] for row in df.iterrows()})

#wordsをpickleに保存
import pickle
with open('words.pkl', 'wb') as f:
pickle.dump(words, f)

from gensim import corpora

# words はさっきの単語リスト
dictionary = corpora.Dictionary(words)
#print(dictionary.token2id)

#dictionary.filter_extremes(no_below=20, no_above=0.3)
# no_berow: 使われてる文章がno_berow個以下の単語無視
# no_above: 使われてる文章の割合がno_above以上の場合無視

#辞書をファイルに保存します。
dictionary.save_as_text('tweetdic.txt')
#作った辞書ファイルをロードして辞書オブジェクト作るとき dictionary = corpora.Dictionary.load_from_text('livedoordic.txt')

corpus = [dictionary.doc2bow(text) for text in words]
with open('corpus.pkl', 'wb') as f:
pickle.dump(corpus, f)

 

上記のコード(tweet文書を読み込んで,corpusというBOWを出力する)を動かした結果,


<ipython-input-45-d0315e671431> in tokenize(text)
106 while node:
107 if node.feature.split(',')[0] == '名詞':
--> 108 yield node.surface.lower()
109 node = node.next
110

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa2 in position 2: invalid start byte

 

というエラーが起きた.

 

これの原因はすぐ見つかった.

 

MeCabのバージョン0.996で、この方法によりPython3からMeCabのparseToNode関数を使うと、最初のsurface(表層形)が表示されないというバグがある。

 

 

 

www.trifields.jp

 

こちらを参考にした.

 

 

このバグの最も簡単な回避策は、parseToNode関数を呼び出す前に、一度、parse関数を呼び出すことで回避することができる。
実際に見てみよう。

先ほどのPython3のコードに「t.parse(”)」という一文を追加する。

 とあるので,

mecab = MeCab.Tagger(~~)

の直後に

mecab.parse("")

を挿入して対策完了

 

 

エラーが解消された.