ArneBinder commited on
Commit
c66e4e5
·
1 Parent(s): 3b2dec7

add DOCUMENT_CONVERTERS

Browse files
Files changed (1) hide show
  1. conll2012_ontonotesv5.py +42 -2
conll2012_ontonotesv5.py CHANGED
@@ -1,12 +1,12 @@
1
  import dataclasses
2
- import itertools
3
  from collections import defaultdict
4
  from typing import Any, Callable, Dict, List, Optional, Tuple
5
 
6
  import datasets
7
  import pytorch_ie
8
- from pytorch_ie.annotations import BinaryRelation, LabeledSpan, NaryRelation, Span
9
  from pytorch_ie.core import Annotation, AnnotationList, Document, annotation_field
 
10
 
11
 
12
  @dataclasses.dataclass(eq=True, frozen=True)
@@ -229,6 +229,42 @@ def example_to_document(
229
  return doc
230
 
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  class Conll2012OntonotesV5Config(datasets.BuilderConfig):
233
  """BuilderConfig for the CoNLL formatted OntoNotes dataset."""
234
 
@@ -257,6 +293,10 @@ class Conll2012OntonotesV5Config(datasets.BuilderConfig):
257
  class Conll2012Ontonotesv5(pytorch_ie.data.builder.GeneratorBasedBuilder):
258
  DOCUMENT_TYPE = Conll2012OntonotesV5Document
259
 
 
 
 
 
260
  BASE_DATASET_PATH = "DFKI-SLT/conll2012_ontonotesv5"
261
 
262
  BUILDER_CONFIGS = [
 
1
  import dataclasses
 
2
  from collections import defaultdict
3
  from typing import Any, Callable, Dict, List, Optional, Tuple
4
 
5
  import datasets
6
  import pytorch_ie
7
+ from pytorch_ie.annotations import LabeledSpan, NaryRelation, Span
8
  from pytorch_ie.core import Annotation, AnnotationList, Document, annotation_field
9
+ from pytorch_ie.documents import TextDocumentWithLabeledEntitiesAndLabeledPartitions
10
 
11
 
12
  @dataclasses.dataclass(eq=True, frozen=True)
 
229
  return doc
230
 
231
 
232
+ def token_based_document_with_entities_and_sentences_to_text_based(
233
+ doc: Conll2012OntonotesV5Document,
234
+ token_separator: str = " ",
235
+ ) -> TextDocumentWithLabeledEntitiesAndLabeledPartitions:
236
+ start = 0
237
+ token_offsets: List[Tuple[int, int]] = []
238
+ for token in doc.tokens:
239
+ end = start + len(token)
240
+ token_offsets.append((start, end))
241
+ # we add the separator after each token
242
+ start = end + len(token_separator)
243
+
244
+ text = token_separator.join([token for token in doc.tokens])
245
+
246
+ entity_map: Dict[Tuple[int, int], LabeledSpan] = {}
247
+
248
+ for entity in doc.entities:
249
+ char_start = token_offsets[entity.start][0]
250
+ char_end = token_offsets[entity.end - 1][1]
251
+ char_offset_entity = LabeledSpan(start=char_start, end=char_end, label=entity.label)
252
+ entity_map[(entity.start, entity.end)] = char_offset_entity
253
+
254
+ sentence_map: Dict[Tuple[int, int], LabeledSpan] = {}
255
+ for sentence in doc.sentences:
256
+ char_start = token_offsets[sentence.start][0]
257
+ char_end = token_offsets[sentence.end - 1][1]
258
+ char_offset_sentence = LabeledSpan(start=char_start, end=char_end, label="sentence")
259
+ sentence_map[(sentence.start, sentence.end)] = char_offset_sentence
260
+
261
+ new_doc = TextDocumentWithLabeledEntitiesAndLabeledPartitions(text=text, id=doc.id)
262
+ new_doc.entities.extend(entity_map.values())
263
+ new_doc.partitions.extend(sentence_map.values())
264
+
265
+ return new_doc
266
+
267
+
268
  class Conll2012OntonotesV5Config(datasets.BuilderConfig):
269
  """BuilderConfig for the CoNLL formatted OntoNotes dataset."""
270
 
 
293
  class Conll2012Ontonotesv5(pytorch_ie.data.builder.GeneratorBasedBuilder):
294
  DOCUMENT_TYPE = Conll2012OntonotesV5Document
295
 
296
+ DOCUMENT_CONVERTERS = {
297
+ TextDocumentWithLabeledEntitiesAndLabeledPartitions: token_based_document_with_entities_and_sentences_to_text_based
298
+ }
299
+
300
  BASE_DATASET_PATH = "DFKI-SLT/conll2012_ontonotesv5"
301
 
302
  BUILDER_CONFIGS = [