2.5使用NLTK进行标记和处理

Natural Language Toolkit,简称NLTK,是一个为处理和建模文本而编写的Python库,它提供了加载和处理文本的工具,我们可以使用这些工具来为我们的机器学习准备所需要的数据

2.5.1 安装NLTK

你可以使用自己喜欢的包管理工具来下载NLTK,例如:pip

pip install nltk

安装NLTK后,你需要安装NLTK数据,包括一系列大量的文本集合,可以在以后用它来测试NLTK中的其他工具。首先下载文本数据

import nltk
nltk.download()

2.5.2 分句

一个很好的思路就是,第一步将文本拆分为句子,一些模型更喜欢以段落或句子的形式输入数据,如word2vec,可以先将文本分解成句子,在将每个句子分解成单词。NLTK提供了sent_tokenize()函数将文本拆分成句子。

from nltk import sent_tokenize
with open('Metamorphosis.txt','r')as f:text = f.read()
sentences = sent_tokenize(text)
print(sentences[0])

其结果是:

One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.

2.5.3 分词

NLTK提供了一个word_tokenize()函数,用于将字符串拆分为单词。它是根据空格和标点符号来进行分割的。例如,逗号和句号被视为单独的标记。缩略词被分开(Whats被分为What和s’’),引号被保留.

from nltk import word_tokenize
with open('Metamorphosis.txt','r')as f:text = f.read()
words = word_tokenize(text)
print(words[:100])

其结果为:

