1、要预处理xml文件,解决docs,url,content标签问题。主要代码是deal_label.py 存入数据文件夹是sougou_label_after

SogouCS.reduced:存放原始的txt;
sougou_label_after:解决docs,url,content标签问题
import os
# 预处理xml文件,解决docs,url,content标签问题
def file_fill(file_dir,half_dir):
# 查看half_dir文件夹下的文件夹和文件目录
for root, dirs,files in os.walk(file_dir):
# 判断是否存在half_dir,如果没有则创建
if not os.path.exists(half_dir):
os.makedirs(half_dir)
for f in files:
tmp_dir = half_dir+'\\'+f
text_init_dir = file_dir+'\\'+f
# 遍历文件夹下的每一篇xml文件
with open(text_init_dir, 'r', encoding='gb18030') as source_file:
start,end = '<docs>\n', '</docs>'
line_content = source_file.readlines()
# 在目标文件夹中创建新文件保存预处理后的文件
with open(tmp_dir, 'w+', encoding='utf-8') as handle_file:
# 添加'<docs>'头标签
handle_file.write(start)
for line in line_content:
# 处理url中的‘&’符号
text = line.replace('&', '&amp;')
# 添加'</docs>'头标签
handle_file.write(text)
handle_file.write(end)
if __name__ == '__main__':
file_dir = r'E:\sssss\SogouCS.reduced' #原始文件夹
half_dir = r'E:\sssss\sougou_label_after' # 修改格式和符号生成的文件夹 无需自己手动建文件夹
file_fill(file_dir, half_dir) #将选好的文件进行加上<docs>和</docs>,并修改&这个符号

2、部分样本函数获取,同时随机取去训练数据和测试数据。主要代码是data_train_test.py 存入数据文件夹分别为train_choice , test_choice

import os, random

# 部分样本获取函数
def choice_files(half_dir,choice_dir_train,choice_dir_test,n,m):
if not os.path.exists(choice_dir_train):
os.makedirs(choice_dir_train)
if not os.path.exists(choice_dir_test):
os.makedirs(choice_dir_test) for _,_,files in os.walk(half_dir):
file_list_train = random.sample(files, n) #训练数据去随机的n个文件
for file_choice in file_list_train:
files.remove(file_choice)
file_list_test = random.sample(files, m) #测试数据取随机的m个文件
for file in file_list_train:
with open(half_dir+'\\'+file, 'r', encoding='utf-8') as f1_train:
doc_train = f1_train.read()
path = choice_dir_train+'\\'+file
with open(path, 'w+', encoding='utf-8') as f2_train:
f2_train.write(doc_train)
for file in file_list_test:
with open(half_dir+'\\'+file, 'r', encoding='utf-8')as f1_test:
doc_test = f1_test.read()
path = choice_dir_test +'\\'+file
with open(path, 'w+', encoding='utf-8') as f2_test:
f2_test.write(doc_test)
# print(file_list_train)
# print(file_list_test)
return file_list_train, file_list_test if __name__ == '__main__':
# 随机抽取10个文件进行深加工
half_dir = r'E:\sssss\sougou_label_after'
choice_dir_train = r'E:\sssss\train_choice'
choice_dir_test = r'E:\sssss\test_choice'
file_list_train, file_list_test = choice_files(half_dir, choice_dir_train, choice_dir_test, 10, 5)

3、提取文档文本内容,并且根据url将文本分好类.主要代码是data_content.py 存入数据文件夹是data

import os
from xml.dom import minidom
from urllib.parse import urlparse # 检查url对应的文章是否在分类字典中
def check_class(url_lb,labels):
if url_lb in labels:
return True
return False def file_read(half_dir, labels, path):
for _, _, files in os.walk(half_dir):
for f in files:
filename = half_dir + '\\' + f
doc = minidom.parse(filename) #加载读取xml文件
root = doc.documentElement #获取xml节点属性值
claimtext = root.getElementsByTagName('content') #获取xml节点对象集合
claimurl = root.getElementsByTagName('url')
for ind in range(len(claimurl)):
if claimtext[ind].firstChild == None:
continue
url = urlparse(claimurl[ind].firstChild.data)
url_lb = url.hostname.strip().split('.')[0]
# 建立url和类别的映射字典
if check_class(url_lb, labels):
if not os.path.exists(path):
os.makedirs(path)
if not os.path.exists(path + './' + labels[url_lb]):
os.makedirs(path + './' + labels[url_lb])
file_name = path + './' + labels[url_lb] + './' + "{}.txt".format(labels[url_lb])
with open(file_name, "a+", encoding='utf-8') as file_in:
file_in.write(claimtext[ind].firstChild.data + '\n') if __name__ == '__main__':
labels = {'auto': '汽车', 'it': '互联网', 'health': '健康',
'sports': '体育','travel': '旅游', 'learning': '教育', 'career': '职业',
'cul': '文化','mil': '军事', 'house': '房产', 'yule': '娱乐',
'women': '女人','media': '媒体', '2008': '奥运',
'business': '商业'
}
half_dir = r'E:\sssss\train_choice'
path = r'E:\sssss\data\data_train' # 新创建的文件夹,主要是放分好类的文件
file_read(half_dir, labels, path) # 将选好的文件进行纯文本提取和分类存储 half_dir = r'E:\sssss\test_choice'
path = r'E:\sssss\data\data_test' #新创建的文件夹,主要是放分好类的文件
file_read(half_dir, labels, path) # 将选好的文件进行纯文本提取和分类存储

