Python下中文预处理

本文介绍了在Python环境下进行中文文本预处理的步骤,包括获取原始文本、中文分词、去除停用词、处理全半角字符,以及解决Python在Windows下的中文解码问题。
摘要由CSDN通过智能技术生成

一 得到原始文本内容

    def FileRead(self,filePath):
        f = open(filePath)
        raw=f.read()
        return raw

二 中文分词

参考之前的一篇博客Python下的中文分词实现

def NlpirTokener(self,raw):
        result=''
        tokens = nlpir.Seg(raw)
        for w in tokens:
#           result+= w[0]+"/"+w[1] #加词性标注
            result+= w[0] +'/'#加词性标注
        return result
    def JiebaTokener(self,raw):
        result=''
        words = pseg.cut(raw) #进行分词
        result=""  #记录最终结果的变量
        for w in words:
#            result+= str(w.word)+"/"+str(w.flag) #加词性标注
             result+= str(w.word)+"/" #加词
        return result

三 去停用词

  def StopwordsRm(self,words):
        result=''
        print words
        wordList=[word for word in words.split('#')]
        print wordList[:20]
        stopwords = {}.fromkeys([ line.rstrip()for line in open(conf.PreConfig.CHSTOPWORDS)])
        cleanTokens= [w for w in wordList ifw not in stopwords]
        print cleanTokens[:20]
        for c in cleanTokens:
            result+=c+"#"
        print result
       returnresult

在这个地方我遇到了一个很烦人的问题,那就是Python的中文解码问题,在最开始的一个小时里我在在去停用词之后一直看到的结果是这样的:


\xe3\x80\x90/\xe6\x97\xa5\xe6\x9c\x9f/\xe3\这种东西没说的肯定是解码造成的,于是开始找解决的方法。

后来找到CSDN上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值