['One', 'morning', ',', 'when', 'Gregor', 'Samsa', 'woke', 'from', 'troubled', 'dreams', ',', 'he', 'found', 'himself', 'transformed', 'in', 'his', 'bed', 'into', 'a', 'horrible', 'vermin', '.', 'He', 'lay', 'on', 'his', 'armour-like', 'back', ',', 'and', 'if', 'he', 'lifted', 'his', 'head', 'a', 'little', 'he', 'could', 'see', 'his', 'brown', 'belly', ',', 'slightly', 'domed', 'and', 'divided', 'by', 'arches', 'into', 'stiff', 'sections', '.', 'The', 'bedding', 'was', 'hardly', 'able', 'to', 'cover', 'it', 'and', 'seemed', 'ready', 'to', 'slide', 'off', 'any', 'moment', '.', 'His', 'many', 'legs', ',', 'pitifully', 'thin', 'compared', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him', ',', 'waved', 'about', 'helplessly', 'as', 'he', 'looked', '.', 'What', "'s", 'happened', 'to', 'me']

我们看到标点符号被分成了词标记,下面我们可以来过滤标点符号。

2.5.4 过滤标点

我们可以过滤点所有不想要的标点符号,例如所有独立的标点符号。这可以通过迭代所有标记并仅保留那些全部为字母的标记来完成。Python具有可以使用的函数isalpha()。

from nltk import word_tokenize
with open('Metamorphosis.txt','r')as f:text = f.read()
words = word_tokenize(text)
words = [word for word in words if word.isalpha()]
print(words[:100])

其结果为:

['One', 'morning', 'when', 'Gregor', 'Samsa', 'woke', 'from', 'troubled', 'dreams', 'he', 'found', 'himself', 'transformed', 'in', 'his', 'bed', 'into', 'a', 'horrible', 'vermin', 'He', 'lay', 'on', 'his', 'back', 'and', 'if', 'he', 'lifted', 'his', 'head', 'a', 'little', 'he', 'could', 'see', 'his', 'brown', 'belly', 'slightly', 'domed', 'and', 'divided', 'by', 'arches', 'into', 'stiff', 'sections', 'The', 'bedding', 'was', 'hardly', 'able', 'to', 'cover', 'it', 'and', 'seemed', 'ready', 'to', 'slide', 'off', 'any', 'moment', 'His', 'many', 'legs', 'pitifully', 'thin', 'compared', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him', 'waved', 'about', 'helplessly', 'as', 'he', 'looked', 'What', 'happened', 'to', 'me', 'he', 'thought', 'It', 'was', 'a', 'dream', 'His', 'room', 'a', 'proper', 'human', 'room']

从其结果看:分词例子中除了标点没有之外,还有连词“‘armour-like’”也删除了。

2.5.5 过滤掉停用词(和管道)

停用词是那些对词语的深层含义没有贡献的词语,他们是最常用的词语。例如:a和is对于某些问题,删除停用词是有意义的。NLTK提供了各种语言的停用词列表。变现方式如下:

from nltk.corpus import stopwords
stop_words = stopwords.words('english')
print(stop_words)

其结构如下

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]

可以看出他们都是小写。可以使用tokens与停用词进行比较并过滤掉他们。我们还是用Metamorphosis.txt来做演示,其步骤如下:

  • 加载文本
  • 分成tokens
  • 转化为小写
  • 删除标记中的符号
  • 从剩下的tokens中过滤掉非字母
  • 从剩下的tokens中过滤掉停用词
import string,re
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
with open('Metamorphosis.txt','r')as f:text = f.read()tokens = word_tokenize(text)
tokens = [token.lower() for token in tokens]
re_punc = re.compile('[%s]'%re.escape(string.punctuation))
stripped = [re_punc.sub(' ',w)for w in tokens]
words = [word for word in stripped if  word.isalpha()]
stop_words = set(stopwords.words('english'))
words = [word for word in words if not word in stop_words]
print(words[:100])

结果:

['one', 'morning', 'gregor', 'samsa', 'woke', 'troubled', 'dreams', 'found', 'transformed', 'bed', 'horrible', 'vermin', 'lay', 'back', 'lifted', 'head', 'little', 'could', 'see', 'brown', 'belly', 'slightly', 'domed', 'divided', 'arches', 'stiff', 'sections', 'bedding', 'hardly', 'able', 'cover', 'seemed', 'ready', 'slide', 'moment', 'many', 'legs', 'pitifully', 'thin', 'compared', 'size', 'rest', 'waved', 'helplessly', 'looked', 'happened', 'thought', 'dream', 'room', 'proper', 'human', 'room', 'although', 'little', 'small', 'lay', 'peacefully', 'four', 'familiar', 'walls', 'collection', 'textile', 'samples', 'lay', 'spread', 'table', 'samsa', 'travelling', 'salesman', 'hung', 'picture', 'recently', 'cut', 'illustrated', 'magazine', 'housed', 'nice', 'gilded', 'frame', 'showed', 'lady', 'fitted', 'fur', 'hat', 'fur', 'boa', 'sat', 'upright', 'raising', 'heavy', 'fur', 'muff', 'covered', 'whole', 'lower', 'arm', 'towards', 'viewer', 'gregor', 'turned']

可以看到除了其他变换之外,像a和 to之类的都被清除。

2.5.6 词干

词干是指将每个单词缩减的过程,例如shing,shed,sher都减少到sh,一些文本分类中,可以从词干中受益,以便既减少词汇又专注于文档的情感而不是更深层的含义。尽管有很多词干提取算法,但是目前行业流行的方法还是Porter Stemming算法,可以通过NLTK类库中的porterstemmer类使用这个算法。例如:

from nltk.tokenize import word_tokenize
from nltk.stem.porter import PorterStemmer
with open('Metamorphosis.txt','r')as f:text = f.read()tokens = word_tokenize(text)
porter = PorterStemmer()
stemmed = [porter.stem(word) for word in tokens]
print(stemmed[:100])

其结果为:

['one', 'morn', ',', 'when', 'gregor', 'samsa', 'woke', 'from', 'troubl', 'dream', ',', 'he', 'found', 'himself', 'transform', 'in', 'hi', 'bed', 'into', 'a', 'horribl', 'vermin', '.', 'He', 'lay', 'on', 'hi', 'armour-lik', 'back', ',', 'and', 'if', 'he', 'lift', 'hi', 'head', 'a', 'littl', 'he', 'could', 'see', 'hi', 'brown', 'belli', ',', 'slightli', 'dome', 'and', 'divid', 'by', 'arch', 'into', 'stiff', 'section', '.', 'the', 'bed', 'wa', 'hardli', 'abl', 'to', 'cover', 'it', 'and', 'seem', 'readi', 'to', 'slide', 'off', 'ani', 'moment', '.', 'hi', 'mani', 'leg', ',', 'piti', 'thin', 'compar', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him', ',', 'wave', 'about', 'helplessli', 'as', 'he', 'look', '.', 'what', "'s", 'happen', 'to', 'me']

运行这个例子,单词已经减少到他们的词干,比如trouble已经成为troubl,还可以看到词干实现还将token全部变成小写。
在NLTK中有一套很好的词干和词形还原算法可供选择,如果将词语缩减到他们的根目录就是你的项目需要的东西。

2.6 其他文字处理注意事项

上面的教程中所使用的文本非常干净,所有会跳过了很多需要自己预处理的问题。下面是在处理其他文本过程中需要注意的简短列表:

  1. 处理的问题不适合含有大量文档和大型文档;
  2. 从标记中提取文本,如HTML,PDF或者其他结构化文档格式;
  3. 从其他语言到英文的音译;
  4. 将Unicode字符解码为规范格式,例如utf-8
  5. 处理特定领域的单词,短语和首字母缩略图
  6. 处理或者删除数字,例如金额或日期
  7. 找到并纠正拼写错误
  8. 以及其他

这份列表还可以给出更多,希望你能将其运用并获得干净的文本。

keras自然语言处理(五)相关推荐

  1. keras.metrics有五种accuracy

    keras.metrics有五种accuracy,其使用的场景如下: accuracy真实标签和模型预测均为标量,如真实标签为[0,1,1,0,2,0],模型输出的预测为[0,2,1,1,2,0],此 ...

  2. keras自然语言处理(四)

    第二章 如何使用NLTK手动清理文本 你不能直接从原始文本训练机器学习或深度学习模型,必须将文本转化成能够输入到模型的张量(tensor),这个过程意味着对原始文本进行处理,例如单词,标点和大小写等. ...

  3. Keras自然语言处理(九)

    第六章 为电影评论的情感分析做准备 每个问题的文本数据都不同,准备工作从简单的步骤开始,例如加载数据,但是随着任务的进行,数据清理工作会变得越来越困难.下面我们来逐步了解如何为电影评论的情绪分析准备文 ...

  4. 【小白学习keras教程】五、基于reuters数据集训练不同RNN循环神经网络模型

    @Author:Runsen 文章目录 循环神经网络RNN Load Dataset 1. Vanilla RNN 2. Stacked Vanilla RNN 3. LSTM 4. Stacked ...

  5. keras.metrics中各种metric的区别

    (一)keras.metrics有五种accuracy https://blog.csdn.net/weixin_44866160/article/details/106437277?utm_medi ...

  6. 解读Keras在ImageNet中的应用:详解5种主要的图像识别模型

    更多深度文章,请关注:https://yq.aliyun.com/cloud 几个月前,我写了一篇关于如何使用CNN(卷积神经网络)尤其是VGG16来分类图像的教程,该模型能够以很高的精确度识别我们日 ...

  7. 从代码设计到应用开发,入坑深度学习看这本书就够了

    深度学习(Deep Learning)是机器学习中一种基于对数据进行表征学习的方法.近年来,深度学习已经在科技界.工业界日益广泛地应用.随着全球各领域多样化数据的极速积累和计算资源的成熟化商业服务,深 ...

  8. 想转行人工智能?哈佛博士后有话说!

    从17年开始,各大高校的数据科学与大数据技术专业持续火爆,2018年,北京大学.西安交通大学等高校更在本科阶段设立人工智能一级学科,中国顶尖人才的流向在悄然改变-- 据目前最新的数据显示,AI行业开发 ...

  9. 2019,转行人工智能?机会来了!

    最近有个脑筋急转弯,很有意思. 问:移动支付时代,口香糖的销量为什么急速下滑? 答案:从前购物用现金支付,大家结账时为了凑整,总爱顺手拿只口香糖.口香糖价格便宜,也挺实用,所以常常摆在收银台上. 如今 ...

最新文章

  1. 创建ContentProvider的详细步骤
  2. CentOS7-64bit 编译 Hadoop-2.5.0,并分布式安装
  3. 如何 0 改造,让单体/微服务应用成为 Serverless Application
  4. python ==》 内置函数
  5. vivadoRAM中初始化文件coe如何快速生成
  6. 树状数组萌新讲解+基础习题【一点一滴】
  7. rpm安装mysql
  8. JavaScript初级学习笔记(待完成)
  9. 《Kotlin从零到精通Android开发》面世啦
  10. 梦幻可以在2个服务器无限转吗,梦幻西游手游:从iOS到双平台,转区竟然能转成了全服第一?...
  11. 如何绘制业务流程图?
  12. python字符串与十六进制互相转换
  13. 微信小程序界面设计入门课程-样式wxss中使用css课程-文本-direction 文本方向
  14. linux设备授权命令,# Linux命令
  15. npm介绍与cnpm介绍
  16. 前端导出word实现方法
  17. java程序从键盘输入十个整数存入数组a中_并编程实现:_2.用指针方式编写程序:从键盘输入10个整数,并存入数组,要求将10个书中的最大数与第一个输入的数交...
  18. STL系列之十 全排列(百度迅雷笔试题)
  19. C 王小二切饼 SDUT
  20. 全球牛油果市场处于不断增长状态,墨西哥已成为世界上最大的牛油果生产国与出口国[图]

热门文章

  1. 建武28a对讲机最大距离_健伍TH-26A,TG-28A,TH-28A和TK208对讲机检修实例说明
  2. word2013怎么去掉所有文字下面的波浪线
  3. DNS中cname记录的作用
  4. NGS数据分析实践:03. 涉及的常用数据格式[4] - bed和Wiggle/Bigwig/bedgraph格式
  5. 【科普】1分钟帮你搞懂机械硬盘和固态硬盘
  6. 「镁客早报」苹果将在德国停售iPhone 7&8;“刘强东事件”正式结案...
  7. 【微信开发第一章】SpringBoot实现微信公众号创建菜单,同步菜单功能
  8. 华硕发布全球首款8G内存手机ZenFone VR
  9. 京东数据分析软件 | 如何获取京东平台的“增长榜单”
  10. 在线生成ascii字符画网站字符图案在线生成工具