Upload CLISTER.py
Browse files- CLISTER.py +156 -0
CLISTER.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
import numpy as np
|
7 |
+
import pandas as pd
|
8 |
+
|
9 |
+
_CITATION = """\
|
10 |
+
@inproceedings{hiebel:cea-03740484,
|
11 |
+
TITLE = {{CLISTER: A corpus for semantic textual similarity in French clinical narratives}},
|
12 |
+
AUTHOR = {Hiebel, Nicolas and Ferret, Olivier and Fort, Kar{\"e}n and N{\'e}v{\'e}ol, Aur{\'e}lie},
|
13 |
+
URL = {https://hal-cea.archives-ouvertes.fr/cea-03740484},
|
14 |
+
BOOKTITLE = {{LREC 2022 - 13th Language Resources and Evaluation Conference}},
|
15 |
+
ADDRESS = {Marseille, France},
|
16 |
+
PUBLISHER = {{European Language Resources Association}},
|
17 |
+
SERIES = {LREC 2022 - Proceedings of the 13th Conference on Language Resources and Evaluation},
|
18 |
+
VOLUME = {2022},
|
19 |
+
PAGES = {4306‑4315},
|
20 |
+
YEAR = {2022},
|
21 |
+
MONTH = Jun,
|
22 |
+
KEYWORDS = {Semantic Similarity ; Corpus Development ; Clinical Text ; French ; Semantic Similarity},
|
23 |
+
PDF = {https://hal-cea.archives-ouvertes.fr/cea-03740484/file/2022.lrec-1.459.pdf},
|
24 |
+
HAL_ID = {cea-03740484},
|
25 |
+
HAL_VERSION = {v1},
|
26 |
+
}
|
27 |
+
"""
|
28 |
+
|
29 |
+
_DESCRIPTION = """\
|
30 |
+
Modern Natural Language Processing relies on the availability of annotated corpora for training and \
|
31 |
+
evaluating models. Such resources are scarce, especially for specialized domains in languages other \
|
32 |
+
than English. In particular, there are very few resources for semantic similarity in the clinical domain \
|
33 |
+
in French. This can be useful for many biomedical natural language processing applications, including \
|
34 |
+
text generation. We introduce a definition of similarity that is guided by clinical facts and apply it \
|
35 |
+
to the development of a new French corpus of 1,000 sentence pairs manually annotated according to \
|
36 |
+
similarity scores. This new sentence similarity corpus is made freely available to the community. We \
|
37 |
+
further evaluate the corpus through experiments of automatic similarity measurement. We show that a \
|
38 |
+
model of sentence embeddings can capture similarity with state of the art performance on the DEFT STS \
|
39 |
+
shared task evaluation data set (Spearman=0.8343). We also show that the CLISTER corpus is complementary \
|
40 |
+
to DEFT STS. \
|
41 |
+
"""
|
42 |
+
|
43 |
+
_HOMEPAGE = "https://gitlab.inria.fr/codeine/clister"
|
44 |
+
|
45 |
+
_LICENSE = "unknown"
|
46 |
+
|
47 |
+
class CLISTER(datasets.GeneratorBasedBuilder):
|
48 |
+
|
49 |
+
DEFAULT_CONFIG_NAME = "source"
|
50 |
+
|
51 |
+
BUILDER_CONFIGS = [
|
52 |
+
datasets.BuilderConfig(name="source", version="1.0.0", description="The CLISTER corpora"),
|
53 |
+
]
|
54 |
+
|
55 |
+
def _info(self):
|
56 |
+
|
57 |
+
features = datasets.Features(
|
58 |
+
{
|
59 |
+
"id": datasets.Value("string"),
|
60 |
+
"document_1_id": datasets.Value("string"),
|
61 |
+
"document_2_id": datasets.Value("string"),
|
62 |
+
"text_1": datasets.Value("string"),
|
63 |
+
"text_2": datasets.Value("string"),
|
64 |
+
"label": datasets.Value("float"),
|
65 |
+
}
|
66 |
+
)
|
67 |
+
|
68 |
+
return datasets.DatasetInfo(
|
69 |
+
description=_DESCRIPTION,
|
70 |
+
features=features,
|
71 |
+
supervised_keys=None,
|
72 |
+
homepage=_HOMEPAGE,
|
73 |
+
license=str(_LICENSE),
|
74 |
+
citation=_CITATION,
|
75 |
+
)
|
76 |
+
|
77 |
+
def _split_generators(self, dl_manager):
|
78 |
+
|
79 |
+
data_dir = self.config.data_dir.rstrip("/")
|
80 |
+
|
81 |
+
return [
|
82 |
+
datasets.SplitGenerator(
|
83 |
+
name=datasets.Split.TRAIN,
|
84 |
+
gen_kwargs={
|
85 |
+
"csv_file": data_dir + "train.csv",
|
86 |
+
"json_file": data_dir + "id_to_sentence_train.json",
|
87 |
+
"split": "train",
|
88 |
+
},
|
89 |
+
),
|
90 |
+
datasets.SplitGenerator(
|
91 |
+
name=datasets.Split.VALIDATION,
|
92 |
+
gen_kwargs={
|
93 |
+
"csv_file": data_dir + "train.csv",
|
94 |
+
"json_file": data_dir + "id_to_sentence_train.json",
|
95 |
+
"split": "validation",
|
96 |
+
},
|
97 |
+
),
|
98 |
+
datasets.SplitGenerator(
|
99 |
+
name=datasets.Split.TEST,
|
100 |
+
gen_kwargs={
|
101 |
+
"csv_file": data_dir + "test.csv",
|
102 |
+
"json_file": data_dir + "id_to_sentence_test.json",
|
103 |
+
"split": "test",
|
104 |
+
},
|
105 |
+
),
|
106 |
+
]
|
107 |
+
|
108 |
+
def _generate_examples(self, csv_file, json_file, split):
|
109 |
+
|
110 |
+
all_res = []
|
111 |
+
|
112 |
+
key = 0
|
113 |
+
|
114 |
+
# Load JSON file
|
115 |
+
f_json = open(json_file)
|
116 |
+
data_map = json.load(f_json)
|
117 |
+
f_json.close()
|
118 |
+
|
119 |
+
# Load CSV file
|
120 |
+
df = pd.read_csv(csv_file, sep="\t")
|
121 |
+
print(df)
|
122 |
+
|
123 |
+
for index, e in df.iterrows():
|
124 |
+
|
125 |
+
all_res.append({
|
126 |
+
"id": str(key),
|
127 |
+
"document_1_id": e["id_1"],
|
128 |
+
"document_2_id": e["id_2"],
|
129 |
+
"text_1": data_map[e["id_1"]],
|
130 |
+
"text_2": data_map[e["id_2"]],
|
131 |
+
"label": e["sim"],
|
132 |
+
})
|
133 |
+
|
134 |
+
if split != "test":
|
135 |
+
|
136 |
+
ids = [r["id"] for r in all_res]
|
137 |
+
|
138 |
+
random.seed(4)
|
139 |
+
random.shuffle(ids)
|
140 |
+
random.shuffle(ids)
|
141 |
+
random.shuffle(ids)
|
142 |
+
|
143 |
+
train, validation = np.split(ids, [int(len(ids)*0.8333)])
|
144 |
+
|
145 |
+
if split == "train":
|
146 |
+
allowed_ids = list(train)
|
147 |
+
elif split == "validation":
|
148 |
+
allowed_ids = list(validation)
|
149 |
+
|
150 |
+
for r in all_res:
|
151 |
+
if r["id"] in allowed_ids:
|
152 |
+
yield r["id"], r
|
153 |
+
else:
|
154 |
+
|
155 |
+
for r in all_res:
|
156 |
+
yield r["id"], r
|