Python 语料库的搭建

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 07:43   11   0

语料库就是我们要分析文件的合计。

语料库构建

http://blog.csdn.net/happylife_haha/article/details/44566975

构建方法:

os.walk(fileDir)

fileDir 文件夹路径

文件读取:

codecs.open(filePath, method, encoding)

filePath 文件路径

method 打开方式,r 读, w 写, rw 读写;

encoding 文件的编码,打开方式UTF-8。

# -*- coding: utf-8 -*-

import os

import os.path

#导入OS模块

#定义

filePaths = []

for root, dirs, files in os.walk(

"D:\\PDM\\2.1\\SogouC.mini\\Sample"

):

for name in files:

filePaths.append(os.path.join(root, name))


import codecs

filePaths = [];

fileContents = [];

for root, dirs, files in os.walk(

"D:\\PDM\\2.1\\SogouC.mini\\Sample"

):

#遍历目录下所有的文件,

for name in files:

filePath = os.path.join(root, name);

filePaths.append(filePath);


4938065-e389347fa70bbace.png

#只读文件

f = codecs.open(filePath, 'r', 'utf-8')#打开文件

fileContent = f.read()

f.close()#关闭文件

fileContents.append(fileContent)


4938065-73605641a41ec42e.png

#构建语料库

import pandas;

corpos = pandas.DataFrame({

'filePath': filePaths,

'fileContent': fileContents

})


4938065-8a288df745bd0615.png
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP