File size: 12,299 Bytes
a221720
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Code borrowed from DaCy (Kenneth)


"""
TODO:
- Save data to HF datasets
- save as docbin using # docbin.to_disk(path, store_user_data=True)
"""

import json
from collections import defaultdict
from pathlib import Path

import spacy
from conllu import parse
from spacy.tokens import Doc, DocBin, Span, Token
from spacy.training.corpus import Corpus

file_path = Path(__file__)
assets_path = file_path.parent.parent / "assets"
corpus_path = file_path.parent.parent / "corpus"

Doc.set_extension("domain", default=None)
Doc.set_extension("sent_id", default=None)
Doc.set_extension("sent_ids", default=None)
Doc.set_extension("conllu", default=None)
Doc.set_extension("doc_id", default=None)
Token.set_extension("qid", default=None)


def load_cdt(custom_split_ids: bool = True):
    """
    Load the copenhagen dependency treebank / DaCoref dataset
    """
    cdt_path = assets_path / "dacoref" / "CDT_coref.conllu"
    with cdt_path.open(encoding="utf-8") as f:
        text = f.read()

    sentences = parse(
        text,
        fields=[
            "id",
            "form",
            "lemma",
            "upos",
            "xpos",
            "feats",
            "head",
            "deprel",
            "deps",
            "misc",
            "coref_id",
            "coref_rel",
            "doc_id",
            "qid",
        ],
    )

    if not custom_split_ids:  # use the ones specified by the authors
        split_ids = {"train": [], "dev": [], "test": []}

        for split in split_ids:
            split_path = assets_path / "dacoref" / f"CDT_{split}_ids.json"
            with split_path.open(encoding="utf-8") as f:
                split_ids[split] = json.load(f)

    else:  # use the one we created that respects the DDT splits
        with open(assets_path / "CDT_ddt_compatible_splits.json") as f:
            split_ids = json.load(f)

    return sentences, split_ids


def _add_sent_id(docs, split, dataset):
    path = assets_path / dataset / f"{split}.conllu"
    with path.open(encoding="utf-8") as f:
        text = f.read()
    sentences = parse(text)
    for sent, doc in zip(sentences, docs):
        assert doc.text.strip() == sent.metadata["text"].strip()
        doc._.sent_id = sent.metadata["sent_id"]
        doc._.conllu = sent


def load_da_ddt():
    """
    Loads the UD Danish Dependency Treebank
    """
    nlp = spacy.blank("da")
    ddt_path = corpus_path / "da_ddt"

    # With a single file
    ddt = {}
    for split in ["train", "dev", "test"]:
        path = ddt_path / f"{split}.spacy"
        corpus = Corpus(path, shuffle=False)
        examples = list(corpus(nlp))
        docs = [e.reference for e in examples]
        ddt[split] = docs
        _add_sent_id(docs, split, dataset="da_ddt")

    return ddt


def load_dane():
    """
    Loads the UD Danish Dependency Treebank
    """
    nlp = spacy.blank("da")
    dane_path = corpus_path / "dane"

    # With a single file
    dane = {}
    for split in ["train", "dev", "test"]:
        path = dane_path / f"{split}.spacy"
        corpus = Corpus(path, shuffle=False)
        examples = list(corpus(nlp))
        docs = [e.reference for e in examples]
        dane[split] = docs
        _add_sent_id(docs, split, dataset="dane")
    return dane

# original
#def add_dane_to_ddt(ddt, dane):
#    """
#    Add the dane data to the ddt data
#    """
#    for split in ["train", "dev", "test"]:
#        assert len(ddt[split]) == len(dane[split])
#        for doc, dane_doc in zip(ddt[split], dane[split]):
#            
#            # assert doc.text.strip() == dane_doc.text.strip() <-- Removed to not crash script because of whitespace misalignment. Mikkel
#            # Claude recommended this check instead due to benign whitespace misalignment (see diagnose_dane_to_ddt_mismatch.py):
#            assert [t.text for t in doc] == [t.text for t in dane_doc], (
#                f"token mismatch at {doc._.sent_id}: "
#                f"{[t.text for t in doc]!r} vs {[t.text for t in dane_doc]!r}"
#            )
#
#            assert doc._.sent_id == dane_doc._.sent_id
#            # convert dane ents to ddt ents
#            ents = [Span(doc, e.start, e.end, label=e.label_) for e in dane_doc.ents]
#            doc.ents = ents
#    return ddt

