
- Python 数据科学教程
- Python 数据科学 - 首页
- Python 数据科学 - 入门
- Python 数据科学 - 环境设置
- Python 数据科学 - Pandas
- Python 数据科学 - Numpy
- Python 数据科学 - SciPy
- Python 数据科学 - Matplotlib
- Python 数据处理
- Python 数据操作
- Python 数据清洗
- Python 处理 CSV 数据
- Python 处理 JSON 数据
- Python 处理 XLS 数据
- Python 关系型数据库
- Python NoSQL 数据库
- Python 日期和时间
- Python 数据整理
- Python 数据聚合
- Python 读取 HTML 页面
- Python 处理非结构化数据
- Python 词汇标记化
- Python 词干提取和词形还原
- Python 数据可视化
- Python 图表属性
- Python 图表样式
- Python 箱线图
- Python 热力图
- Python 散点图
- Python 气泡图
- Python 3D 图表
- Python 时间序列
- Python 地理数据
- Python 图数据
Python 词汇标记化
词汇标记化是将大段文本分割成单词的过程。这是自然语言处理任务中的一个要求,其中需要捕获每个单词并进行进一步分析,例如对特定情感进行分类和计数等。自然语言工具包 (NLTK) 是用于实现此目的的库。在继续进行 Python 词汇标记化程序之前,请安装 NLTK。
conda install -c anaconda nltk
接下来,我们使用word_tokenize方法将段落分割成单个单词。
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)
执行上述代码时,会产生以下结果。
['It', 'originated', 'from', 'the', 'idea', 'that', 'there', 'are', 'readers', 'who', 'prefer', 'learning', 'new', 'skills', 'from', 'the', 'comforts', 'of', 'their', 'drawing', 'rooms']
句子标记化
我们还可以像标记化单词一样标记化段落中的句子。我们使用sent_tokenize方法来实现此目的。下面是一个例子。
import nltk sentence_data = "Sun rises in the east. Sun sets in the west." nltk_tokens = nltk.sent_tokenize(sentence_data) print (nltk_tokens)
执行上述代码时,会产生以下结果。
['Sun rises in the east.', 'Sun sets in the west.']
广告