Preprocessing

Tokenizer

source code:https://github.com/keras-team/keras-preprocessing/blob/master/keras_preprocessing/text.py#L490-L519

some important functions and variables

  • init

  • def fit_on_texts(self, texts) #texts can be a string or a list of strings or a list of list of strings

  • self.word_index # the type of variance is dictonary, which contain a specific word subject to a unique index

  • self.index_word #r eserve the key and value of the word_index

sample

  import tensorflow as tf
from tensorflow import keras
# the package which can tokenizer
from tensorflow.keras.preprocessing.text import Tokenizer
'''
transform the word into number
'''
sentences= ['i love my dog', 'i love my cat','you love my dog!']
tokenizer = Tokenizer(num_words = 100)
tokenizer.fit_on_texts(sentences)
word_index = tokenizer.word_index
print(word_index)
# get the result {'love': 1, 'my': 2, 'i': 3, 'dog': 4, 'cat': 5, 'you': 6}

Serialization

sample

sentences= ['i love my dog', 'i love my cat','you love my dog!','do you think my dog is amazing']
sequences = tokenizer.texts_to_sequences(sentences)
print(sequences)
'''
result is [[3, 1, 2, 4], [3, 1, 2, 5], [6, 1, 2, 4], [6, 2, 4]]
which is not encoding for amazing, because it's not appear in fit texts
'''

To solve this problem,we can set a oov in tokenizer to encode a word which not appear before.

tokenizer = Tokenizer(num_words = 100, oov_token = "<OOV>")
'''
restart the code,we can get the result
[[4, 2, 3, 5], [4, 2, 3, 6], [7, 2, 3, 5], [1, 7, 1, 3, 5, 1, 1]]
'''

but each sequences has the different length of the series, it's difficult for train a neuro network,so we need make the sequnces has the same length.

from tensorflow.keras.preprocessing.sequence import pad_sequences
padded_sequences = pad_sequences(sequences,
padding = 'post', # right padding
maxlen = 5, # max len of senquence
truncating = 'post') # right cut
padded_sequences
'''
then we can get the result
array([[5, 3, 2, 4, 0],
[5, 3, 2, 7, 0],
[6, 3, 2, 4, 0],
[8, 6, 9, 2, 4]])
'''

word processing in nlp with tensorflow的更多相关文章

  1. 论文阅读 | Text Processing Like Humans Do: Visually Attacking and Shielding NLP Systems

    [code&data] [pdf] 主要工作 文章首先证明了对抗攻击对NLP系统的影响力,然后提出了三种屏蔽方法: visual character embeddings adversaria ...

  2. 自然语言处理资源NLP

    转自:https://github.com/andrewt3000/DL4NLP Deep Learning for NLP resources State of the art resources ...

  3. NLP与深度学习(一)NLP任务流程

    1. 自然语言处理简介 根据工业界的估计,仅有21% 的数据是以结构化的形式展现的[1].在日常生活中,大量的数据是以文本.语音的方式产生(例如短信.微博.录音.聊天记录等等),这种方式是高度无结构化 ...

  4. NLP新手入门指南|北大-TANGENT

    开源的学习资源:<NLP 新手入门指南>,项目作者为北京大学 TANGENT 实验室成员. 该指南主要提供了 NLP 学习入门引导.常见任务的开发实现.各大技术教程与文献的相关推荐等内容, ...

  5. 分词(Tokenization) - NLP学习(1)

    自从开始使用Python做深度学习的相关项目时,大部分时候或者说基本都是在研究图像处理与分析方面,但是找工作反而碰到了很多关于自然语言处理(natural language processing: N ...

  6. TensorFlow系列专题(十一):RNN的应用及注意力模型

    磐创智能-专注机器学习深度学习的教程网站 http://panchuang.net/ 磐创AI-智能客服,聊天机器人,推荐系统 http://panchuangai.com/ 目录: 循环神经网络的应 ...

  7. TensorFlow开发者证书 中文手册

    经过一个月的准备,终于通过了TensorFlow的开发者认证,由于官方的中文文档较少,为了方便大家了解这个考试,同时分享自己的备考经验,让大家少踩坑,我整理并制作了这个中文手册,请大家多多指正,有任何 ...

  8. TensorFlow 在android上的Demo(1)

    转载时请注明出处: 修雨轩陈 系统环境说明: ------------------------------------ 操作系统 : ubunt 14.03 _ x86_64 操作系统 内存: 8GB ...

  9. 初学者如何查阅自然语言处理(NLP)领域学术资料

    1. 国际学术组织.学术会议与学术论文 自然语言处理(natural language processing,NLP)在很大程度上与计算语言学(computational linguistics,CL ...

随机推荐

  1. 3.初识Java

    一.Java特性和优势 简单性 面向对象 可移植性 高性能 分布式 动态性 多线程 安全性 健壮性 二.Java三大版本 一次编写到处运行 JavaSE:标准版(桌面程序,控制台开发) JavaME: ...

  2. vue3组合式API

    vue3组合式API 为什么要用组合式API,我们来看看它是如何解决vue2的局限性的 1.vue2的局限性 当组件内容越来越多,逻辑越来越复杂,可读性就会降低,并且难以维护. vue2组件采用配置式 ...

  3. go源码阅读 - container/ring

    相比于List,环的结构有些特殊,环的头部就是尾部,所以每个元素可以代表自身这个环.环其实是一个双向回环链表.type Ring struct { next, prev *Ring Value int ...

  4. EMC信号完整性落地实测1---走出玄学

    EMC信号完整性落地实测1---走出玄学 无论我们从51单片机,STM32电路,运放,传感器,ADC采集还是可控硅晶闸管等等电源电路跨入到电子工程师的行业,我们通常会长时间处于低频的电子电路设计调试阶 ...

  5. Docker部署PostgreSQL主从

    #准备 PostgreSQL12.3版本容器两台,部署参考https://www.cnblogs.com/zspwf/p/16113298.html 主库: 192.168.3.14:2200 从库: ...

  6. 【openstack】红帽公开课笔记内容openstack

    overcloud节点自省失败(introspection) 节点自省--获取overcloud

  7. vue项目中cookie的使用

    Vue使用cookie和session 1:cookie和session 为了防止数据运输或存储终端,特地设置了cookie和session,他们其实都是将数据存储当地. cookie数据保存在客户端 ...

  8. 同时将代码备份到Gitee和GitHub

    同时将代码备份到Gitee和GitHub 如何将GitHub项目一步导入Gitee 如何保持Gitee和GitHub同步更新 如何将GitHub项目一步导入Gitee 方法一: 登陆 Gitee 账号 ...

  9. FreeRTOS --(11)任务管理之系统节拍

    转载自 https://blog.csdn.net/zhoutaopower/article/details/107146764 前面有了创建任务.启动调度器.任务控制,接下来便开始分析一个 Tick ...

  10. pandas子集选取的三种方法:[]、.loc[]、.iloc[]

    pandas读取Excel.csv文件中的数据时,得到的大多是表格型的二维数据,在pandas中对应的即为DataFrame数据结构.在处理这类数据时,往往要根据据需求先获取数据中的子集,如某些列.某 ...