python 语料处理(从文件夹中读取文件夹中文件,分词,去停用词,去单个字)
# -*- coding:utf8 -*-
import os
import jieba def splitSentence(inputFile):
fin = open(inputFile, 'r') #以读的方式打开文件
global fout #以写得方式打开文件
#print fin
global stop
for eachLine in fin:
#print eachLine
line = eachLine.strip()#.decode('utf-8', 'ignore') #去除每行首尾可能出现的空格,并转为Unicode进行处理
line=line.strip('\n') #去掉多余空行
wordList = list(jieba.cut(line)) #用结巴分词,对每行内容进行分词
#wordList = list(jieba.cut_for_search(line)) outStr = ''
for word in wordList:#
if len(word)>1:
if not word in stop:
outStr += word
outStr += ' ' fout.write(outStr.strip().encode('utf-8')) #将分词好的结果写入到输出文件
fout.write('\n')
fin.close() #path=r'/media/软件/zhuomian/VARandLDAr/train'
#r'D:/zhuomian/VARandLDA/train'
path='/home/xdj/train'
fns=[os.path.join(root,fn) for root,dirs,files in os.walk(path) for fn in files]
stop = [line.strip().decode('utf-8', 'ignore') for line in open('/home/xdj/chstop.txt').readlines()]
fout = open('myOutput.txt', 'w')
fout.write('%d' %len(fns)+'\n') for f in fns:
splitSentence(f) #splitSentence('/home/xdj/train/C3-Art/C3-Art1459.txt', 'myOutput.txt')
print(len(fns))
fout.close()
python 语料处理(从文件夹中读取文件夹中文件,分词,去停用词,去单个字)的更多相关文章
- python使用jieba实现中文文档分词和去停用词
分词工具的选择: 现在对于中文分词,分词工具有很多种,比如说:jieba分词.thulac.SnowNLP等.在这篇文档中,笔者使用的jieba分词,并且基于python3环境,选择jieba分词的理 ...
- python模块:xlsxwriter和xlrd相结合读取、写入excel文件
python模块简单说明: xlsxwriter:负责写入数据 xlrd:负责读取数据 xlsxwriter 官方文档:http://xlsxwriter.readthedocs.org 本实例是刚写 ...
- IDEA中读取 resource目录下文件
1. 资源文件 2. 加载文件 public void test() { try { System.out.println("begin test"); String filepa ...
- Kafka消费者 从Kafka中读取数据并写入文件
Kafka消费者 从Kafka中读取数据 最近有需求要从kafak上消费读取实时数据,并将数据中的key输出到文件中,用于发布端的原始点进行比对,以此来确定是否传输过程中有遗漏数据. 不废话,直接上代 ...
- 在spark udf中读取hdfs上的文件
某些场景下,我们在写UDF实现业务逻辑时候,可能需要去读取某个文件. 我们可以将此文件上传个hdfs某个路径下,然后通过hdfs api读取该文件,但是需要注意: UDF中读取文件部分最好放在静态代码 ...
- SpringBoot在logback.xml中读取application.properties中配置的日志路径
1.在springboot项目中使用logback记录日志,在logback.xml中配置日志存储位置时读取application.properties中配置的路径,在 logback.xml中配置引 ...
- winform中读取App.config中数据连接字符串
1.首先要在工程引用中导入System.Configuration.dll文件的引用. 2.通过System.Configuration.ConfigurationManager.Connection ...
- python调用jieba(结巴)分词 加入自定义词典和去停用词功能
把语料从数据库提取出来以后就要进行分词啦,我是在linux环境下做的,先把jieba安装好,然后找到内容是build jieba PKG-INFO setup.py test的那个文件夹(我这边是ji ...
- 一篇文章告诉你Python接口自动化测试中读取Text,Excel,Yaml文件的方法
前言 不管是做Ui自动化和接口自动,代码和数据要分离,会用到Text,Excel,Yaml.今天讲讲如何读取文件数据 Python也可以读取ini文件,传送门 记住一点:测试的数据是不能写死在代码里面 ...
随机推荐
- windows安装python
1:首先去python网站下载安装包:https://www.python.org/downloads/,注意自己的系统版本 2:在自己指定目录安装即可: 3:将python路径加入PATH环境变量: ...
- clear both
原文地址:http://www.codefans.net/articles/653.shtml 因CSS很多布局是需要浮动的,当属性设置float(浮动)时,其所在的物理位置已经脱离文档流了,为了使f ...
- Color the ball(线段树)
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- Spring Collections XML 配置
List <property name="lists"> <list> <value>1</value> <ref bean= ...
- java-数字类
浏览以下内容前,请点击并阅读 声明 对于6种基本的数字类型,java提供了相对应的类.Number类和六种细分的子类.
- hiveserver2
hiveserver2 默认绑定了ip:localhost 和 port:10000 !connect jdbc:hive2://localhost:10000 org.apache.hive.jd ...
- Redis内存缓存系统入门
网站:http://redis.io/ key-value cache and store data structure server 1. 服务器端 1.1 安装 下载安装包:http://r ...
- BZOJ 2527 & 整体二分
Byteotian Interstellar Union有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨 ...
- Codeforces Round #243 (Div. 2) B. Sereja and Mirroring
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
- [Leetcode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...