2016-07-11 4 views
1

LSTMベースのNNの入力データを準備しようとしています。 私はいくつかのテキスト文書をたくさん持っています、そして、私が望むのは、各文書のためのシーケンスベクトルを作ることです。それで列車データとしてLSTM RNNに送ることができます。Pythonのテキストからシーケンスベクトルを作成する

私の貧弱なアプローチ:

import re 
import numpy as np 
#raw data 
train_docs = ['this is text number one', 'another text that i have'] 

#put all docs together 
train_data = '' 
for val in train_docs: 
    train_data += ' ' + val 

tokens = np.unique(re.findall('[a-zа-я0-9]+', train_data.lower())) 
voc = {v: k for k, v in dict(enumerate(tokens)).items()} 

、その後brutforce "VOC" のdictで、各ドキュメントを交換してください。

このタスクに役立つライブラリがありますか?

+0

参照します。https: //github.com/JonathanRaiman/theano_lstm –

答えて

1

NLTKを使用してトレーニング文書をトークン化できます。 NLTKは標準的な単語トークナイザーを提供するか、独自のトークナイザー(例えばRegexpTokenizer)を定義することを可能にします。さまざまなトークナイザ機能の詳細については、hereをご覧ください。

Hereは、テキストの前処理にも役立ちます。

以下のトークナイザNLTKの事前訓練された単語を使用してクイックデモ:Kerasテキスト前処理クラスで解決

from nltk import word_tokenize 

train_docs = ['this is text number one', 'another text that i have'] 
train_docs = ' '.join(map(str, train_docs)) 

tokens = word_tokenize(train_docs) 
voc = {v: k for k, v in dict(enumerate(tokens)).items()} 
5

http://keras.io/preprocessing/text/

は次のように行わ:

from keras.preprocessing.text import Tokenizer, text_to_word_sequence 

train_docs = ['this is text number one', 'another text that i have'] 
tknzr = Tokenizer(lower=True, split=" ") 
tknzr.fit_on_texts(train_docs) 
#vocabulary: 
print(tknzr.word_index) 

Out[1]: 
{'this': 2, 'is': 3, 'one': 4, 'another': 9, 'i': 5, 'that': 6, 'text': 1, 'number': 8, 'have': 7} 

#making sequences: 
X_train = tknzr.texts_to_sequences(train_docs) 
print(X_train) 

Out[2]: 
[[2, 3, 1, 8, 4], [9, 1, 6, 5, 7]]