A-A+

文本转换为向量 tensorflow.keras.preprocessing.text.Tokenizer文本预处理

2020年05月27日 23:29 汪洋大海 暂无评论 共4216字 (阅读2,665 views次)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from keras.preprocessing.text import Tokenizer
from keras.preprocessing import sequence
 
text1 = "学习keras的Tokenizer"
text2 = "就是这么简单"
texts = [text1, text2]
 
"""
# num_words 表示用多少词语生成词典(vocabulary)
# char_level表示 如果为True,则每个字符都将被视为标记。if True, every character will be treated as a token.
# oov_token是out-of-vocabulary,用来代替那些字典上没有的字。
"""
tokenizer = Tokenizer(num_words=5000, char_level=True, oov_token='UNK')
tokenizer.fit_on_texts(texts)
 
# 可以设置词典
# tokenizer.word_index = {'UNK': 1, '学': 2, '习': 3}
 
# 每个word出现了几次
print(tokenizer.word_counts)
# 每个word出现在几个文档中
print(tokenizer.word_docs)
# 每个word出现了几次
print(tokenizer.document_count)
# 每个word对应的index,字典映射
print(tokenizer.word_index)
# mode:‘binary’,‘count’,‘tfidf’,‘freq’之一,默认为‘binary’
# 返回值:形如(len(texts), nb_words)的numpy array
print(tokenizer.texts_to_matrix(texts))
# 序列的列表
print(tokenizer.texts_to_sequences(texts))
texts = tokenizer.texts_to_sequences(texts)
texts = sequence.pad_sequences(texts, maxlen=30, padding='post',truncating='post')
print(texts)
OrderedDict([('学', 1), ('习', 1), ('k', 2), ('e', 3), ('r', 2), ('a', 1), ('s', 1), ('的', 1), ('t', 1), ('o', 1), ('n', 1), ('i', 1), ('z', 1), ('就', 1), ('是', 1), ('这', 1), ('么', 1), ('简', 1), ('单', 1)])
defaultdict(<class 'int'>, {'t': 1, 'o': 1, 'r': 1, 'n': 1, '习': 1, '学': 1, 's': 1, 'k': 1, 'a': 1, 'z': 1, 'e': 1, '的': 1, 'i': 1, '么': 1, '这': 1, '简': 1, '就': 1, '单': 1, '是': 1})
2
{'UNK': 1, 'e': 2, 'k': 3, 'r': 4, '学': 5, '习': 6, 'a': 7, 's': 8, '的': 9, 't': 10, 'o': 11, 'n': 12, 'i': 13, 'z': 14, '就': 15, '是': 16, '这': 17, '么': 18, '简': 19, '单': 20}
[[0. 0. 1. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]
[[5, 6, 3, 2, 4, 7, 8, 9, 10, 11, 3, 2, 12, 13, 14, 2, 4], [15, 16, 17, 18, 19, 20]]
[[ 5  6  3  2  4  7  8  9 10 11  3  2 12 13 14  2  4  0  0  0  0  0  0  0
   0  0  0  0  0  0]
 [15 16 17 18 19 20  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0  0  0  0  0  0]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from keras.preprocessing.text import text_to_word_sequence
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
 
text1 = "今天 北京 下 暴雨 了"
text2 = "我 今天 打车 回家"
texts = [text1, text2]
 
print(text_to_word_sequence(text1))  # 按空格分割语料
# ['今天', '北京', '下', '暴雨', '了']
 
tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(texts)
print(tokenizer.document_count) # 处理文档的数量
# 2
print(tokenizer.word_counts) # 词频字典,按词频从大到小排序
# OrderedDict([('今天', 2), ('北京', 1), ('下', 1), ('暴雨', 1), ('了', 1), ('我', 1), ('打车', 1), ('回家', 1)])
print(tokenizer.word_docs) # 保存每个word出现的文档的数量
# {'了': 1, '暴雨': 1, '北京': 1, '下': 1, '今天': 2, '打车': 1, '回家': 1, '我': 1}
print(tokenizer.word_index) # 给每个词唯一id
# {'今天': 1, '北京': 2, '下': 3, '暴雨': 4, '了': 5, '我': 6, '打车': 7, '回家': 8}
print(tokenizer.index_docs) # 保存word的id出现的文档的数量
# {5: 1, 4: 1, 2: 1, 3: 1, 1: 2, 7: 1, 8: 1, 6: 1}
print(tokenizer.texts_to_matrix(texts))
# [[0. 1. 1. ... 0. 0. 0.]
# [0. 1. 0. ... 0. 0. 0.]]
# shape = (2, 5000)
print(tokenizer.texts_to_sequences(texts))
# [[1, 2, 3, 4, 5],
#  [6, 1, 7, 8] ] 
 
 
# 将序列填充到maxlen长度
print(pad_sequences([[1,2,3],[4,5,6]],maxlen=10,padding='pre')) # 在序列前填充
# [[0 0 0 0 0 0 0 1 2 3]
# [0 0 0 0 0 0 0 4 5 6]]
print(pad_sequences([[1,2,3],[4,5,6]],maxlen=10,padding='post')) # 在序列后填充
# [[1 2 3 0 0 0 0 0 0 0]
# [4 5 6 0 0 0 0 0 0 0]]
['今天', '北京', '下', '暴雨', '了']
2
OrderedDict([('今天', 2), ('北京', 1), ('下', 1), ('暴雨', 1), ('了', 1), ('我', 1), ('打车', 1), ('回家', 1)])
defaultdict(<class 'int'>, {'了': 1, '下': 1, '暴雨': 1, '今天': 2, '北京': 1, '打车': 1, '回家': 1, '我': 1})
{'今天': 1, '北京': 2, '下': 3, '暴雨': 4, '了': 5, '我': 6, '打车': 7, '回家': 8}
defaultdict(<class 'int'>, {5: 1, 3: 1, 4: 1, 1: 2, 2: 1, 7: 1, 8: 1, 6: 1})
[[0. 1. 1. ... 0. 0. 0.]
 [0. 1. 0. ... 0. 0. 0.]]
[[1, 2, 3, 4, 5], [6, 1, 7, 8]]
[[0 0 0 0 0 0 0 1 2 3]
 [0 0 0 0 0 0 0 4 5 6]]
[[1 2 3 0 0 0 0 0 0 0]
 [4 5 6 0 0 0 0 0 0 0]]

文章来源:https://github.com/CLOVEXCWZ/Learn_AI/blob/e93005d64b03bc126b0417fd2957f9f38260b37c/101_%E5%BA%93%E5%AD%A6%E4%B9%A0/203_Keras/501_%E4%B8%80%E4%BA%9B%E7%94%A8%E6%B3%95/101_%E6%96%87%E6%9C%AC%E9%A2%84%E5%A4%84%E7%90%86.ipynb

布施恩德可便相知重

微信扫一扫打赏

支付宝扫一扫打赏

×
标签:

给我留言