在进行文本分类之前,需要对文本进行预处理。中文文本和英文文本预处理的方式有所差别。
(1)英文文本预处理
文本预处理过程大致分为以下几点:
1、英文缩写替换
预处理过程中需要把英文缩写进行替换,如it’s和it is是等价的,won’t和will not也是等价的,等等。

text = "The story loses its bite in a last-minute happy ending that's even less plausible than the rest of the picture ."text.replace("that's", "that is")

‘The story loses its bite in a last-minute happy ending that is even less plausible than the rest of the picture .’
2、转换为小写字母
文本中英文有大小写之分,例如"Like"和"like",这两个字符串是不同的,但在统计单词时希望把它们认为是相同的单词。

text = "This is a film well worth seeing , talking and singing heads and all ."text.lower()

‘this is a film well worth seeing , talking and singing heads and all .’
3、删除标点符号、数字及其它特殊字符
标点符号、数字及其它特殊字符对文本分类不起作用,把它们删除可以减少特征的维度。一般用正则化方法删除这些字符。

import retext = "disney has always been hit-or-miss when bringing beloved kids' books to the screen . . . tuck everlasting is a little of both ."
text = re.sub("[^a-zA-Z]", " ", text)# 删除多余的空格
' '.join(text.split())

‘disney has always been hit or miss when bringing beloved kids books to the screen tuck everlasting is a little of both’
4、分词
英文文本的分词和中文文本的分词方法不同,英文文本分词方法可以根据所提供的文本进行选择,如果文本中单词和标点符号或者其它字符是以空格隔开的,例如"a little of both .",那么可以直接使用split()方法;如果文本中单词和标点符号没有用空格隔开,例如"a little of both.",可以使用nltk库中的word_tokenize()方法。nltk库安装也比较简单,在windows下,用pip install nltk进行安装即可。

# 单词和标点符号用空格隔开
text = "part of the charm of satin rouge is that it avoids the obvious with humour and lightness ."
text.split()

[‘part’, ‘of’, ‘the’, ‘charm’, ‘of’, ‘satin’, ‘rouge’, ‘is’, ‘that’, ‘it’, ‘avoids’, ‘the’, ‘obvious’, ‘with’, ‘humour’, ‘and’, ‘lightness’, ‘.’]

# 单词和标点符号没有用空格隔开
from nltk.tokenize import word_tokenizetext = "part of the charm of satin rouge is that it avoids the obvious with humour and lightness."
word_tokenize(text)

[‘part’, ‘of’, ‘the’, ‘charm’, ‘of’, ‘satin’, ‘rouge’, ‘is’, ‘that’, ‘it’, ‘avoids’, ‘the’, ‘obvious’, ‘with’, ‘humour’, ‘and’, ‘lightness’, ‘.’]
5、拼写检查
由于英文文本中可能有拼写错误,因此一般需要进行拼写检查。如果确信分析的文本没有拼写错误,可以略去此步。拼写检查,一般用pyenchant类库完成。

from enchant.checker import SpellChecker
chkr = SpellChecker("en_US")
chkr.set_text("Many peope likee to watch in the Name of People.")
for err in chkr:print("ERROR:", err.word)

ERROR: peope
ERROR: likee
6、词干提取和词形还原
词干提取(stemming)和词型还原(lemmatization)是英文文本预处理的特色。两者其实有共同点,即都是要找到词的原始形式。只不过词干提取(stemming)会更加激进一点,它在寻找词干的时候可以会得到不是词的词干。比如"imaging"的词干可能得到的是"imag", 并不是一个词。而词形还原则保守一些,它一般只对能够还原成一个正确的词的词进行处理。在nltk中,做词干提取的方法有PorterStemmer,LancasterStemmer和SnowballStemmer。推荐使用SnowballStemmer。这个类可以处理很多种语言,当然,除了中文。

from nltk.stem.porter import PorterStemmer
stem_porter = PorterStemmer()
stem_porter.stem('countries')  # 输出countri
from nltk.stem import SnowballStemmer
stemmer = SnowballStemmer("english")
stemmer.stem("countries")  # 输出countri
from nltk.stem.wordnet import WordNetLemmatizer
stem_wordnet = WordNetLemmatizer()
stem_wordnet.lemmatize('countries')  # 输出country

7、删除停用词
停用词对文本分类没什么影响,比如’a’,‘is’,‘of’。nltk库中自带英文停用词。

