2016-07-11 5 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()} 

다음은 "VOC"DICT 각 문서를 대체 brutforce.

이 작업에 도움이되는 라이브러리가 있습니까?

+0

참조 : HTTPS : //github.com/JonathanRaiman/theano_lstm –

답변

1

NLTK를 사용하여 교육 문서를 토큰화할 수 있습니다. NLTK는 표준 단어 토큰 화기를 제공하거나 자신의 토큰 라이저 (예 : RegexpTokenizer)를 정의 할 수 있도록합니다. 사용할 수있는 다양한 토큰 라이저 기능에 대한 자세한 내용은 here을 참조하십시오.

Here은 텍스트를 사전 처리하는 데 도움이 될 수 있습니다.

아래 tokeniser 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]]