python文本分析与挖掘(一)-构建语料库
实现功能:
python文本分析与挖掘(一)-构建语料库
实现代码:
1 | import os |
2 | from warnings import simplefilter |
3 | simplefilter(action='ignore', category=FutureWarning) |
4 | import os.path |
5 | import codecs |
6 | import pandas |
7 | |
8 | #==========词料库构建================= |
9 | def Create_corpus(file): |
10 | filePaths = [] |
11 | fileContents=[] |
12 | for root, dirs, files in os.walk(file): |
13 | print(root) |
14 | print(dirs) |
15 | print(files) |
16 | # os.path.join()方法拼接文件名返回所有文件的路径,并储存在变量filePaths中 |
17 | for name in files: |
18 | filePath=os.path.join(root, name) |
19 | filePaths.append(filePath) |
20 | print(filePaths) |
21 | f = codecs.open(filePath, 'r', 'utf-8') |
22 | print(f) |
23 | fileContent = f.read() |
24 | print(fileContent) |
25 | f.close() |
26 | fileContents.append(fileContent) |
27 | #codecs.open()方法打开每个文件,用文件的read()方法依次读取其中的文本,将所有文本内容依次储存到变量fileContenst中,然后close()方法关闭文件。 |
28 | #创建数据框corpos,添加filePaths和fileContents两个变量作为数组 |
29 | corpos = pandas.DataFrame({'filePath': filePaths,'fileContent': fileContents}) |
30 | print(corpos) |
31 | |
32 | Create_corpus("F:\医学大数据课题\AI_SLE\AI_SLE_TWO\TEST_DATA") |
实现效果:
喜欢记得点赞,在看,收藏,
关注V订阅号:数据杂坛,获取数据集,完整代码和效果,将持续更新!