-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_normalizer.py
More file actions
51 lines (37 loc) · 1.37 KB
/
text_normalizer.py
File metadata and controls
51 lines (37 loc) · 1.37 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
import argparse
import os
import re
import string
import validators
def normalize_text(line):
if not line:
return ""
trimmed_line = " ".join(line.split())
split_line = ""
for word in trimmed_line.split(" "):
if validators.url(word):
continue
split_line += word.lower() + " "
if not split_line or split_line.isspace():
return ""
pattern = re.compile(r"[^A-Za-z ]+", re.UNICODE)
normalized_line = pattern.sub("", split_line)
return normalized_line
def main(args):
print("Reading input file {}. Writing output to {}.".format(args.inputfile, args.outputfile));
with open(args.outputfile, "w+", encoding="utf-8") as output_file:
with open(args.inputfile, encoding="utf-8") as input_file:
for input_line in input_file:
output_line = normalize_text(input_line.rstrip())
if output_line:
output_file.write(output_line)
print("Finished processing.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
optional = parser._action_groups.pop()
required = parser.add_argument_group("required arguments")
required.add_argument("-i", "--inputfile", required=True)
optional.add_argument("-o", "--outputfile", nargs="?")
parser._action_groups.append(optional)
args = parser.parse_args()
main(args)