语料库:是我们要分析的所有文档的集合

使用搜狗实验室提供的语料库,里面有一个classlist,里面内容是文件的编号及分类名称

 

1、导入模块

import os   
import os.path

filePaths=[]    #建立一个空的列表来存放语料库的文件名称,数组变量
for root,dirs,files in os.walk(     
    "D:\\Python\\Python数据挖掘\\2.1\\SogouC.mini\\Sample"):
    for name in files:
        filePaths.append(os.path.join(root,name))

 使用os.walk传入这个目录作为参数,遍历该文件夹下的全部文件,该方法返回一个Truple的数组,第一个root是文件所在目录,第二个是root文件下的子目录命名为dirs,第三个root文件下的所有文件命名为files

拼接文件路径(可解决不同系统下的的文件拼接)

os.path.join(root,name)

 

2、把第一步的文件路径下的内容读取到内存中

 

import codecs

filePaths=[]
fileContents=[]
filenames=[]
for root,dirs,files in os.walk(
    "D:\\Python\\Python数据挖掘\\2.1\\SogouC.mini\\Sample"):
    for name in files:
        filePaths.append(os.path.join(root,name))
        filePath=os.path.join(root,name)
        f=codecs.open(filePath,"r",encoding="utf-8")
        fileContent=f.read()   #读取内容后关闭
        fileContents.append(fileContent)

 

使用codecs.open(filePath,method,encoding)来打开文件,然后用文件的read()方法

 

3、把读取到的内容变成一个数据框

import pandas
corpos=pandas.DataFrame({
        "filePath":filePaths,
        "fileContent":fileContents,
        "class":filenames})

 

分类:

技术点:

相关文章: