搜狗文本分类语料库-中文文本分类


实现文本分类的主要包括几个步骤文本分词处理,特征选择,特征权重计算,文本特征向量表示,基于训练文本的特征向量数据训练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 == “
“ 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.ideaChineseTextClassify.iml
     文件         164  2016-07-31 08:48  ChineseTextClassify-master.ideaencodings.xml
     文件         215  2016-07-31 08:48  ChineseTextClassify-master.ideamisc.xml
     文件         290  2016-07-31 08:48  ChineseTextClassify-master.ideamodules.xml
     目录           0  2016-07-31 08:48  ChineseTextClassify-master.ideascopes
     文件         139  2016-07-31 08:48  ChineseTextClassify-master.ideascopesscope_settings.xml
     文件         164  2016-07-31 08:48  ChineseTextClassify-master.ideavcs.xml
     文件       34607  2016-07-31 08:48  ChineseTextClassify-master.ideaworkspace.xml
     目录           0  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentation
     文件        1178  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationChineseCut.py
     目录           0  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GB
     文件      341175  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GBpku_test.txt
     文件      549918  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GBpku_test_gold.txt
     文件      443710  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GBpku_test_result.txt
     文件     5887805  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GBpku_training.txt
     文件      347101  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GBpku_training_words.txt
     文件     2196634  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GBscore.txt
     目录           0  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GBscripts
     文件        3543  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GBscriptsmwseg.pl
     文件        7225  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPKU_GBscriptsscore
     文件        6926  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationPreHMM.py
     文件        6098  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationViterbi.py
     文件      277953  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationemit.txt
     文件         303  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentation ran.txt
     文件       14092  2016-07-31 08:48  ChineseTextClassify-masterChineseSegmentationworddict.txt
     文件        5522  2016-07-31 08:48  ChineseTextClassify-masterFeatureSelecion.py
     文件        3518  2016-07-31 08:48  ChineseTextClassify-masterFeatureWeight.py
     目录           0  2016-07-31 08:48  ChineseTextClassify-masterLIBSVM
............此处省略6047个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)