sunlandbell1 2016-06-15 09:50 采纳率: 0%
浏览 9091

python 中文分词 去停用词问题

本人菜鸟,要对lon文件夹下的20个txt文档进行中文分词,且去停用词,停用词表stopword.txt,运行结果并没有去除停用词,求大神解答代码如下
#encoding=utf-8

import sys

import re

import codecs

import os

import shutil

import jieba

import jieba.analyse

#导入自定义词典

#jieba.load_userdict("dict_baidu.txt")

stopwords = {}.fromkeys([ line.rstrip() for line in open('stopword.txt') ])
#Read file and cut

def read_file_cut():

#create path

path = "lon\"

respath = "lon_Result\"

if os.path.isdir(respath):

shutil.rmtree(respath, True)

os.makedirs(respath)

num = 1  
while num<=20:  
    name = "%d" % num   
    fileName = path + str(name) + ".txt"  
    resName = respath + str(name) + ".txt"  
    source = open(fileName, 'r')  
    if os.path.exists(resName):  
        os.remove(resName)  
    result = codecs.open(resName, 'w', 'utf-8')  
    line = source.readline()  
    line = line.rstrip('\n')  

    while line!="":  
        line = unicode(line, "utf-8")  
        seglist = jieba.cut(line,cut_all=False)  #精确模式  
        output = ' '.join(list(seglist))         #空格拼接  
        for seg in seglist:
            seg=seg.encode('gbk')
            if seg not in stopwords:
                    output+=seg
                    print output
        result.write(output + '\r\n')
        line = source.readline()  
    else:  
        print 'End file: ' + str(num)  
        source.close()  
        result.close()  
    num = num + 1  
else:  
    print 'End All'  

#Run function

if name == '__main__':

read_file_cut()

  • 写回答

1条回答 默认 最新

  • qq_34633970 2016-11-08 11:29
    关注

    path="lon\"
    两个\
    你现在这个结果跑出来没?我也是在网上找的和你相同代码,也是没有结果,希望交流下Q1814693998

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