こちらの記事を見ながら中国語の文章の形態素解析を試みています。そこで記事内の#コーパスを前処理する
にあるコードを書いてる時にエラーが起きました。エディタはGoogle Colabを使っております。
from operator import itemgetter from itertools import chain import numpy as np import pandas as pd import jieba from nltk.corpus import stopwords from sklearn.feature_extraction.text import TfidfVectorizer import matplotlib.pyplot as plt from PIL import Image from wordcloud import WordCloud, ImageColorGenerator from progressbar import progressbar # コーパスを読み込む df = pd.read_csv('drive/My Drive/Colab Notebooks/HSK_text.csv') corpus = df.Listenning_Script print(corpus.head(260)) # 今回は英語を含んでないため、中国語のstop wordsだけ読み込む stopwords = pd.read_csv('drive/My Drive/Colab Notebooks/stop_words.txt', header=None)[0].tolist() # ユーザー定義単語は一旦割愛 # コーパスを前処理する def preprocess_text(text): tokens = jieba.cut(text) lower_alpha = [t.lower() for t in tokens if t.isalpha()] no_stops = [t for t in lower_alpha if t not in set(stopwords)] return ','.join(no_stops) corpus_processed = corpus.apply(preprocess_text)
でlower_alpha = [t.lower() for t in tokens if t.isalpha()]
に波線が引かれるのですが、エラー内容が表示されませんでした。同じように前処理をしてるこちらのような記事などを参考にしたりしたのですが原因がわかりませんでした。
追記:ppaul樣より波線はエラーではないと教えていただいたため、エラーが出た箇所を提示しますと
AttributeError Traceback (most recent call last) <ipython-input-31-90f2e95cf399> in <module>() 31 return ','.join(no_stops) 32 ---> 33 corpus_processed = corpus.apply(preprocess_text) /usr/local/lib/python3.7/dist-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds) 4211 else: 4212 values = self.astype(object)._values -> 4213 mapped = lib.map_infer(values, f, convert=convert_dtype) 4214 4215 if len(mapped) and isinstance(mapped[0], Series): pandas/_libs/lib.pyx in pandas._libs.lib.map_infer() <ipython-input-7-ee03f1307bb0> in preprocess_text(text) 27 def preprocess_text(text): 28 tokens = jieba.cut(text) ---> 29 lower_alpha = [t.lower() for t in tokens if t.isalpha()] 30 no_stops = [t for t in lower_alpha if t not in set(stopwords)] 31 return ','.join(no_stops) <ipython-input-7-ee03f1307bb0> in <listcomp>(.0) 27 def preprocess_text(text): 28 tokens = jieba.cut(text) ---> 29 lower_alpha = [t.lower() for t in tokens if t.isalpha()] 30 no_stops = [t for t in lower_alpha if t not in set(stopwords)] 31 return ','.join(no_stops) /usr/local/lib/python3.7/dist-packages/jieba/__init__.py in cut(self, sentence, cut_all, HMM, use_paddle) 298 """ 299 is_paddle_installed = check_paddle_install['is_paddle_installed'] --> 300 sentence = strdecode(sentence) 301 if use_paddle and is_paddle_installed: 302 # if sentence is null, it will raise core exception in paddle. /usr/local/lib/python3.7/dist-packages/jieba/_compat.py in strdecode(sentence) 77 if not isinstance(sentence, text_type): 78 try: ---> 79 sentence = sentence.decode('utf-8') 80 except UnicodeDecodeError: 81 sentence = sentence.decode('gbk', 'ignore') AttributeError: 'float' object has no attribute 'decode'
AttributeError: 'float' object has no attribute 'encode'
という同じようなエラーを解決されてる質問を見つけたのですが、今回はどう直せばよろしいでしょうか?
HSK_test.csvに関しましては、Listenning_Scriptカラムに中国語が入ってるような形になります。
知見をお持ちの方、よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー