Datasets:
Matej Klemen
commited on
Commit
•
388286d
1
Parent(s):
a3ec4b4
Add first version of KOMET
Browse files- dataset_infos.json +1 -0
- komet.py +167 -0
dataset_infos.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"default": {"description": "KOMET 1.0 is a hand-annotated corpus for metaphorical expressions which contains about 200,000 words from \nSlovene journalistic, fiction and on-line texts. \n\nTo annotate metaphors in the corpus an adapted and modified procedure of the MIPVU protocol \n(Steen et al., 2010: A method for linguistic metaphor identification: From MIP to MIPVU, https://www.benjamins.com/catalog/celcr.14) \nwas used. The lexical units (words) whose contextual meanings are opposed to their basic meanings are considered \nmetaphor-related words. The basic and contextual meaning for each word in the corpus was identified using the \nDictionary of the standard Slovene Language. The corpus was annotated for the metaphoric following relations: \nindirect metaphor (MRWi), direct metaphor (MRWd), borderline case (WIDLI) and metaphor signal (MFlag). \nIn addition, the corpus introduces a new 'frame' tag, which gives information about the concept to which it refers.\n", "citation": "@InProceedings{antloga2020komet,\ntitle = {Korpus metafor KOMET 1.0},\nauthor={Antloga, \u000b{S}pela},\nbooktitle={Proceedings of the Conference on Language Technologies and Digital Humanities (Student abstracts)},\nyear={2020},\npages={167-170}\n}\n", "homepage": "http://hdl.handle.net/11356/1293", "license": "Creative Commons - Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)", "features": {"document_name": {"dtype": "string", "id": null, "_type": "Value"}, "idx": {"dtype": "uint32", "id": null, "_type": "Value"}, "idx_paragraph": {"dtype": "uint32", "id": null, "_type": "Value"}, "idx_sentence": {"dtype": "uint32", "id": null, "_type": "Value"}, "sentence_words": {"feature": {"dtype": "string", "id": null, "_type": "Value"}, "length": -1, "id": null, "_type": "Sequence"}, "met_type": {"feature": {"dtype": "string", "id": null, "_type": "Value"}, "length": -1, "id": null, "_type": "Sequence"}, "met_frame": {"feature": {"dtype": "string", "id": null, "_type": "Value"}, "length": -1, "id": null, "_type": "Sequence"}}, "post_processed": null, "supervised_keys": null, "task_templates": null, "builder_name": "komet", "config_name": "default", "version": {"version_str": "1.0.0", "description": null, "major": 1, "minor": 0, "patch": 0}, "splits": {"train": {"name": "train", "num_bytes": 5762717, "num_examples": 13963, "dataset_name": "komet"}}, "download_checksums": {"https://www.clarin.si/repository/xmlui/bitstream/handle/11356/1293/komet.tei.zip": {"num_bytes": 7311643, "checksum": "213f8f5c5b4e4989705a88e014c345fa6038f66e14a83fecb94e08e9f0da6640"}}, "download_size": 7311643, "post_processing_size": null, "dataset_size": 5762717, "size_in_bytes": 13074360}}
|
komet.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Metaphor corpus KOMET 1.0"""
|
2 |
+
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
import xml.etree.ElementTree as ET
|
6 |
+
from typing import List
|
7 |
+
|
8 |
+
import datasets
|
9 |
+
|
10 |
+
_CITATION = """\
|
11 |
+
@InProceedings{antloga2020komet,
|
12 |
+
title = {Korpus metafor KOMET 1.0},
|
13 |
+
author={Antloga, \v{S}pela},
|
14 |
+
booktitle={Proceedings of the Conference on Language Technologies and Digital Humanities (Student abstracts)},
|
15 |
+
year={2020},
|
16 |
+
pages={167-170}
|
17 |
+
}
|
18 |
+
"""
|
19 |
+
|
20 |
+
|
21 |
+
_DESCRIPTION = """\
|
22 |
+
KOMET 1.0 is a hand-annotated corpus for metaphorical expressions which contains about 200,000 words from
|
23 |
+
Slovene journalistic, fiction and on-line texts.
|
24 |
+
|
25 |
+
To annotate metaphors in the corpus an adapted and modified procedure of the MIPVU protocol
|
26 |
+
(Steen et al., 2010: A method for linguistic metaphor identification: From MIP to MIPVU, https://www.benjamins.com/catalog/celcr.14)
|
27 |
+
was used. The lexical units (words) whose contextual meanings are opposed to their basic meanings are considered
|
28 |
+
metaphor-related words. The basic and contextual meaning for each word in the corpus was identified using the
|
29 |
+
Dictionary of the standard Slovene Language. The corpus was annotated for the metaphoric following relations:
|
30 |
+
indirect metaphor (MRWi), direct metaphor (MRWd), borderline case (WIDLI) and metaphor signal (MFlag).
|
31 |
+
In addition, the corpus introduces a new 'frame' tag, which gives information about the concept to which it refers.
|
32 |
+
"""
|
33 |
+
|
34 |
+
_HOMEPAGE = "http://hdl.handle.net/11356/1293"
|
35 |
+
|
36 |
+
_LICENSE = "Creative Commons - Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)"
|
37 |
+
|
38 |
+
_URLS = {
|
39 |
+
"komet": "https://www.clarin.si/repository/xmlui/bitstream/handle/11356/1293/komet.tei.zip"
|
40 |
+
}
|
41 |
+
|
42 |
+
|
43 |
+
def namespace(element):
|
44 |
+
# https://stackoverflow.com/a/12946675
|
45 |
+
m = re.match(r'\{.*\}', element.tag)
|
46 |
+
return m.group(0) if m else ''
|
47 |
+
|
48 |
+
|
49 |
+
def resolve(element) -> List:
|
50 |
+
def _resolve_recursively(element, metaphor_type: str, frame_buffer: List):
|
51 |
+
# Leaf node: word or punctuation character
|
52 |
+
if element.tag.endswith(("w", "pc")):
|
53 |
+
if len(frame_buffer) == 0:
|
54 |
+
return element.text, metaphor_type, "O"
|
55 |
+
else:
|
56 |
+
# Frame annotations may be nested, encode them with a "/" separator;
|
57 |
+
# e.g., the first annotation is the frame of the phrase involving current word and the last annotation
|
58 |
+
# is the frame of a phrase part
|
59 |
+
return element.text, metaphor_type, "/".join(frame_buffer)
|
60 |
+
|
61 |
+
# Annotated word or word group
|
62 |
+
elif element.tag.endswith("seg"):
|
63 |
+
mtype, new_frame_buffer = "O", frame_buffer
|
64 |
+
if element.attrib["subtype"] != "frame":
|
65 |
+
mtype = element.attrib["subtype"]
|
66 |
+
else:
|
67 |
+
# Frame annotations in KOMET are prepended with "#met.", while those in GKomet are not: unify
|
68 |
+
if element.attrib["ana"].startswith("#met."):
|
69 |
+
_mframe = element.attrib["ana"][5:]
|
70 |
+
else:
|
71 |
+
_mframe = element.attrib["ana"]
|
72 |
+
new_frame_buffer.append(_mframe)
|
73 |
+
|
74 |
+
parsed_data = []
|
75 |
+
for child in element:
|
76 |
+
# spaces between words, skip
|
77 |
+
if child.tag.endswith("c"):
|
78 |
+
continue
|
79 |
+
|
80 |
+
res = _resolve_recursively(child, mtype, new_frame_buffer)
|
81 |
+
if isinstance(res, list):
|
82 |
+
parsed_data.extend(res)
|
83 |
+
else:
|
84 |
+
parsed_data.append(res)
|
85 |
+
|
86 |
+
return parsed_data
|
87 |
+
|
88 |
+
curr_annotations = _resolve_recursively(element, "O", [])
|
89 |
+
if not isinstance(curr_annotations, list):
|
90 |
+
curr_annotations = [curr_annotations]
|
91 |
+
|
92 |
+
return curr_annotations
|
93 |
+
|
94 |
+
|
95 |
+
class Komet(datasets.GeneratorBasedBuilder):
|
96 |
+
"""KOMET is a hand-annotated Slovenian corpus of metaphorical expressions."""
|
97 |
+
|
98 |
+
VERSION = datasets.Version("1.0.0")
|
99 |
+
|
100 |
+
def _info(self):
|
101 |
+
features = datasets.Features(
|
102 |
+
{
|
103 |
+
"document_name": datasets.Value("string"),
|
104 |
+
"idx": datasets.Value("uint32"), # index inside current document
|
105 |
+
"idx_paragraph": datasets.Value("uint32"),
|
106 |
+
"idx_sentence": datasets.Value("uint32"), # index inside current paragraph
|
107 |
+
"sentence_words": datasets.Sequence(datasets.Value("string")),
|
108 |
+
"met_type": datasets.Sequence(datasets.Value("string")),
|
109 |
+
"met_frame": datasets.Sequence(datasets.Value("string"))
|
110 |
+
}
|
111 |
+
)
|
112 |
+
return datasets.DatasetInfo(
|
113 |
+
description=_DESCRIPTION,
|
114 |
+
features=features,
|
115 |
+
homepage=_HOMEPAGE,
|
116 |
+
license=_LICENSE,
|
117 |
+
citation=_CITATION,
|
118 |
+
)
|
119 |
+
|
120 |
+
def _split_generators(self, dl_manager):
|
121 |
+
data_dir = dl_manager.download_and_extract(_URLS["komet"])
|
122 |
+
return [
|
123 |
+
datasets.SplitGenerator(
|
124 |
+
name=datasets.Split.TRAIN,
|
125 |
+
gen_kwargs={"data_dir": os.path.join(data_dir, "komet.tei")},
|
126 |
+
)
|
127 |
+
]
|
128 |
+
|
129 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
130 |
+
def _generate_examples(self, data_dir):
|
131 |
+
data_files = []
|
132 |
+
for fname in os.listdir(data_dir):
|
133 |
+
curr_path = os.path.join(data_dir, fname)
|
134 |
+
if os.path.isfile(curr_path) and fname.endswith(".xml") and fname != "komet.xml": # komet.xml = meta-file
|
135 |
+
data_files.append(fname)
|
136 |
+
|
137 |
+
idx_example = 0
|
138 |
+
for fname in data_files:
|
139 |
+
fpath = os.path.join(data_dir, fname)
|
140 |
+
|
141 |
+
curr_doc = ET.parse(fpath)
|
142 |
+
root = curr_doc.getroot()
|
143 |
+
NAMESPACE = namespace(root)
|
144 |
+
|
145 |
+
idx_sent_glob = 0
|
146 |
+
for idx_par, curr_par in enumerate(root.iterfind(f"{NAMESPACE}p")):
|
147 |
+
for idx_sent, curr_sent in enumerate(curr_par.iterfind(f"{NAMESPACE}s")):
|
148 |
+
words, types, frames = [], [], []
|
149 |
+
for curr_el in curr_sent:
|
150 |
+
if curr_el.tag.endswith(("w", "pc", "seg")):
|
151 |
+
curr_res = resolve(curr_el)
|
152 |
+
for _el in curr_res:
|
153 |
+
words.append(_el[0])
|
154 |
+
types.append(_el[1])
|
155 |
+
frames.append(_el[2])
|
156 |
+
|
157 |
+
yield idx_example, {
|
158 |
+
"document_name": fname,
|
159 |
+
"idx": idx_sent_glob,
|
160 |
+
"idx_paragraph": idx_par,
|
161 |
+
"idx_sentence": idx_sent,
|
162 |
+
"sentence_words": words,
|
163 |
+
"met_type": types,
|
164 |
+
"met_frame": frames
|
165 |
+
}
|
166 |
+
idx_example += 1
|
167 |
+
idx_sent_glob += 1
|