keras.preprocessing.text.Tokenizer
不能正確處理中文文本。如何修改它以處理中文文本?
from keras.preprocessing.text import Tokenizer
def fit_get_tokenizer(data, max_words):
tokenizer = Tokenizer(num_words=max_words, filters='!"#%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n')
tokenizer.fit_on_texts(data)
return tokenizer
tokenizer = fit_get_tokenizer(df.sentence,max_words=150000)
print('Total number of words: ', len(tokenizer.word_index))
vocabulary_inv = {}
for word in tokenizer.word_index:
vocabulary_inv[tokenizer.word_index[word]] = word
print(vocabulary_inv)
由于我無法在SO中發(fā)布中文文本,我將演示如何使用英語句子,但同樣適用于中文:
確保你有一個中文space-separated句子列表,并且應(yīng)該正確。使用列表將導(dǎo)致意外行為。