Python数据科学教程:NLTK单词标记化(自然语言处理任务)

2018-09-3009:16:42后端程序开发Comments2,513 views字数 823阅读模式

单词标记是将大量文本样本分解为单词的过程。 这是自然语言处理任务中的一项要求,每个单词需要被捕获并进行进一步的分析,如对特定情感进行分类和计数等。自然语言工具包(NLTK)是用于实现这一目的的库。 在继续使用python程序进行字词标记之前,先安装NLTK。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6044.html

conda install -c anaconda nltk

接下来,使用word_tokenize方法将段落拆分为单个单词。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6044.html

import nltk

word_data = "It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms"
nltk_tokens = nltk.word_tokenize(word_data)
print (nltk_tokens)
Python

当我们执行上面的代码时,它会产生以下结果。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6044.html

['It', 'originated', 'from', 'the', 'idea', 'that', 'there', 'are', 'readers', 
'who', 'prefer', 'learning', 'new', 'skills', 'from', 'the',
'comforts', 'of', 'their', 'drawing', 'rooms']
Python

标记句子

还可以在段落中标记句子,就像标记单词一样。使用send_tokenize方法来实现这一点。 下面是一个例子。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6044.html

import nltk
sentence_data = "Sun rises in the east. Sun sets in the west."
nltk_tokens = nltk.sent_tokenize(sentence_data)
print (nltk_tokens)
Python

当我们执行上面的代码时,它会产生以下结果。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6044.html

['Sun rises in the east.', 'Sun sets in the west.']
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6044.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/6044.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定