2016-01-14 23 views
6

私は、余分なデータと一緒に使用できるPythonでTerm Document Matrixを構築する効果的な方法を探しています。Pythonのテキスト処理:NLTKとpandas

いくつかの属性を持つテキストデータがあります。私はテキストを分析したいと思います。テキストから抽出したフィーチャ(個々の単語トークンやLDAトピックなど)を他の属性と関連付けることができます。

私の計画は、パンダのデータフレームとしてデータをロードし、各応答はドキュメントを表します。残念ながら、私はこの問題に遭遇した:両方のケースでは、テキストは、このような方法で処理されたことを

import pandas as pd 
import nltk 

pd.options.display.max_colwidth = 10000 

txt_data = pd.read_csv("data_file.csv",sep="|") 
txt = str(txt_data.comment) 
len(txt) 
Out[7]: 71581 

txt = nltk.word_tokenize(txt) 
txt = nltk.Text(txt) 
txt.count("the") 
Out[10]: 45 

txt_lines = [] 
f = open("txt_lines_only.txt") 
for line in f: 
    txt_lines.append(line) 

txt = str(txt_lines) 
len(txt) 
Out[14]: 1668813 

txt = nltk.word_tokenize(txt) 
txt = nltk.Text(txt) 
txt.count("the") 
Out[17]: 10086 

注意をそのスペース、文字だけのものと、。?! (簡単のために)削除されました。

pandasフィールドが文字列に変換されていることがわかるように、一致が少なくなり、文字列の長さも短くなります。

上記のコードを改善する方法はありますか?

また、str(x)はコメントのうち1つの大きな文字列を作成し、[str(x) for x in txt_data.comment]は単語の袋に分割できないリストオブジェクトを作成します。ドキュメントインデックスを保持するnltk.Textオブジェクトを作成する最良の方法は何ですか?言い換えれば、私はTerm Document Matrix、TermDocumentMatrix()と同等のRをtmパッケージから作成する方法を探しています。

多くのありがとうございます。

+1

あなたの質問は確かではありませんが、パターン、textblob、C&Cのようなライブラリ、あなたのために役立つかもしれないNLP用の他のライブラリがあります。他の人よりも自分自身の優位性。 – mid

+0

ありがとう@mid、私はgensimを認識していますが、以前はtextblobについて聞いたことがありません。私はPythonの新機能です(私は通常Rで働いています)。パッケージがどれほど普及しているかを考えれば、NLTKで行き詰まっていることに本当に疑問を抱きます。 – IVR

答えて

9

pandasDataFrameを使用する利点は、そのように各rownltk機能を適用するには、次のようになります。

word_file = "/usr/share/dict/words" 
words = open(word_file).read().splitlines()[10:50] 
random_word_list = [[' '.join(np.random.choice(words, size=1000, replace=True))] for i in range(50)] 

df = pd.DataFrame(random_word_list, columns=['text']) 
df.head() 

               text 
0 Aaru Aaronic abandonable abandonedly abaction ... 
1 abampere abampere abacus aback abalone abactor... 
2 abaisance abalienate abandonedly abaff abacina... 
3 Ababdeh abalone abac abaiser abandonable abact... 
4 abandonable abandon aba abaiser abaft Abama ab... 

len(df) 

50 

txt = df.text.apply(word_tokenize) 
txt.head() 

0 [Aaru, Aaronic, abandonable, abandonedly, abac... 
1 [abampere, abampere, abacus, aback, abalone, a... 
2 [abaisance, abalienate, abandonedly, abaff, ab... 
3 [Ababdeh, abalone, abac, abaiser, abandonable,... 
4 [abandonable, abandon, aba, abaiser, abaft, Ab... 

txt.apply(len) 

0  1000 
1  1000 
2  1000 
3  1000 
4  1000 
.... 
44 1000 
45 1000 
46 1000 
47 1000 
48 1000 
49 1000 
Name: text, dtype: int64 

その結果、各rowエントリの.count()を取得:

txt = txt.apply(lambda x: nltk.Text(x).count('abac')) 
txt.head() 

0 27 
1 24 
2 17 
3 25 
4 32 

これを使用して結果を合計することができます。

txt.sum() 

1239 
+0

ありがとう@Stefan、ちょうど私の問題を解決しますが、 'txt'オブジェクトはまだ' apply'、 'map'や' for'ループを使っていくつかのNLTK関数を使うことができることを意味するpandasデータフレームオブジェクトです。しかし、もしnltk.Text(txt).concordance( "the")のようなことをしたいのなら、私は問題に遭遇します。これを解決するためには、テキスト変数全体を文字列に変換する必要があります。最初の例で見たように、文字列は何らかの理由で切り捨てられます。どのようにこれを克服するための任意の考え?どうもありがとう! – IVR

+0

'' df.text.tolist()]のt - 作成後または '.tokenize()'の後に '[t for t for df.text.tolist()]'を使用して 'text''列全体を1つの単語リストに変換できます。 – Stefan

関連する問題