-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpreprocessing.py
58 lines (45 loc) · 1.86 KB
/
preprocessing.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
import re
import argparse
from generator_utils import encode
def preprocess_sentence(w):
# creating a space between a word and the punctuation following it
# eg: "he is a boy." => "he is a boy ."
# Reference:- https://stackoverflow.com/questions/3645931/python-padding-punctuation-with-white-spaces-keeping-punctuation
w = re.sub(r"([?.!,¿])", r" \1 ", w)
w = re.sub(r'[" "]+', " ", w)
# replacing everything with space except (a-z, A-Z, ".", "?", "!", ",")
# w = re.sub(r"[^a-zA-Z?.!,¿]+", " ", w)
w = w.rstrip().strip().lower()
# adding a start and an end token to the sentence
# so that the model know when to start and stop predicting.
# w = '<start> ' + w + ' <end>'
return w
def preprocess_sparql(s):
# Remove prefixes
s = re.sub(r"PREFIX\s[^\s]*\s[^\s]*", "", s)
s = encode(s.rstrip().strip())
s = ' '.join(s.split())
return s
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--en', dest='english_file', required=True)
parser.add_argument('--sparql', dest='sparql_file', required=True)
parser.add_argument('--output', dest='output_prefix', required=True)
args = parser.parse_args()
english_file = args.english_file
sparql_file = args.sparql_file
output_prefix = args.output_prefix
with open(output_prefix+'.en', 'w') as out:
with open(english_file) as f:
for line in f:
if '\n' == line[-1]:
line = line[:-1]
out.write(preprocess_sentence(line))
out.write('\n')
with open(output_prefix+'.sparql', 'w') as out:
with open(sparql_file) as f:
for line in f:
if '\n' == line[-1]:
line = line[:-1]
out.write(preprocess_sparql(line))
out.write('\n')