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

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

Serialization

sample

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

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

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

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.

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

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. springcloud集群测试

    使用ribbon实现负载均衡,访问同一个url,轮询不同的服务提供端,从不同的数据库中取数据.

  2. SpringMVC基础原理

    1.拦截所有请求到DispatcherServlet 2.去寻找映射器 3.根据处理器适配器处理业务,返回视图 4.视图解析器解析显示视图

  3. JavaWeb学习day6-Response初学

    web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpSevletRequest对象,代表响应的一个HttpSevletResponse 如果要获取客户端请求过来的数据, ...

  4. 改造@vue/cli项目为服务端渲染-ServerSideRender

    VUE SEO方案二 - SSR服务端渲染 在上一章中,我们分享了预渲染的方案来解决SEO问题,个人还是很中意此方案的,既简单又能解决大部分问题.但是也有着一定的缺陷,所以我们继续来看下一个方案--服 ...

  5. [译]ng指令中的compile与link函数解析 转

    通常大家在使用ng中的指令的时候,用的链接函数最多的是link属性,下面这篇文章将告诉大家complie,pre-link,post-link的用法与区别. 原文地址 angularjs里的指令非常神 ...

  6. 阿里云centos postgresql9.4源码安装 精简步骤、问题解答

    阿里云centos环境源码安装postgresql9.4 本文的安装步骤主要来源于http://www.cnblogs.com/mchina/archive/2012/06/06/2539003.ht ...

  7. C++中 指针的指针是什么?指针的引用又是什么?你可能需要看看这篇文章

    关于变量的定义 我们都知道变量的定义包括一个基本数据类型(base type)和一组声明符,在同一条定义语句中,输入基本数据类型不同,但是声明符的形式却可以不同. //如:i是一个int的整数,a是一 ...

  8. k8s入门之Service(六)

    将一组pod公开为网络服务,通过service代理,可以实现负载均衡 一.ClusterIP 此方式只能在集群内访问 1.使用命令暴露已存在的pod (1)继续使用前面章节的案例,查看名称为nginx ...

  9. 【HCIE】ipv6之6to4隧道如何计算48位前缀地址

    6to4隧道支持router-router,host-router,router-host,host-host 采用专用6to4地址,前缀为2002::/16 其中如何结合ipv4地址? 2002:i ...

  10. Linux获取本机公网IP,调整双节点主从服务的RPC调用逻辑

    简单记录一次双节点的之间的服务调用叭 ~ 现有: 服务A的双节点A1.A2 服务B的双节点B1.B2 服务A 和服务B 通过 Netty 实现 RPC 通信,可能会导致比较玄学的问题.如图: 要做到 ...