资源简介
实现文本分类的主要包括几个步骤文本分词处理,特征选择,特征权重计算,文本特征向量表示,基于训练文本的特征向量数据训练SVM模型,对于测试集进行特征向量表示代入训练得到的svm模型中进行预测分类,达到93%的准确率
代码片段和文件信息
__author__ = ‘ShadowWalker‘
import codecs
import math
import sys
# 使用开方检验选择特征
# 按UTF-8编码格式读取文件
# 定义停止词
def ignore(s):
return s == ‘nbsp‘ or s == ‘ ‘ or s == ‘ ‘ or s == ‘/t‘ or s == ‘/n‘ \
or s == ‘,‘ or s == ‘。‘ or s == ‘!‘ or s == ‘、‘ or s == ‘―‘\
or s == ‘?‘ or s == ‘@‘ or s == ‘:‘ \
or s == ‘#‘ or s == ‘%‘ or s == ‘&‘ \
or s == ‘(‘ or s == ‘)‘ or s == ‘《‘ or s == ‘》‘ \
or s == ‘[‘ or s == ‘]‘ or s == ‘{‘ or s == ‘}‘ \
or s == ‘*‘ or s == ‘‘ or s == ‘.‘ or s == ‘&‘ \
or s == ‘!‘ or s == ‘?‘ or s == ‘:‘ or s == ‘;‘\
or s == ‘-‘ or s == ‘&‘\
or s == ‘<‘ or s == ‘>‘ or s == ‘(‘ or s == ‘)‘ \
or s == ‘[‘ or s == ‘]‘ or s == ‘{‘ or s == ‘}‘ or s == ‘nbsp10‘ or s == ‘3.6‘ or s==‘about‘ or s ==‘there‘ \
or s == “see“ or s == “can“ or s == “U“ or s == “L“ or s == “ “ or s == “in“ or s ==“;“ or s ==“a“ or s ==“0144“\
or s == “\n“ or s == “our“
# print(stopwords)
# 对卡方检验所需的 a b c d 进行计算
# a:在这个分类下包含这个词的文档数量
# b:不在该分类下包含这个词的文档数量
# c:在这个分类下不包含这个词的文档数量
# d:不在该分类下,且不包含这个词的文档数量
#
ClassCode = [‘C000007‘ ‘C000008‘ ‘C000010‘ ‘C000013‘ ‘C000014‘ ‘C000016‘ ‘C000020‘ ‘C000022‘ ‘C000023‘ ‘C000024‘]
# 构建每个类别的词Set
# 分词后的文件路径
# textCutbasePath = “G:\\ChineseTextClassify\\SogouCCut\\“
textCutbasePath = sys.path[0] + “\\SogouCCut\\“
# 构建每个类别的词向量
def buildItemSets(classDocCount):
termDic = dict()
# 每个类别下的文档集合用list表示 每个set表示一个文档,整体用一个dict表示
termClassDic = dict()
for eachclass in ClassCode:
currClassPath = textCutbasePath+eachclass+“\\“
eachClassWordSets = set()
eachClassWordList = list()
for i in range(classDocCount):
eachDocPath = currClassPath+str(i)+“.cut“
eachFileObj = open(eachDocPath ‘r‘)
eachFileContent = eachFileObj.read()
eachFileWords = eachFileContent.split(“ “)
eachFileSet = set()
for eachword in eachFileWords:
# 判断是否是停止词
stripeachword = eachword.strip(“ “)
if not ignore(eachword) and len(stripeachword) > 0:
eachFileSet.add(eachword)
eachClassWordSets.add(eachword)
eachClassWordList.append(eachFileSet)
# print(eachFileSet)
termDic[eachclass] = eachClassWordSets
termClassDic[eachclass] = eachClassWordList
return termDic termClassDic
# 对得到的两个词典进行计算,可以得到a b c d 值
# K 为每个类别选取的特征个数
# 卡方计算公式
def ChiCalc(a b c d):
result = float(pow((a*d - b*c) 2)) /float((a+c) * (a+b) * (b+d) * (c+d))
return result
def featureSelection(termDic termClassDic K):
termCountDic = dict()
for key in termDic:
classWordSets = termDic[key]
classTermCountDic = dict()
for eachword in classWordSets: # 对某个类别下的每一个单词的 a b c d 进行计算
a = 0
b = 0
c = 0
d = 0
for eachclass in termClassD
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-07-31 08:48 ChineseTextClassify-master\
目录 0 2016-07-31 08:48 ChineseTextClassify-master\.idea\
文件 19 2016-07-31 08:48 ChineseTextClassify-master\.idea\.name
文件 284 2016-07-31 08:48 ChineseTextClassify-master\.idea\ChineseTextClassify.iml
文件 164 2016-07-31 08:48 ChineseTextClassify-master\.idea\encodings.xm
文件 215 2016-07-31 08:48 ChineseTextClassify-master\.idea\misc.xm
文件 290 2016-07-31 08:48 ChineseTextClassify-master\.idea\modules.xm
目录 0 2016-07-31 08:48 ChineseTextClassify-master\.idea\scopes\
文件 139 2016-07-31 08:48 ChineseTextClassify-master\.idea\scopes\scope_settings.xm
文件 164 2016-07-31 08:48 ChineseTextClassify-master\.idea\vcs.xm
文件 34607 2016-07-31 08:48 ChineseTextClassify-master\.idea\workspace.xm
目录 0 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\
文件 1178 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\ChineseCut.py
目录 0 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\
文件 341175 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\pku_test.txt
文件 549918 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\pku_test_gold.txt
文件 443710 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\pku_test_result.txt
文件 5887805 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\pku_training.txt
文件 347101 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\pku_training_words.txt
文件 2196634 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\score.txt
目录 0 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\sc
文件 3543 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\sc
文件 7225 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PKU_GB\sc
文件 6926 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\PreHMM.py
文件 6098 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\Viterbi.py
文件 277953 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\emit.txt
文件 303 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\tran.txt
文件 14092 2016-07-31 08:48 ChineseTextClassify-master\ChineseSegmentation\worddict.txt
文件 5522 2016-07-31 08:48 ChineseTextClassify-master\FeatureSelecion.py
文件 3518 2016-07-31 08:48 ChineseTextClassify-master\FeatureWeight.py
目录 0 2016-07-31 08:48 ChineseTextClassify-master\LIBSVM\
............此处省略6047个文件信息
相关资源
- 复旦中文文本分类-训练集
- 搜狗最新文本分类语料库
- imdb电影情感分类数据集
- 复旦新闻语料库测试集
- 中文文本分类语料复旦完整版
- 复旦大学中文文本分类数据集
- 新闻文本分类数据集50000条
- 中文文本分类语料库复旦大学训练与
- 新闻分类文本分类
- 复旦大学中文文本分类数据集-训练
- 中文文本分类语料复旦-训练集
- 喜悦、愤怒、厌恶、低落微博带标注
- 复旦文本分类语料数据集 包含训练集
- 中文文本分类语料复旦-测试集
- 复旦大学文本分类新闻语料测试集+训
- 搜狐2012新闻语料已分类,utf8格式
- 新闻类中文文本分类数据集
- 中文文本分类语料复旦训练集+测试集
- 面向大数据的高效能垃圾文本分类
- 基于LDA主题模型的短文本分类方法_张
- 基于LDA高频词扩展的中文短文本分类
- 基于LDA 主题模型的短文本分类方法
- 完整用CNN(Tensorflow)完成文本分类的
- 文本分类时所用的中英文停用词表
- 北邮计算机研一《数据挖掘》文本分
- sogou中文语料库
- 论文研究-基于不同文本表示协同训练
- SVM和lda结合的文本分类
- 基于n-gram的文本分类
- 知乎文本分类竞赛 数据集1/2
评论
共有 条评论