-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.py
45 lines (32 loc) · 1.03 KB
/
clean.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
44
45
from nltk.tokenize import sent_tokenize
import re
def clean_text(data):
# corpus specific cleaning
data = re.sub(r"#.{1,50}#", " ", data)
# Eg. #MERGER PROPOSED#
data = re.sub(r"_.{1,50}_", " ", data)
# Eg. _AUSTIN, TEXAS_
data = re.sub(r"[~&@{}<>]", "", data)
return data
def write_file(lines, filename):
with open(filename, "w", encoding="utf8") as f:
for line in lines:
f.write(line + "\n")
if __name__ == "__main__":
with open("./brown.txt", "r", encoding="utf8") as f:
data = f.read()
data = re.sub(r"\s+", " ", data)
data = clean_text(data)
data = re.sub(r"\s+", " ", data)
sents = sent_tokenize(data)
count = len(sents)
# print(count)
offset_incr = int(count // 10)
offset = offset_incr * 7
lines = sents[:offset]
write_file(lines, "./train.txt")
lines = sents[offset : offset + (offset_incr)]
write_file(lines, "./valid.txt")
offset = offset + (offset_incr)
lines = sents[offset:]
write_file(lines, "./test.txt")