-
Notifications
You must be signed in to change notification settings - Fork 9
/
token_plain_text.py
43 lines (30 loc) · 1.2 KB
/
token_plain_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import absolute_import
import sys
import pathlib
import jieba
import joblib
from tqdm import tqdm
input_dir = sys.argv[1]
input_path = pathlib.Path(input_dir)
input_files = input_path.glob("**/*")
# filter out hidden files (e.g. .gitignore)
input_files = filter(lambda x: not x.parts[-1].startswith('.'), input_files)
output_dir = sys.argv[2]
output_path = pathlib.Path(output_dir)
def process_file(input_file_path):
input_file = str(input_file_path.absolute())
output_file_path = output_path / input_file_path.parts[-1]
output_file = str(output_file_path.absolute())
with open(input_file, 'rt') as in_fd, open(output_file, 'wt') as out_fd:
output_lines = []
for line in in_fd:
seg_list = jieba.cut(line, cut_all=False)
output_line = " ".join(filter(lambda x: x != " ", seg_list)) # 精确模式
output_lines.append(output_line)
out_fd.writelines(output_lines)
joblib.Parallel(n_jobs=-1)(joblib.delayed(process_file)(input_file) for input_file in tqdm(input_files))