2017-05-08 19 views
0

私はパンダを使ってcsvファイルから入力を受け取り、nltkを使ってトークン化を行っています。しかし、私は次のエラーを取得しています:numpy配列を通常のpythonリストに変換するには?

Traceback (most recent call last): 
    File "test.py", line 20, in <module> 
    word = nltk.word_tokenize(words) 
    File "/home/codelife/.local/lib/python3.5/site-packages/nltk/tokenize/__init__.py", line 109, in word_tokenize 
    return [token for sent in sent_tokenize(text, language) 
    File "/home/codelife/.local/lib/python3.5/site-packages/nltk/tokenize/__init__.py", line 94, in sent_tokenize 
    return tokenizer.tokenize(text) 
    File "/home/codelife/.local/lib/python3.5/site-packages/nltk/tokenize/punkt.py", line 1237, in tokenize 
    return list(self.sentences_from_text(text, realign_boundaries)) 
    File "/home/codelife/.local/lib/python3.5/site-packages/nltk/tokenize/punkt.py", line 1285, in sentences_from_text 
    return [text[s:e] for s, e in self.span_tokenize(text, realign_boundaries)] 
    File "/home/codelife/.local/lib/python3.5/site-packages/nltk/tokenize/punkt.py", line 1276, in span_tokenize 
    return [(sl.start, sl.stop) for sl in slices] 
    File "/home/codelife/.local/lib/python3.5/site-packages/nltk/tokenize/punkt.py", line 1276, in <listcomp> 
    return [(sl.start, sl.stop) for sl in slices] 
    File "/home/codelife/.local/lib/python3.5/site-packages/nltk/tokenize/punkt.py", line 1316, in _realign_boundaries 
    for sl1, sl2 in _pair_iter(slices): 
    File "/home/codelife/.local/lib/python3.5/site-packages/nltk/tokenize/punkt.py", line 310, in _pair_iter 
    prev = next(it) 
    File "/home/codelife/.local/lib/python3.5/site-packages/nltk/tokenize/punkt.py", line 1289, in _slices_from_text 
    for match in self._lang_vars.period_context_re().finditer(text): 
TypeError: expected string or bytes-like object 

は、次のコードです:

from textblob import TextBlob 
import nltk  #for cleaning and stop wrds removal 
import pandas as pd  #csv 
import numpy 

data = pd.read_csv("Sample.csv", usecols=[0]) #reading from csv file 
num_rows = data.shape[0] 
#print(questions) 

# cleaning the data 

count_nan = data.isnull().sum() #counting no of null elements 
count_without_nan = count_nan[count_nan==0] #no of not null elements 
data = data[count_without_nan.keys()] # removing null columns 

data_mat = data.as_matrix(columns= None) #converting to numpy matrix 
print(data_mat) 
for question in data_mat: 
    words = question.tolist() 
    word = nltk.word_tokenize(words) 
    print(word) 

私はnumpyの配列を使用していますので、それがあると仮定。どのように私はそれを通常のPythonリストに変換するのですか?

+3

いいえ、エラーが示すように 'nltk.word_tokenize'が'期待文字列またはバイト様オブジェクト 'のためです。それはあなたがすでに成功裏に実行した 'list'に変換するのを助けません。私は*文字列*が必要です。 –

+1

'words'またはその一部を見てください。それが何で、何が含まれているかを理解してください。 – hpaulj

+0

['df.apply()'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html)はあなたの友人です=) – alvas

答えて

0

nltkのword_tokenize()関数は、単一の文字列を取得する必要があります。それに含まれるトークンのリストを返します。これをPythonリスト全体、numpy配列またはpandasデータフレームに適用するには、Pythonで繰り返し処理(ループまたは理解)が必要です。numpyまたはpandas apply*メソッドを使用してください。たとえば、wordsnp.arrayの場合は、次のようにして繰り返し処理できます。

sentences = [ nltk.word_tokenize(string) for string in words ] 

言葉が何か他のものである場合は、コードを修正するか、質問にどのように表示されるかを表示する必要があります。

関連する問題