文本分类——预处理
前言: 文本分类,NLP领域比较经典的使用场景;文本分类一般分为:特征工程+分类器+结果评价与反馈。 特征工程分为:文本预处理+特征提取+文本表示。 本文主要是文本预处理,分词——文本标准化——便于对文本的后序操作,再进行词频统计。 一、代码: import nltk from nltk.corpus import stopwords #从nltk语料库中调用停用词语库 from nltk.tokenize import sent_tokenize#从nltk.tokenize中调用sen_tokenize函数实例来将短文分成句子 def read_file(filename): """ #读取文档内容 #:param filename:文档名称 #:return: 文本数据字符串 """ with open(filename,'r',encoding='UTF-8') as obj_file:#不加encoding='UTF-8'有时会出现UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 11: illeg contents = obj_file.read()#从文本中读取数据存到变量contents中,字符串 return contents def del_stop_words(pre