from nltk.corpus import stopwordsstop_words = stopwords.words("english")text = "part of the charm of satin rouge is that it avoids the obvious with humour and lightness"words = [w for w in text.split() if w not in stop_words]
' '.join(words)

‘part charm satin rouge avoids obvious humour lightness’
下面我使用传统的机器学习方法来实现简单的英文文本分类任务,使用情感分类数据集,是个二分类数据集,标签为positive或者negative,数据集可以从网上下载或者发给你。分类任务过程如下:首先进行文本预处理,然后提取TFIDF特征,最后利用传统的机器学习方法逻辑回归对文本进行分类。我假设文本中没有拼写错误,另外模型简单,分类效果一般,需要进一步优化。有什么不对或者遗漏之处,望各位不吝指教,谢谢。

import numpy as np
import os
import re
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from sklearn.linear_model import LogisticRegression
import time
import warningswarnings.filterwarnings('ignore')path_neg = './polarity_data/rt-polarity.neg'
path_pos = './polarity_data/rt-polarity.pos'# 文本预处理
def text_preprocessing(path):# 读取数据text = []with open(path, 'r', encoding='utf-8', errors='ignore') as f:for line in f.readlines():text.append(line.strip())# 英文缩写替换text_abbreviation = []for item in text:item = item.lower().replace("it's", "it is").replace("i'm", "i am").replace("he's", "he is").replace("she's", "she is")\.replace("we're", "we are").replace("they're", "they are").replace("you're", "you are").replace("that's", "that is")\.replace("this's", "this is").replace("can't", "can not").replace("don't", "do not").replace("doesn't", "does not")\.replace("we've", "we have").replace("i've", " i have").replace("isn't", "is not").replace("won't", "will not")\.replace("hasn't", "has not").replace("wasn't", "was not").replace("weren't", "were not").replace("let's", "let us")text_abbreviation.append(item)# 删除标点符号、数字等其他字符text_clear_str = []for item in text_abbreviation:item = re.sub("[^a-zA-Z]", " ", item)text_clear_str.append(' '.join(item.split()))text_clear_str_stem_del_stopwords = []stem_porter = PorterStemmer()  # 词形归一化stop_words = stopwords.words("english")  # 停用词# 分词、词形归一化、删除停用词for item in text_clear_str:words_token = word_tokenize(item)  # 分词words = [stem_porter.stem(w) for w in words_token if w not in stop_words]text_clear_str_stem_del_stopwords.append(' '.join(words))return text_clear_str_stem_del_stopwordsstart_time1 = time.clock()text_neg = text_preprocessing(path_neg)
text_pos = text_preprocessing(path_pos)end_time1 = time.clock()print("the time of text preprocessing is %.2f s" % (end_time1 - start_time1))text = text_neg + text_pos# 特征提取
def features_extraction(text):vectors = TfidfVectorizer()features = vectors.fit_transform(text).todense()return featuresstart_time2 = time.clock()
features = features_extraction(text)
end_time2 = time.clock()print("the time of features extracting is %.2f s" % (end_time2 - start_time2))
print('.'*40)m = len(text_neg)
n = len(text_pos)# 标签
labels = np.vstack((np.zeros((m, 1)), np.ones((n, 1))))
# 数据集
data = np.hstack((features, labels))
# 数据集随机打乱
np.random.shuffle(data)# 样本特征
features = data[:, :-1]
# 样本标签
labels = data[:, -1]print(features.shape)
print(labels.shape)# 训练集、测试集划分
x_train, x_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)# 逻辑回归
start_time3 = time.clock()
lr = LogisticRegression().fit(x_train, y_train)
end_time3 = time.clock()# 训练时间
print("the time of LogisicRegression training is %.2f s" % (end_time3 - start_time3))y_pred = lr.predict(x_test)print("the f1_score of LogisicRegression is %.4f" % f1_score(y_test, y_pred))

(2)中文文本预处理
1、删除标点符号、数字及其它特殊字符

import retext = '最后是12月12日《篮球先锋报》的新闻报道“湖”涂开营。到底是保罗还是霍华德,\
湖人转了一圈之后终于发现,三巨头不切实际,而霍华德也比保罗更符合湖人构建王朝的要求。\
于是,湖人放弃追逐保罗,又把奥多姆送去小牛,目的就是抢魔兽。这一次,他们能如愿吗?标签:$LOTOzf$'text = re.sub("[0-9《》“”‘’。、,?!——$¥#@%……&*^()a-zA-Z<>;:/]", "", text)
print(text)

