300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 【Python NLP】:搜狗语料库-新闻语料处理

【Python NLP】:搜狗语料库-新闻语料处理

时间:2019-01-25 10:12:30

相关推荐

【Python NLP】:搜狗语料库-新闻语料处理

搜狗新闻语料处理

自然语言处理,最重要的当然是语料数据,选择搜狗实验室的语料库是不错的,但是模型训练前,需要对数据做一些处理,过程如下。

1、数据下载链接:搜狗新闻语料库

先在链接上下载“搜狐新闻数据(SogouCS)”,请直接下载“精简版”!(迷你版有雷。。。)

2、数据下载下来后是这样子的

3、数据量已经很大了,每一个txt采用的是ANSI编码方式

4、每个新闻可以根据url、contenttitle、content三者进行拆分

url:获取内容类别

contenttitle:获取内容标题,作为之后txt的文档名

content:正文内容

5、开始拆分数据

import reimport osclass Sougou(object):def __init__(self):self.directory = 'sogoucs/'self.file = [file for a,b,file in os.walk(self.directory)][0]def split_language_database(self):main_config = 'sogoucs_split/'os.makedirs(main_config) if not os.path.exists(main_config) else print('Is Exists')for file in self.file:# 读取txt文件text = open(self.directory + file, 'rb').read().decode("ansi")# 匹配 url 和 正文内容content = re.findall('<url>(.*?)</url>.*?<contenttitle>(.*?)</contenttitle>.*?<content>(.*?)</content>', text, re.S)# 根据 url 存放每一个 正文内容for news in content:url_title= news[0]content_title = news[1]news_text= news[2]# 提取正文的类别title = re.findall('http://(.*?).', url_title)[0]# 存储正文if len(title)>0 and len(content_title)>0 and len(news_text)>30:print('【{}】【{}】【{}】'.format(file, title, content_title))# 目标保存路径save_config = main_config + title# 如果没有该文件,则创建文件夹os.makedirs(save_config) if not os.path.exists(save_config) else print('Is Exists')# 保存文件f = open('{}/{}.txt'.format(save_config, content_title), 'w', encoding='utf-8')f.write(news_text)f.close()if __name__ == '__main__':sg = Sougou()sg.split_language_database()

基本步骤(不出意外,基本上是可以直接运行的)

1、首先把上面那一坨txt放到一个名为【sogoucs】的文件夹

2、把代码【搜狗语料库.py】放在同一个目下

3、最后输出的结果会生成在【sogoucs_split

如图所示

整个处理过程很久。。我反正跑了快【2小时】呢日。。

6、最后的整理结果如下

有了数据就可以随心训练模型了,什么TF-IDF、LSI、LDA、TEXTRank赶紧安排上!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。