-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquotes.py
More file actions
129 lines (118 loc) · 4.61 KB
/
quotes.py
File metadata and controls
129 lines (118 loc) · 4.61 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import re
import operator
import collections
import logging
import json
import sys
import numpy as np
from collections import defaultdict
from constants import (
GROUP_PART_NAMES,
IS_QUOTE_TYPE,
QUOTE_TYPE_NAMES,
REQUIRED_PARTS,
SENTENCE_START,
SENTENCE_END,
)
logger = logging.getLogger(__name__)
def output_quote_json(input_file, output_file, predictions, mark_sentence):
outputs = []
for line in input_file:
input = json.loads(line)
documentName = input["documentName"]
del input["annotations"]
pred_clusters = predictions[documentName]
annotations = []
gold_tokens = input["tokens"]
originalText = input["originalText"]
if mark_sentence:
tokens = [None]
lastSent = gold_tokens[0]["sentence"]
for t in gold_tokens:
s = t["sentence"]
if lastSent != s:
tokens.append(None)
tokens.append(None)
tokens.append(t)
lastSent = s
else:
tokens = gold_tokens
for cluster in pred_clusters:
# is_special = [t in special_tokens for t in pred_tokens]
# offsets = np.cumsum()
parts = defaultdict(list)
cluster.sort()
for span_start, span_end, part in cluster:
# span_start -= offsets[span_start]
# span_end -= offsets[span_end]
start_token, end_token = tokens[span_start], tokens[span_end]
i = 1
while start_token is None:
print("error start_token is none", file=sys.stderr)
start_token = tokens[span_start + i]
i += 1
i = 1
while end_token is None:
print("error end_token is none", file=sys.stderr)
end_token = tokens[span_end - i]
i -= 1
parts[part].append(
{
"charBegin": start_token["charBegin"],
"charEnd": end_token["charEnd"],
"begin": start_token["id"],
"end": end_token["id"] + 1,
}
)
a = {}
for part, spans in parts.items():
tokenIds = [x for s in spans for x in range(s["begin"], s["end"])]
tokenIds.sort()
text = " ".join(
[originalText[s["charBegin"] : s["charEnd"]] for s in spans]
)
a[GROUP_PART_NAMES[part if part < len(GROUP_PART_NAMES) else 1]] = {
"spans": spans,
"text": text,
"tokenIds": tokenIds,
}
if part > len(GROUP_PART_NAMES) or part == 1:
a["type"] = (
QUOTE_TYPE_NAMES[part - 6]
if IS_QUOTE_TYPE and part > len(GROUP_PART_NAMES)
else "Indirect"
)
a["medium"] = "Speech"
if a.get("quote", None) is not None:
annotations.append(a)
input["annotations"] = annotations
outputs.append(input)
if output_file is not None:
json.dump(input, output_file, ensure_ascii=False)
output_file.write("\n")
return outputs
# coref_list = []
# if word_index in end_map:
# for cluster_id in end_map[word_index]:
# coref_list.append("{})".format(cluster_id))
# if word_index in word_map:
# for cluster_id in word_map[word_index]:
# coref_list.append("({})".format(cluster_id))
# if word_index in start_map:
# for cluster_id in start_map[word_index]:
# coref_list.append("({}".format(cluster_id))
def convert_to_quote_json(
quote_gold_path, predict_clusters_or_json_path, mark_sentence, out_file=None
):
if isinstance(predict_clusters_or_json_path, str):
predictions = {}
with open(predict_clusters_or_json_path, "r") as json_pred_file:
for line in json_pred_file:
doc = json.loads(line)
predictions[doc["doc_key"][:-2]] = doc["predict_clusters"]
else:
predictions = predict_clusters_or_json_path # {k[:-2]: v for k,v in predict_clusters_or_json_path.items()}
with open(quote_gold_path, "r") as gold_file:
return output_quote_json(gold_file, out_file, predictions, mark_sentence)
if __name__ == "__main__":
convert_to_quote_json(sys.argv[1], sys.argv[2], sys.argv[3] == "True", sys.stdout)