解析搜狗实验室精简版数据:1、批量将.txt编码格式转化为utf8 2、解析提取数据的更多相关文章

  1. MSSQL数据的批量插入

    一.概述: 对于数据的批量插入操作似乎成了某些大数据量操作的必用手段,MSSQL也提供了一些数据批量插入的操作方法,先将这些方法汇总,以便于下次用到使用.面对数据的批量插入操作,我们也应该考虑一个问题 ...

  2. Python_实现json数据的jsonPath(精简版)定位及增删改操作

    基于python实现json数据的jsonPath(精简版)定位及增删改操作   by:授客 QQ:1033553122 实践环境 win7 64 Python 3.4.0 代码 #-*- encod ...

  3. SX_WIN10X64LTSB2016_EN_LITE英文精简版

    SX_WIN10X64LTSB2016_EN_LITE英文精简版该版本为英文版!该版本为英文版!该版本为英文版!因为论坛巴基斯坦的maanu兄弟PM我,所以抽空做了一个.介绍沿用原来的,中文用谷歌翻译 ...

  4. WIN10X64_LTSB2016极限精简版by双心

    WIN10X64LTSB2016极限精简版by双心http://www.cnblogs.com/liuzhaoyzz/p/9162113.html 一.前言:关于极限精简版的说明 本系统为极限精简版, ...

  5. WIN10X64LTSC2019中度精简版by双心

    WIN10X64LTSC2019中度精简版by双心https://www.cnblogs.com/liuzhaoyzz/p/11295032.html 一.前言:关于LTSC中度精简版的说明 一个MM ...

  6. Linux上oracle精简版客户端快速部署

    RHEL6 + Oracle 11g客户端快速部署 需求:只是用到客户端的sqlplus, sqlldr功能. 方案:用精简版实现客户端的快速部署 1.上传oracle精简版客户端到服务器/tmp目录 ...

  7. HBase 高性能获取数据(多线程批量式解决办法) + MySQL和HBase性能测试比较

    摘要:   在前篇博客里已经讲述了通过一个自定义 HBase Filter来获取数据的办法,在末尾指出此办法的性能是不能满足应用要求的,很显然对于如此成熟的HBase来说,高性能获取数据应该不是问题. ...

  8. 小韦XPSP3 V10.0_Ghost精简版

    小韦XPSP3 V10.0_Ghost精简版 该版本基于小韦XPSP3 V10.0,用EasySysprep_4.1进行重新封装,最大程度保持原版极度精简的理念.C盘为NTFS格式,安装后C盘只占用2 ...

  9. stl总结精简版

    STL 精简版 vetor #include<vector> #include<cstdio> #include<algorithm> #include<io ...

随机推荐

  1. ZooKeeper Getting Started Guide

    http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html What is ZooKeeper? ZooKeeper is a centra ...

  2. AJAX_违反了同源策略_就是"跨域"——jsonp 和 cors

    https 协议    默认端口号 443 http 协议    默认端口号 80 同源策略 由网景公司提出的——浏览器 的 为了浏览器安全而生 同源策略: 协议.域名.端口号    必须完全一致 违 ...

  3. [LeetCode] Bricks Falling When Hit 碰撞时砖头掉落

    We have a grid of 1s and 0s; the 1s in a cell represent bricks.  A brick will not drop if and only i ...

  4. Hadoop-2.9.2单机版安装(伪分布式模式)(一)

    一.环境 硬件:虚拟机VMware.win7 操作系统:Centos-7 64位 主机名: hadoopServerOne 安装用户:root软件:jdk1.8.0_181.Hadoop-2.9.2 ...

  5. python自动化打开网页

    from selenium.webdriver.firefox.options import Options as FOptionsfrom selenium.webdriver.chrome.opt ...

  6. Java集合-treebag

    import org.apache.commons.collections4.Bag; import org.apache.commons.collections4.bag.TreeBag; impo ...

  7. c#拷贝整个文件夹到指定文件夹下(非递归)

    public static void CopyEntireDir(string sourcePath, string destPath) { //Now Create all of the direc ...

  8. Gym 101606 - A/B/C/D/E/F/G/H/I/J/K/L - (Undone)

    链接:https://codeforces.com/gym/101606 A - Alien Sunset 暴力枚举小时即可. #include<bits/stdc++.h> using ...

  9. Python 学习笔记5 变量-列表

    列表是python常用的一种变量. 是由一些列按照特定顺序排列的元素组成的.你可以创建包含字母表中的所有字母,数字.可以将任何东西都加入到列表中. 通常情况下,列表中都包含多个元素,所以建议变量的名称 ...

  10. JS promise

    1.Promise是什么? Promise是抽象异步处理对象以及对其进行各种操作的组件. 2.实例化 使用new来调用Promise的构造器来进行实例化 var promise = new Promi ...