‘最后是月日篮球先锋报的新闻报道湖涂开营到底是保罗还是霍华德湖人转了一圈之后终于发现三巨头不切实际而霍华德也比保罗更符合湖人构建王朝的要求于是湖人放弃追逐保罗又把奥多姆送去小牛目的就是抢魔兽这一次他们能如愿吗标签’

2、jieba分词
可使用 jieba.cut 和 jieba.cut_for_search 方法进行分词,两者所返回的结构都是一个可迭代的 generator,可使用 for 循环来获得分词后得到的每一个词语(unicode),或者直接使用 jieba.lcut 以及 jieba.lcut_for_search 直接返回 list。其中:jieba.cut 和 jieba.lcut 接受 3 个参数:
sentence:需要分词的字符串(unicode 或 UTF-8 字符串、GBK 字符串)
cut_all 参数:是否使用全模式,默认值为 False
HMM 参数:用来控制是否使用 HMM 模型,默认值为 True

jieba.cut_for_search 和 jieba.lcut_for_search 接受 2 个参数:
sentence:需要分词的字符串(unicode 或 UTF-8 字符串、GBK 字符串)
HMM 参数:用来控制是否使用 HMM 模型,默认值为 True
尽量不要使用 GBK 字符串,可能无法预料地错误解码成 UTF-8

jieba 是目前最好的 Python 中文分词组件,它有以下三种分词模式:
2.1、精准模式

import jiebasentence= '他来到北京大学参加暑期夏令营'# 精准模式
print(list(jieba.cut(sentence, cut_all=False)))

[‘他’, ‘来到’, ‘北京大学’, ‘参加’, ‘暑期’, ‘夏令营’]

2.2、全模式

sentence= '他来到北京大学参加暑期夏令营'# 全模式
print(list(jieba.cut(sentence, cut_all=True)))

[‘他’, ‘来到’, ‘北京’, ‘北京大学’, ‘大学’, ‘参加’, ‘暑期’, ‘夏令’, ‘夏令营’]

2.3、搜索引擎模式

sentence = '他毕业于北京大学机电系,后来在一机部上海电器科学研究所工作'# 搜索引擎模式
print(list(jieba.cut_for_search(sentence)))

[‘他’, ‘毕业’, ‘于’, ‘北京’, ‘大学’, ‘北京大学’, ‘机电’, ‘系’, ‘,’, ‘后来’, ‘在’, ‘一机部’, ‘上海’, ‘电器’, ‘科学’, ‘研究’, ‘研究所’, ‘工作’]

3、删除停用词
首先需要在网上下载中文的停用词表或者自己制作停用词表,也可以下载好停用词表,然后加上一些其它的停用词。

import jieba
import re# 停用词
stop_words = ['于', '后来', '在']sentence = '他毕业于北京大学机电系后来在一机部上海电器科学研究所工作'# 分词
sent_cut = list(jieba.cut(sentence))# 删除停用词
text = [w for w in sent_cut if w not in stop_words]print('分词:', sent_cut)
print('删除停用词:', text)

分词: [‘他’, ‘毕业’, ‘于’, ‘北京大学’, ‘机电’, ‘系’, ‘后来’, ‘在’, ‘一机部’, ‘上海’, ‘电器’, ‘科学’, ‘研究所’, ‘工作’]
删除停用词: [‘毕业’, ‘北京大学’, ‘机电’, ‘系’, ‘一机部’, ‘上海’, ‘电器’, ‘科学’, ‘研究所’, ‘工作’]
详细代码可以在我的github上查看https://github.com/LeungFe/Text-preprocessing

