forked from lcdevelop/ChatBotCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
95 lines (84 loc) · 2.99 KB
/
filter.py
File metadata and controls
95 lines (84 loc) · 2.99 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# coding:utf-8
import sys
import re
import chardet
if __name__ == '__main__':
#illegal=ur"([\u2000-\u2010]+)"
illegal=ur"([\u0000-\u2010]+)"
pattern_illegals = [re.compile(ur"([\u2000-\u2010]+)"), re.compile(ur"([\u0090-\u0099]+)")]
filters = ["字幕", "时间轴:", "校对:", "翻译:", "后期:", "监制:"]
filters.append("时间轴:")
filters.append("校对:")
filters.append("翻译:")
filters.append("后期:")
filters.append("监制:")
filters.append("禁止用作任何商业盈利行为")
filters.append("http")
htmltagregex = re.compile(r'<[^>]+>',re.S)
brace_regex = re.compile(r'\{.*\}',re.S)
slash_regex = re.compile(r'\\\w',re.S)
repeat_regex = re.compile(r'[-=]{10}',re.S)
f = open("./corpus/all.out", "r")
count=0
while True:
line = f.readline()
if line:
line = line.strip()
# 编码识别,不是utf-8就过滤
gb_content = ''
try:
gb_content = line.decode("utf-8")
except Exception as e:
sys.stderr.write("decode error: ", line)
continue
# 中文识别,不是中文就过滤
need_continue = False
for pattern_illegal in pattern_illegals:
match_illegal = pattern_illegal.findall(gb_content)
if len(match_illegal) > 0:
sys.stderr.write("match_illegal error: %s\n" % line)
need_continue = True
break
if need_continue:
continue
# 关键词过滤
need_continue = False
for filter in filters:
try:
line.index(filter)
sys.stderr.write("filter keyword of %s %s\n" % (filter, line))
need_continue = True
break
except:
pass
if need_continue:
continue
# 去掉剧集信息
if re.match('.*第.*季.*', line):
sys.stderr.write("filter copora %s\n" % line)
continue
if re.match('.*第.*集.*', line):
sys.stderr.write("filter copora %s\n" % line)
continue
if re.match('.*第.*帧.*', line):
sys.stderr.write("filter copora %s\n" % line)
continue
# 去html标签
line = htmltagregex.sub('',line)
# 去花括号修饰
line = brace_regex.sub('', line)
# 去转义
line = slash_regex.sub('', line)
# 去重复
new_line = repeat_regex.sub('', line)
if len(new_line) != len(line):
continue
# 去特殊字符
line = line.replace('-', '').strip()
if len(line) > 0:
sys.stdout.write("%s\n" % line)
count+=1
else:
break
f.close()
pass