def add_dane_to_ddt(ddt, dane):
    """
    Add the dane data to the ddt data
    """
    dane_ddt_path = corpus_path / "dane_ddt"
    dane_ddt_path.mkdir(parents=True, exist_ok=True)
    for split in ["train", "dev", "test"]:
        assert len(ddt[split]) == len(dane[split])
        doc_bin = DocBin(store_user_data=True)   # moved inside, fresh per split
        for doc, dane_doc in zip(ddt[split], dane[split]):
            assert [t.text for t in doc] == [t.text for t in dane_doc], (
                f"token mismatch at {doc._.sent_id}: "
                f"{[t.text for t in doc]!r} vs {[t.text for t in dane_doc]!r}"
            )
            assert doc._.sent_id == dane_doc._.sent_id
            ents = [Span(doc, e.start, e.end, label=e.label_) for e in dane_doc.ents]
            doc.ents = ents
            doc_bin.add(doc)                     # actually add the modified doc
        save_path = dane_ddt_path / f"{split}.spacy"
        doc_bin.to_disk(save_path)
    return ddt

def combine_docs(cdt_sentences, ddt_dane):
    sent_id_to_doc_instance = {}
    for _split, docs in ddt_dane.items():
        for doc in docs:
            assert doc._.sent_id not in sent_id_to_doc_instance
            sent_id_to_doc_instance[doc._.sent_id] = doc

    # combine documents
    doc_to_be_created: dict[str, list[str]] = {}
    sent_id_to_sent = {}
    for sent in cdt_sentences:
        sent_id = sent.metadata["sent_id"]
        doc_id = sent[0]["doc_id"]
        if doc_id not in doc_to_be_created:
            doc_to_be_created[doc_id] = []
        doc_to_be_created[doc_id].append(sent_id)
        sent_id_to_sent[sent_id] = sent

    # create docs
    domain_mapping = {
        "mz": "magazine",
        "bn": "broadcast",
        "nw": "newswire",
    }
    docs = []
    for doc_id, sent_ids in doc_to_be_created.items():
        _docs = [sent_id_to_doc_instance.pop(sent_id) for sent_id in sent_ids]
        doc = Doc.from_docs(_docs)
        doc._.doc_id = doc_id
        doc._.sent_ids = sent_ids
        doc._.domain = domain_mapping[doc_id.split("/")[0]]
        doc._.conllu = [sent_id_to_sent[sent_id] for sent_id in sent_ids]
        docs.append(doc)

    # add the remaining docs
    for sent_id in list(sent_id_to_doc_instance.keys()):
        doc = sent_id_to_doc_instance.pop(sent_id)
        docs.append(doc)
    return docs


def add_coreference(cdt_sentences, docs):
    doc_id_to_doc_instance = {doc._.doc_id: doc for doc in docs}
    doc_id_to_cdt_sent = defaultdict(list)
    for sent in cdt_sentences:
        doc_id = sent[0]["doc_id"]
        doc_id_to_cdt_sent[doc_id].append(sent)

    for doc_id, sents in doc_id_to_cdt_sent.items():
        clustermap = defaultdict(list)
        doc = doc_id_to_doc_instance[doc_id]
        tokens = [t for sent in sents for t in sent]
        assert len(doc) == len(tokens)
        for token, s_token in zip(tokens, doc):  # type: ignore
            coref_rel = token["coref_rel"]
            if coref_rel == "-":
                continue
            clusters = sorted(coref_rel.split("|"), reverse=True)
            for mention in clusters:
                full_mention = mention.startswith("(") and mention.endswith(")")
                start_mention = mention.startswith("(")
                end_mention = mention.endswith(")")
                if full_mention:
                    cid = mention[1:-1]
                    clustermap[cid].insert(0, (s_token.i, s_token.i + 1))
                elif start_mention:
                    cid = mention[1:]
                    clustermap[cid].append(s_token.i)
                elif end_mention:
                    cid = mention[:-1]
                    start = clustermap[cid].pop()
                    clustermap[cid].insert(0, (start, s_token.i + 1))
        for i, (_key, vals) in enumerate(clustermap.items()):
            spans = [doc[start:end] for start, end in vals]
            skey = f"coref_clusters_{i}"
            doc.spans[skey] = spans

        # parse and get heads
        for i, (_key, val) in enumerate(clustermap.items()):
            heads = [doc[start:end].root.i for start, end in val]
            heads = list(set(heads))
            if len(heads) == 1:
                continue
            spans = [doc[hh : hh + 1] for hh in heads]
            skey = f"coref_head_clusters_{i}"
            doc.spans[skey] = spans
    return docs


