Keras 文本预处理 text sequence

预处理

句子分割、ohe-hot:

from keras.preprocessing import text
from keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=4) #num_words:None或整数,个人理解就是对统计单词出现数量后选择次数多的前n个单词,后面的单词都不做处理。
tokenizer.fit_on_texts(texts)
print( tokenizer.word_index) #单词对应的index
print( tokenizer.texts_to_sequences(texts)) # 使用字典将对应词转成index。shape为 (文档数,每条文档的长度)
{'some': 1, 'thing': 2, 'to': 3, 'eat': 4, 'drink': 5, 'food': 6}
[[1, 2, 3], [1, 1, 2, 3], [2, 3]]
print( tokenizer.texts_to_matrix(texts)) # 转成one-hot,与前面的不同。shape为[len(texts),num_words]
print( tokenizer.word_counts) #单词在所有文档中的总数量,如果num_words=4,应该选择some thing to
print( tokenizer.word_docs) #单词出现在文档中的数量
print( tokenizer.index_docs) #index对应单词出现在文档中的数量
[[0. 1. 1. 1.]
 [0. 1. 1. 1.]
 [0. 0. 1. 1.]]
OrderedDict([('some', 3), ('thing', 3), ('to', 3), ('eat', 2), ('drink', 1), ('food', 1)])
{'thing': 3, 'some': 2, 'eat': 2, 'to': 3, 'drink': 1, 'food': 1}
{2: 3, 1: 2, 4: 2, 3: 3, 5: 1, 6: 1}
from keras.preprocessing import text
from keras.preprocessing.text import Tokenizer

text1='some thing to eat'
text2='some some thing to drink'
text3='thing to eat food'
texts=[text1, text2, text3]


print(text.text_to_word_sequence(text3))

print(text.one_hot(text2,20))  #n表示编码值在1到n之间
print(text.one_hot(text2,5))

['thing', 'to', 'eat', 'food']
[5, 5, 9, 19, 9]
[2, 2, 1, 3, 4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值