patriziobellan commited on
Commit
3ef6de1
1 Parent(s): 2ed0813

Upload PET.py

Browse files
Files changed (1) hide show
  1. PET.py +187 -0
PET.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import json
3
+ import os
4
+
5
+ import datasets
6
+
7
+
8
+ # Find for instance the citation on arxiv or on the dataset repo/website
9
+ _CITATION = """\
10
+ @article{DBLP:journals/corr/abs-2203-04860,
11
+ author = {Patrizio Bellan and
12
+ Han van der Aa and
13
+ Mauro Dragoni and
14
+ Chiara Ghidini and
15
+ Simone Paolo Ponzetto},
16
+ title = {{PET:} {A} new Dataset for Process Extraction from Natural Language
17
+ Text},
18
+ journal = {CoRR},
19
+ volume = {abs/2203.04860},
20
+ year = {2022},
21
+ url = {https://doi.org/10.48550/arXiv.2203.04860},
22
+ doi = {10.48550/arXiv.2203.04860},
23
+ eprinttype = {arXiv},
24
+ eprint = {2203.04860},
25
+ biburl = {https://dblp.org/rec/journals/corr/abs-2203-04860.bib}
26
+ }
27
+ """
28
+
29
+ # You can copy an official description
30
+ _DESCRIPTION = """\
31
+ Abstract. Although there is a long tradition of work in NLP on extracting entities and relations from text, to date there exists little work on the acquisition of business processes from unstructured data such as textual corpora of process descriptions. With this work we aim at filling this gap and establishing the first steps towards bridging data-driven information extraction methodologies from Natural Language Processing and the model-based formalization that is aimed from Business Process Management. For this, we develop the first corpus of business process descriptions annotated with activities, gateways, actors and flow information. We present our new resource, including a detailed overview of the annotation schema and guidelines, as well as a variety of baselines to benchmark the difficulty and challenges of business process extraction from text.
32
+ """
33
+
34
+ _HOMEPAGE = "https://pdi.fbk.eu/pet-dataset/"
35
+
36
+ _LICENSE = "MIT"
37
+
38
+ _URL = "https://pdi.fbk.eu/pet/PETHuggingFace/"
39
+ _TEST_FILE = "test.json"
40
+ _TEST_FILE_RELATIONS = 'PETrelations.json'
41
+
42
+ _NER = 'token-classification'
43
+ _RELATIONS_EXTRACTION = 'relations-extraction'
44
+ _NER_TAGS = [ "O",
45
+ "B-Actor",
46
+ "I-Actor",
47
+ "B-Activity",
48
+ "I-Activity",
49
+ "B-Activity Data",
50
+ "I-Activity Data",
51
+ "B-Further Specification",
52
+ "I-Further Specification",
53
+ "B-XOR Gateway",
54
+ "I-XOR Gateway",
55
+ "B-Condition Specification",
56
+ "I-Condition Specification",
57
+ "B-AND Gateway",
58
+ "I-AND Gateway"]
59
+
60
+ _STR_PET = """\n
61
+ _______ _ _ _______ _____ _______ _______ ______ _______ _______ _______ _______ _______ _______
62
+ | |_____| |______ |_____] |______ | | \ |_____| | |_____| |______ |______ |
63
+ | | | |______ | |______ | |_____/ | | | | | ______| |______ |
64
+
65
+ Discover more at: [https://pdi.fbk.eu/pet-dataset/]
66
+ """
67
+
68
+
69
+ class PETConfig(datasets.BuilderConfig):
70
+ """The PET Dataset."""
71
+
72
+ def __init__(self, **kwargs):
73
+ """BuilderConfig for PET.
74
+ Args:
75
+ **kwargs: keyword arguments forwarded to super.
76
+ """
77
+ super(PETConfig, self).__init__(**kwargs)
78
+
79
+ class PET(datasets.GeneratorBasedBuilder):
80
+ """PET DATASET."""
81
+
82
+ features_ner = {
83
+ "document name": datasets.Value("string"),
84
+ "sentence-ID": datasets.Value("int8"),
85
+ "tokens": datasets.Sequence(datasets.Value("string")),
86
+ "ner-tags": datasets.Sequence(datasets.features.ClassLabel(names=_NER_TAGS)),
87
+ }
88
+
89
+ features_relations = datasets.Sequence(
90
+ datasets.Features(
91
+
92
+ {
93
+ 'source-head-sentence-ID': datasets.Value("int8"),
94
+ 'source-head-word-ID': datasets.Value("int8"),
95
+ 'relation-type': datasets.Value("string"),
96
+ 'target-head-sentence-ID': datasets.Value("int8"),
97
+ 'target-head-word-ID' : datasets.Value("int8"),
98
+ }
99
+ ))
100
+ BUILDER_CONFIGS = [ PETConfig(
101
+ name=_NER,
102
+ version=datasets.Version("1.0.1"),
103
+ description="The PET Dataset for Token Classification"
104
+ ),
105
+ PETConfig(
106
+ name=_RELATIONS_EXTRACTION,
107
+ version=datasets.Version("1.0.1"),
108
+ description="The PET Dataset for Relation Extraction"
109
+ ),
110
+ ]
111
+
112
+ DEFAULT_CONFIG_NAME = _RELATIONS_EXTRACTION
113
+
114
+ def _info(self):
115
+ print(_STR_PET)
116
+ if self.config.name == _NER:
117
+ features = datasets.Features(self.features_ner)
118
+ else:
119
+ features = datasets.Features(
120
+ {
121
+ "document name": datasets.Value("string"),
122
+ 'tokens':datasets.Sequence(datasets.Value("string")),
123
+ 'tokens-IDs':datasets.Sequence(datasets.Value("int8")),
124
+ 'ner_tags': datasets.Sequence(datasets.Value("string")),
125
+ 'sentence-IDs':datasets.Sequence(datasets.Value("int8")),
126
+ "relations": self.features_relations
127
+ }
128
+ )
129
+ # print(features)
130
+ return datasets.DatasetInfo(
131
+ description=_DESCRIPTION,
132
+ features=datasets.Features(features),
133
+ homepage=_HOMEPAGE,
134
+ license=_LICENSE,
135
+ citation=_CITATION,
136
+ )
137
+
138
+ def _split_generators(self, dl_manager):
139
+ if self.config.name == _NER:
140
+ urls_to_download = {
141
+ "test": f"{_URL}{_TEST_FILE}",
142
+ }
143
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
144
+ return [datasets.SplitGenerator(
145
+ name=datasets.Split.TEST,
146
+ # These kwargs will be passed to _generate_examples
147
+ gen_kwargs={
148
+ "filepath": downloaded_files["test"],
149
+ "split": "test"
150
+ },
151
+ )]
152
+ else:
153
+ urls_to_download = {
154
+ "test": f"{_URL}{_TEST_FILE_RELATIONS}",
155
+ }
156
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
157
+ return [datasets.SplitGenerator(
158
+ name=datasets.Split.TEST,
159
+ # These kwargs will be passed to _generate_examples
160
+ gen_kwargs={
161
+ "filepath": downloaded_files["test"],
162
+ "split": "test"
163
+ },
164
+ )]
165
+
166
+ def _generate_examples(self, filepath, split):
167
+ if self.config.name == _NER:
168
+ with open(filepath, encoding="utf-8", mode='r') as f:
169
+ for key, row in enumerate(f):
170
+ row = json.loads(row)
171
+ yield key, {
172
+ "document name": row["document name"],
173
+ "sentence-ID": row["sentence-ID"],
174
+ "tokens": row["tokens"],
175
+ "ner-tags": row["ner-tags"]
176
+ }
177
+ else:
178
+ with open(filepath, encoding="utf-8", mode='r') as f:
179
+ for key, row in enumerate(json.load(f)):
180
+ yield key, {"document name": row["document name"],
181
+ 'tokens': row["tokens"],
182
+ 'tokens-IDs': row["tokens-IDs"],
183
+ 'ner_tags': row["ner_tags"],
184
+ 'sentence-IDs': row["sentence-IDs"],
185
+
186
+ "relations": row["relations"]
187
+ }