Keras---text.Tokenizer:文本与序列预处理

来源:互联网 发布:贪心算法背包问题代码 编辑:程序博客网 时间:2024/05/18 02:56

keras中文文档:http://keras-cn.readthedocs.io/en/latest/preprocessing/text/

1 简介

在进行自然语言处理之前,需要对文本进行处理。 
本文介绍keras提供的预处理包keras.preproceing下的text与序列处理模块sequence模块

2 text模块提供的方法

  • text_to_word_sequence(text,fileter) 可以简单理解此函数功能类str.split
  • one_hot(text,vocab_size) 基于hash函数(桶大小为vocab_size),将一行文本转换向量表示(把单词数字化,vocab_size=5表示所有单词全都数字化在5以内

3 text.Tokenizer类

这个类用来对文本中的词进行统计计数,生成文档词典,以支持基于词典位序生成文本的向量表示。 
init(num_words) 构造函数,传入词典的最大值

3.1 成员函数

  • fit_on_text(texts) 使用一系列文档来生成token词典,texts为list类,每个元素为一个文档。
  • texts_to_sequences(texts) 将多个文档转换为word下标的向量形式,shape为[len(texts),len(text)] -- (文档数,每条文档的长度)
  • texts_to_matrix(texts) 将多个文档转换为矩阵表示,shape为[len(texts),num_words]

3.2 成员变量

  • document_count 处理的文档数量
  • word_index 一个dict,保存所有word对应的编号id,从1开始
  • word_counts 一个dict,保存每个word在所有文档中出现的次数
  • word_docs 一个dict,保存每个word出现的文档的数量
  • index_docs 一个dict,保存word的id出现的文档的数量

3.3 示例

import keras.preprocessing.text as Tfrom keras.preprocessing.text import Tokenizertext1='some thing to eat'text2='some thing to drink'texts=[text1,text2]print T.text_to_word_sequence(text1)  #以空格区分,中文也不例外 ['some', 'thing', 'to', 'eat']print T.one_hot(text1,10)  #[7, 9, 3, 4] -- (10表示数字化向量为10以内的数字)print T.one_hot(text2,10)  #[7, 9, 3, 1]tokenizer = Tokenizer(num_words=None) #num_words:None或整数,处理的最大单词数量。少于此数的单词丢掉tokenizer.fit_on_texts(texts)print( tokenizer.word_counts) #[('some', 2), ('thing', 2), ('to', 2), ('eat', 1), ('drink', 1)]print( tokenizer.word_index) #{'some': 1, 'thing': 2,'to': 3 ','eat': 4, drink': 5}print( tokenizer.word_docs) #{'some': 2, 'thing': 2, 'to': 2, 'drink': 1,  'eat': 1}print( tokenizer.index_docs) #{1: 2, 2: 2, 3: 2, 4: 1, 5: 1}# num_words=多少会影响下面的结果,行数=num_wordsprint( tokenizer.texts_to_sequences(texts)) #得到词索引[[1, 2, 3, 4], [1, 2, 3, 5]]print( tokenizer.texts_to_matrix(texts))  # 矩阵化=one_hot[[ 0.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.], [ 0.,  1.,  1.,  1.,  0.,  1.,  0.,  0.,  0.,  0.]]'''将新闻文档处理成单词索引序列,单词与序号之间的对应关系靠单词的索引表word_index来记录'''#例-------------------------------------------------------------------------tokenizer = Tokenizer(num_words=None) # 分词MAX_NB_WORDStokenizer.fit_on_texts(all_texts)sequences = tokenizer.texts_to_sequences(all_texts) #受num_words影响word_index = tokenizer.word_index # 词_索引print('Found %s unique tokens.' % len(word_index))data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)  #将长度不足 100 的新闻用 0 填充(在前端填充)labels = to_categorical(np.asarray(all_labels)) #最后将标签处理成 one-hot 向量,比如 6 变成了 [0,0,0,0,0,0,1,0,0,0,0,0,0],print('Shape of data tensor:', data.shape)print('Shape of label tensor:', labels.shape)# Shape of data tensor: (81, 1000)  -- 81条数据# Shape of label tensor: (81, 14)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

4 sequence模块

4.1 模块提供的方法

  • pad_sequences(sequences, maxlen, padding=’pre’, truncating=’pre’, value=0.) 将序列填充到maxlen长度,padding取值有pre|post,value指定用何值填充的值

4.2 示例

import keras.preprocessing.sequence as SS.pad_sequences([[1,2,3]],10,padding='post')# [[1, 2, 3, 0, 0, 0, 0, 0, 0, 0]]
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

原创粉丝点击