文本分类(1)-文本预处理相关推荐

  1. 中文信息处理(五)—— 文本分类与文本表示

    文章目录 1. 文本分类 1.1 文本分类方法 基于传统机器学习的文本分类 1.2 文本分类的一般流程 2. 基于向量空间模型(VSM)的文本表示方法 2.1 one-hot表示 2.2 VSM ① ...

  2. 【文本分类】文本分类案例

    在NLP中,文本分类是一项非常常见的任务,他的目的是将一个文本归结为特定的某个标签.实践过程中可以用各种数据集作为研究的对象,比如哔哩哔哩视频评论.IMDB电影评论.微博评论的个作为数据集,搭建模型来 ...

  3. 【文本分类】文本分类流程及算法原理

    分类体系 分类:给定一个对象,从一个事先定义好的分类体系中挑出一个或多个最适合该对象的类别. 文本分类(TC, Text Categorization):在给定的分类体系下,根据文本内容自动的确定文本 ...

  4. NLP入门之新闻文本分类竞赛——文本分类模型

    一.Word2Vec word2vec模型背后的基本思想是对出现在上下文环境里的词进行预测.对于每一条输入文本,我们选取一个上下文窗口和一个中心词,并基于这个中心词去预测窗口里其他词出现的概率.因此, ...

  5. CNN在文本分类的应用(内有代码实现) 论文Convolutional Neural Networks for Sentence Classification

    一.CNN文本分类简介 文本分类是NLP领域的一个重要子任务,文本分类的目标是自动的将文本打上已经定义好的标签,常见的文本分类任务有: 用户评论的情感识别 垃圾邮件过滤 用户查询意图识别 新闻分类 由 ...

  6. python分类流程_文本分类指南:你真的要错过 Python 吗?

    雷锋网按:本文为雷锋字幕组编译的技术博客,原标题 A Comprehensive Guide to Understand and Implement Text Classification in Py ...

  7. 独家 | 教你用Pytorch建立你的第一个文本分类模型!

    作者:Aravind Pai 翻译:王威力 校对:张一豪 本文约3400字,建议阅读10+分钟 本文介绍了利用Pytorch框架实现文本分类的关键知识点,包括使用如何处理Out of Vocabula ...

  8. 用深度学习(CNN RNN Attention)解决大规模文本分类问题 - 综述和实践

    https://zhuanlan.zhihu.com/p/25928551 近来在同时做一个应用深度学习解决淘宝商品的类目预测问题的项目,恰好硕士毕业时论文题目便是文本分类问题,趁此机会总结下文本分类 ...

  9. ig信息增益 java_文本分类综述

    文本分类是一项系统的工程,所涉及的技术很多,按流程可以将文本分类分为:文本预处理阶段.训练阶段.分类阶段.评价四个阶段,其中预处理阶段要文本处理成计算机能识别的格式,首先对文本进行分词处理,中文文本和 ...

  10. 【NLP】授人以渔:分享我的文本分类经验总结

    在我们做一个项目或业务之前,需要了解为什么要做它,比如为什么要做文本分类?项目开发需要,还是文本类数据值得挖掘. 1.介绍 目前讨论文本分类几乎都是基于深度学习的方法,本质上还是一个建模的过程,包括数 ...

最新文章

  1. 【Python】zip函数
  2. kali换源无法保存_Kali Linux 2.0更新源无法正常使用(解决)
  3. 系统仿真平台SkyEye可替代国外Matlab/Sumlink等同类软件
  4. PYTHON知识梳理
  5. cudnn7.6.5下载 solitairetheme8_233小游戏下载安装赚钱-233小游戏下载安装最新版v2.29.4.5...
  6. EasyUI DataGrid 可编辑列级联操作
  7. P2313 [HNOI2005]汤姆的游戏
  8. MAC 用配置设置解决vscode中文乱码问题
  9. 饿了么ui 下拉框远程搜索 绑定清空原始数据
  10. alias rewrite 后出现404,应设置RewriteBase参数
  11. 解决网页文字无法选中或复制
  12. 支付宝开发流程及注意事项
  13. Numpy 数组切片
  14. shift+delete删除的文件还能恢复吗?可以的!
  15. 【转载】因为专注,所以专业
  16. python9行代码_如何用9行Python代码编写一个简易神经网络
  17. 第三周实验题目2——robots协议
  18. 基于matlab的神经网络实践
  19. 聊聊心理学专业去用户体验研究方向的求职
  20. WinPE下安装ISO格式Win系统

热门文章

  1. 基于消失点的相机自标定(1)
  2. 听说你想用Java去掉或者替换PDF里全部文本?看这
  3. 盛迈坤电商:拼多多开店的注意事项
  4. sci matlab出图出得漂亮,按照sci等期刊、会议论文要求进行matlab和Visio图片保存-补充...
  5. SqlServer数据库使用SQL脚本进行定时备份+异机备份
  6. 微信公众号 :h5获取code,授权等问题
  7. FBI警告:新冠疫情之下,谨防四种加密骗局
  8. 机器学习原来这么有趣!第四章:用深度学习识别人脸
  9. 苹果手机验证码自动填充两次bug
  10. python 同花顺thstrader_GitHub - python8642/THSTrader: 量化交易。同花顺免费模拟炒股软件客户端的python API。(Python3)...