-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprepare_data.py
77 lines (60 loc) · 2.15 KB
/
prepare_data.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
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
import urllib.request
import re
import os
import argparse
from nltk.tokenize import word_tokenize
def get_text(url):
with urllib.request.urlopen(url) as response:
text = response.read().decode("utf-8")
return text
def sent_tokenize(text):
text = text.replace("—", " — ")
sents = re.split("(?<=[!?.;])[ ]", text)
_sents = []
for s in sents:
_sents.extend(re.split("[ ](?=[—])", s))
return _sents
def refine(text, seqlen=None):
print("refine..")
if seqlen is None:
return text
text = text.strip().replace("\r\n", " ")
text = re.sub("[ ]+", " ", text)
sents = sent_tokenize(text) + ['dummy'*100]
samples = []
for start in range(len(sents)):
for end in range(start+1, len(sents)+1):
sample = sents[start:end]
sample = " ".join(sample)
length = len(sample) + (end-start-1) # space between sents
if length > seqlen:
sample = sents[start:end-1]
if sample != []:
sample = " ".join(sample)
sample = ' '.join(word_tokenize(sample))
samples.append(sample)
break
text = "\n".join(samples)
# print(text)
return text
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--seqlen", type=int, default=250)
hp = parser.parse_args()
url = "https://www.gutenberg.org/files/2701/2701-0.txt"
text = get_text(url)
train_start = "Call me Ishmael"
train_end = "sat due eastward for the earliest sun."
test_end = "Epilogue"
train_text = re.search(f"(?s)({train_start}.+?{train_end})", text).group(1)
test_text = re.search(f"(?s){train_end}(.+?){test_end}", text).group(1)
train_text = refine(train_text, hp.seqlen)
test_text = refine(test_text)
# save
if not os.path.exists('corpus/train'): os.makedirs('corpus/train')
with open('corpus/train/train.txt', 'w') as fout:
fout.write(train_text)
with open("corpus/valid.txt", 'w') as fout:
fout.write(test_text)
with open("corpus/test.txt", 'w') as fout:
fout.write(test_text)