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. vue_过滤器: 对要显示的数据进行特定格式化后再显示

    过滤器 对要显示的数据进行特定格式化后再显示 并未改变原本的数据,可是产生新的对应的数据 <!DOCTYPE html> <html lang="en"> ...

  2. linux学习:find用法整理

    find path -option [ -print ] [ -exec -ok command ] {} \; path: find命令所查找的目录路径.例如用.来表示当前目录,用/来表示系统根目录 ...

  3. vue学习目录 vue初识 this指向问题 vue组件传值 过滤器 钩子函数 路由 全家桶 脚手架 vuecli element-ui axios bus

    vue学习目录 vue学习目录 Vue学习一之vue初识 Vue学习二之vue结合项目简单使用.this指向问题 Vue学习三之vue组件 Vue学习四之过滤器.钩子函数.路由.全家桶等 Vue学习之 ...

  4. MySQL入门,了解下、

    本人菜鸡一个,一份简单MySQL笔记送给大家,希望大家喜欢.(●'◡'●) Ⅰ. 数据备份与导入导出 1.1.备份基本概念介绍 1.2.mysqldump详解 1.3.mydumper浅析 1.4.M ...

  5. 在VM虚拟机中安装Centos操作系统

    首先我们要下载  Centos https://www.centos.org/ 这个是Centos官方 最新版本 7 https://www.centos.org/download/ 提供有 DVD安 ...

  6. springcloud第八步:hystrix解决服务雪崩

    断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...

  7. 2018-2019-2 网络对抗技术 20165236 Exp2 后门原理与实践

    2018-2019-2 网络对抗技术 20165236 Exp2 后门原理与实践 一.实验内容 (3.5分) (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用soc ...

  8. Appium IOS 使用多模拟器并发执行测试

    申明一下   转载请注明出处  复制粘贴请滚蛋  !!!!!!!! 最近在是用appium进行app的并发测试,并且Android已经实现在同一台PC机使用多个模拟器并发测试的功能 这里说一句模拟器使 ...

  9. selenium的

    1.简介 selenium可以认为是反反爬虫的最佳利器,它基本可以等同于真实的浏览器访问,用它可以加载到动态数据,也省去了cookie的操作,但是用这个有一个重大的效率问题.所以selenium可以用 ...

  10. java的智能提示无法打开

    第一步:选中“window”->“preference”   第二步:选中“java”,并展开   第三步:选中“Editor”,并展开   第四步:选中“Content Assist”,在右侧 ...