def add_qid(docs):
    # add QID to each token
    for doc in docs:
        if doc._.doc_id is None:
            continue
        sents = doc._.conllu
        tokens = [t for sent in sents for t in sent]
        qid_spans = {}  # construct qid spans to check if any of them are not entities
        qid = None
        start = None
        for t, s_t in zip(tokens, doc):
            end_of_span = qid is not None and (t["qid"] == "-" or t["qid"] != qid)
            if end_of_span:
                qid_spans[(start, s_t.i)] = qid
                start = None
                qid = None
            if t["qid"] != "-":
                qid = t["qid"]
                assert qid.startswith("Q")
                s_t._.qid = qid
                if start is None:
                    start = s_t.i
        if start is not None:
            qid_spans[(start, s_t.i)] = qid  # type: ignore

        ents_spans = {(ent.start, ent.end) for ent in doc.ents}
        for qid_span in qid_spans:
            if qid_span[1] - qid_span[0] == 1:
                continue  # ignore single token spans
            if qid_span not in ents_spans:
                print(
                    f"{doc[qid_span[0]:qid_span[1]]} with QID {qid_spans[qid_span]} is not in entities",
                )
            # great no problems here!!

        # map QID to each entity
        new_ents = []
        for ent in doc.ents:
            start, end = ent.start, ent.end
            qids = [t["qid"] for t in tokens[start:end]]
            unique_qid = len(set(qids)) == 1
            if unique_qid:
                qid = qids[0]
                new_ent = Span(doc, start, end, label=ent.label_, kb_id=qid)
            else:
                print(f"Ent {ent} has multiple QIDs: {qids}")
                new_ent = Span(doc, start, end, label=ent.label_)
            new_ents.append(new_ent)
        doc.ents = new_ents
    return docs


cdt_sentences, cdf_split_ids = load_cdt()
doc_id_to_split_mapping = {
    id_: split for split, ids in cdf_split_ids.items() for id_ in ids
}

ddt = load_da_ddt()
dane = load_dane()
ddt_dane = add_dane_to_ddt(ddt, dane)

# add doc_id
sent_id_to_doc_id = {}
for sent in cdt_sentences:
    sent_id_to_doc_id[sent.metadata["sent_id"]] = sent[0]["doc_id"]
for split in ["train", "dev", "test"]:
    for doc in ddt_dane[split]:
        if doc._.sent_id in sent_id_to_doc_id:
            doc._.doc_id = sent_id_to_doc_id[doc._.sent_id]

# check that splits are the same  -- they are not
# for split in ["train", "dev", "test"]:
#     for doc in ddt_dane[split]:
#         assert doc_id_to_split_mapping[doc._.doc_id] == split

# any doc id in cdt that is not in ddt_dane
doc_ids = {doc._.doc_id for split, docs in ddt_dane.items() for doc in docs}
doc_ids_cdt = {sent[0]["doc_id"] for sent in cdt_sentences}
assert len(doc_ids_cdt - doc_ids) == 0

docs = combine_docs(cdt_sentences, ddt_dane)
docs = add_coreference(cdt_sentences, docs)
#docs = add_qid(docs)

doc_bin = DocBin(store_user_data=True)
for doc in docs:
    doc_bin.add(doc)

save_path = corpus_path / "cdt_ddt" / "data.spacy"
save_path.parent.mkdir(parents=True, exist_ok=True)
doc_bin.to_disk(save_path)

# do it again for the cdt only but do it in splits:
for split in ["train", "dev", "test"]:
    doc_bin = DocBin(store_user_data=True)
    for doc in docs:
        if doc._.doc_id is None:
            continue
        _split = doc_id_to_split_mapping[doc._.doc_id]
        if _split != split:
            continue
        doc_bin.add(doc)

    save_path = corpus_path / "cdt" / f"{split}.spacy"
    save_path.parent.mkdir(parents=True, exist_ok=True)
    doc_bin.to_disk(save_path)