forked from clarkkev/attention-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_depparse.py
42 lines (34 loc) · 1.25 KB
/
preprocess_depparse.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
"""Preprocesses dependency parsing data and writes the result as JSON."""
import argparse
import os
import utils
def preprocess_depparse_data(raw_data_file):
examples = []
with open(raw_data_file) as f:
current_example = {"words": [], "relns": [], "heads": []}
for line in f:
line = line.strip()
if line:
word, label = line.split()
head, reln = label.split("-")
head = int(head)
current_example["words"].append(word)
current_example["relns"].append(reln)
current_example["heads"].append(head)
else:
examples.append(current_example)
current_example = {"words": [], "relns": [], "heads": []}
utils.write_json(examples, raw_data_file.replace(".txt", ".json"))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--data-dir", required=True,
help="The location of dependency parsing data. Should contain files "
"train.txt and dev.txt. See the README for expected data format.")
args = parser.parse_args()
for split in ["train", "dev"]:
print("Preprocessing {:} data...".format(split))
preprocess_depparse_data(os.path.join(args.data_dir, split + ".txt"))
print("Done!")
if __name__ == "__main__":
main()