-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreprocess_qed.py
More file actions
26 lines (24 loc) · 981 Bytes
/
preprocess_qed.py
File metadata and controls
26 lines (24 loc) · 981 Bytes
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
import re
import os
import sys
from langid.langid import LanguageIdentifier, model
if __name__=="__main__":
identifier = LanguageIdentifier.from_modelstring(model, norm_probs=True)
os.makedirs(sys.argv[2], exist_ok=True)
for f in os.listdir(sys.argv[1]):
if f.endswith(".xml"):
document = ""
out = open(os.path.join(sys.argv[2], f[:-4] + ".txt"), "w")
for line in open(os.path.join(sys.argv[1], f)):
if re.match(r"\s*<s", line):
sentence = ""
elif not re.match(r"\s*<", line):
sentence += line.strip() + " "
elif re.match(r"\s*</s", line):
document += sentence + "\n"
elif re.match(r"\s*</document", line):
if identifier.classify(document)[0] == "en":
out.write(document)
out.close()
else:
continue