Datasets:
Delete prepare_corpus.py
Browse files- prepare_corpus.py +0 -124
prepare_corpus.py
DELETED
|
@@ -1,124 +0,0 @@
|
|
| 1 |
-
import argparse
|
| 2 |
-
import os
|
| 3 |
-
import xmltodict
|
| 4 |
-
import glob
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def extract_train(train_inp, train_out, docids_out=None):
|
| 8 |
-
with open(train_inp, "r", encoding="utf-8") as f:
|
| 9 |
-
all_docs = f.read()
|
| 10 |
-
docs_xml = [f"{d}</doc>" for d in all_docs.split("</doc>")[:-1]]
|
| 11 |
-
docs = [xmltodict.parse(doc) for doc in docs_xml]
|
| 12 |
-
|
| 13 |
-
train_out_f = open(train_out, "w", encoding="utf-8")
|
| 14 |
-
if docids_out is not None:
|
| 15 |
-
docids_out_f = open(docids_out, "w", encoding="utf-8")
|
| 16 |
-
|
| 17 |
-
for doc in docs:
|
| 18 |
-
if "#text" not in doc["doc"]:
|
| 19 |
-
continue
|
| 20 |
-
doc_id = doc["doc"]["@docid"]
|
| 21 |
-
for line in doc["doc"]["#text"].split("\n"):
|
| 22 |
-
print(line.strip(), file=train_out_f)
|
| 23 |
-
if docids_out is not None:
|
| 24 |
-
print(doc_id, file=docids_out_f)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def extract_eval(eval_inp, eval_out, docids_out=None):
|
| 28 |
-
with open(eval_inp, "r", encoding="utf-8") as f:
|
| 29 |
-
all_docs = f.read()
|
| 30 |
-
docs_xml = xmltodict.parse(all_docs)
|
| 31 |
-
docs = (
|
| 32 |
-
docs_xml["mteval"]["srcset"]["doc"]
|
| 33 |
-
if "srcset" in docs_xml["mteval"]
|
| 34 |
-
else docs_xml["mteval"]["refset"]["doc"]
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
eval_out_f = open(eval_out, "a")
|
| 38 |
-
if docids_out is not None:
|
| 39 |
-
docids_out_f = open(docids_out, "a")
|
| 40 |
-
|
| 41 |
-
for doc in docs:
|
| 42 |
-
doc_id = doc["@docid"]
|
| 43 |
-
if not doc["seg"]:
|
| 44 |
-
continue
|
| 45 |
-
for seg in doc["seg"]:
|
| 46 |
-
line = seg["#text"]
|
| 47 |
-
print(line.strip(), file=eval_out_f)
|
| 48 |
-
if docids_out is not None:
|
| 49 |
-
print(doc_id, file=docids_out_f)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
if __name__ == "__main__":
|
| 53 |
-
parser = argparse.ArgumentParser("parser for iwslt2017 data")
|
| 54 |
-
|
| 55 |
-
parser.add_argument("raw_data")
|
| 56 |
-
parser.add_argument("out_data")
|
| 57 |
-
parser.add_argument("-s", "--source-lang", required=True, type=str)
|
| 58 |
-
parser.add_argument("-t", "--target-lang", required=True, type=str)
|
| 59 |
-
parser.add_argument(
|
| 60 |
-
"--devsets", nargs="*", default=["tst2011", "tst2012", "tst2013", "tst2014"]
|
| 61 |
-
)
|
| 62 |
-
parser.add_argument("--testsets", nargs="*", default=["tst2015"])
|
| 63 |
-
args = parser.parse_args()
|
| 64 |
-
|
| 65 |
-
first_l = (
|
| 66 |
-
args.source_lang
|
| 67 |
-
if glob.glob(f"{args.raw_data}/*.{args.source_lang}-{args.target_lang}.*")
|
| 68 |
-
else args.target_lang
|
| 69 |
-
)
|
| 70 |
-
second_l = (
|
| 71 |
-
args.target_lang
|
| 72 |
-
if glob.glob(f"{args.raw_data}/*.{args.source_lang}-{args.target_lang}.*")
|
| 73 |
-
else args.source_lang
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
train_inp_prefix = os.path.join(args.raw_data, f"train.tags.{first_l}-{second_l}")
|
| 77 |
-
train_out_prefix = os.path.join(
|
| 78 |
-
args.out_data, f"train.{args.source_lang}-{args.target_lang}"
|
| 79 |
-
)
|
| 80 |
-
extract_train(
|
| 81 |
-
f"{train_inp_prefix}.{args.source_lang}",
|
| 82 |
-
f"{train_out_prefix}.{args.source_lang}",
|
| 83 |
-
docids_out=f"{train_out_prefix}.docids",
|
| 84 |
-
)
|
| 85 |
-
extract_train(
|
| 86 |
-
f"{train_inp_prefix}.{args.target_lang}",
|
| 87 |
-
f"{train_out_prefix}.{args.target_lang}",
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
# generate valid set based on devsets paseed
|
| 91 |
-
valid_out_prefix = os.path.join(
|
| 92 |
-
args.out_data, f"valid.{args.source_lang}-{args.target_lang}"
|
| 93 |
-
)
|
| 94 |
-
for devset in args.devsets:
|
| 95 |
-
valid_inp_prefix = os.path.join(
|
| 96 |
-
args.raw_data, f"IWSLT17.TED.{devset}.{first_l}-{second_l}"
|
| 97 |
-
)
|
| 98 |
-
extract_eval(
|
| 99 |
-
f"{valid_inp_prefix}.{args.source_lang}.xml",
|
| 100 |
-
f"{valid_out_prefix}.{args.source_lang}",
|
| 101 |
-
docids_out=f"{valid_out_prefix}.docids",
|
| 102 |
-
)
|
| 103 |
-
extract_eval(
|
| 104 |
-
f"{valid_inp_prefix}.{args.target_lang}.xml",
|
| 105 |
-
f"{valid_out_prefix}.{args.target_lang}",
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
# generate test set based on testsets paseed
|
| 109 |
-
test_out_prefix = os.path.join(
|
| 110 |
-
args.out_data, f"test.{args.source_lang}-{args.target_lang}"
|
| 111 |
-
)
|
| 112 |
-
for testset in args.testsets:
|
| 113 |
-
test_inp_prefix = os.path.join(
|
| 114 |
-
args.raw_data, f"IWSLT17.TED.{testset}.{first_l}-{second_l}"
|
| 115 |
-
)
|
| 116 |
-
extract_eval(
|
| 117 |
-
f"{test_inp_prefix}.{args.source_lang}.xml",
|
| 118 |
-
f"{test_out_prefix}.{args.source_lang}",
|
| 119 |
-
docids_out=f"{test_out_prefix}.docids",
|
| 120 |
-
)
|
| 121 |
-
extract_eval(
|
| 122 |
-
f"{test_inp_prefix}.{args.target_lang}.xml",
|
| 123 |
-
f"{test_out_prefix}.{args.target_lang}",
|
| 124 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|