- Python - 文本处理
- Python - 文本处理入门
- Python - 文本处理环境
- Python - 字符串不变性
- Python - 排序行
- Python - 段落重新格式化
- Python - 统计段落中的词元
- Python - 二进制ASCII转换
- Python - 字符串作为文件
- Python - 反向读取文件
- Python - 过滤重复单词
- Python - 从文本中提取电子邮件
- Python - 从文本中提取URL
- Python - 美化打印
- Python - 文本处理状态机
- Python - 首字母大写和翻译
- Python - 分词
- Python - 去除停用词
- Python - 同义词和反义词
- Python - 文本翻译
- Python - 单词替换
- Python - 拼写检查
- Python - WordNet 接口
- Python - 语料库访问
- Python - 词性标注
- Python - 组块和组块间隙
- Python - 组块分类
- Python - 文本分类
- Python - 二元语法
- Python - 处理PDF
- Python - 处理Word文档
- Python - 读取RSS feed
- Python - 情感分析
- Python - 搜索和匹配
- Python - 文本处理
- Python - 文本换行
- Python - 频率分布
- Python - 文本摘要
- Python - 词干提取算法
- Python - 受限搜索
Python - 文本分类
很多时候,我们需要根据一些预定义的标准将可用的文本分类到不同的类别中。NLTK 提供了作为各种语料库一部分的此类功能。在下面的示例中,我们查看电影评论语料库并检查可用的分类。
# Lets See how the movies are classified
from nltk.corpus import movie_reviews
all_cats = []
for w in movie_reviews.categories():
all_cats.append(w.lower())
print(all_cats)
运行上述程序后,我们将得到以下输出:
['neg', 'pos']
现在让我们来看一下包含正面评价的文件之一的内容。此文件中的句子已被分词,我们打印前四句以查看示例。
from nltk.corpus import movie_reviews
from nltk.tokenize import sent_tokenize
fields = movie_reviews.fileids()
sample = movie_reviews.raw("pos/cv944_13521.txt")
token = sent_tokenize(sample)
for lines in range(4):
print(token[lines])
运行上述程序后,我们将得到以下输出:
meteor threat set to blow away all volcanoes & twisters ! summer is here again ! this season could probably be the most ambitious = season this decade with hollywood churning out films like deep impact , = godzilla , the x-files , armageddon , the truman show , all of which has but = one main aim , to rock the box office . leading the pack this summer is = deep impact , one of the first few film releases from the = spielberg-katzenberg-geffen's dreamworks production company .
接下来,我们对这些文件中的每个单词进行分词,并使用 nltk 中的 FreqDist 函数查找最常见的单词。
import nltk
from nltk.corpus import movie_reviews
fields = movie_reviews.fileids()
all_words = []
for w in movie_reviews.words():
all_words.append(w.lower())
all_words = nltk.FreqDist(all_words)
print(all_words.most_common(10))
运行上述程序后,我们将得到以下输出:
[(,', 77717), (the', 76529), (.', 65876), (a', 38106), (and', 35576), (of', 34123), (to', 31937), (u"'", 30585), (is', 25195), (in', 21822)]
广告