1. 加载数据
首先观察文件构成:
可发现邮件存储以一个个txt文档的形式存放在一个文件夹中,因而需要先遍历文件夹中的所有文件,拿到文件地址
# load data
def file_walker(file_path):
file_list = []
for root, dirs, files in os.walk(file_path): # a generator
for fn in files:
path = str(root+'/'+fn)
file_list.append(path)
return file_list
然后读取txt文件,去掉非中文字符
def read_txt(path, encoding):
with open(path, 'r', encoding=encoding, errors='ignore') as f:
lines = f.readlines()
return lines
def extract_chinese(text):
content = ' '.join(text)