christophalt commited on
Commit
1703cb4
·
1 Parent(s): 85ef67c

Upload conll2003.py

Browse files
Files changed (1) hide show
  1. conll2003.py +13 -24
conll2003.py CHANGED
@@ -2,15 +2,17 @@ from dataclasses import dataclass
2
 
3
  import datasets
4
  import pytorch_ie.data.builder
5
- from pytorch_ie import AnnotationList, LabeledSpan, TextDocument, annotation_field
6
- from pytorch_ie.utils.span import bio_tags_to_spans
 
 
7
 
8
 
9
- class Conll2003Config(datasets.BuilderConfig):
10
- """BuilderConfig for Conll2003"""
11
 
12
  def __init__(self, **kwargs):
13
- """BuilderConfig forConll2003.
14
  Args:
15
  **kwargs: keyword arguments forwarded to super.
16
  """
@@ -28,8 +30,8 @@ class Conll2003(pytorch_ie.data.builder.GeneratorBasedBuilder):
28
  BASE_DATASET_PATH = "conll2003"
29
 
30
  BUILDER_CONFIGS = [
31
- Conll2003Config(
32
- name="conll2003", version=datasets.Version("1.0.0"), description="Conll2003 dataset"
33
  ),
34
  ]
35
 
@@ -39,26 +41,13 @@ class Conll2003(pytorch_ie.data.builder.GeneratorBasedBuilder):
39
  def _generate_document(self, example, int_to_str):
40
  doc_id = example["id"]
41
  tokens = example["tokens"]
42
- ner_tags = example["ner_tags"]
43
 
44
- start = 0
45
- token_offsets = []
46
- tag_sequence = []
47
- for token, tag_id in zip(tokens, ner_tags):
48
- end = start + len(token)
49
- token_offsets.append((start, end))
50
- tag_sequence.append(int_to_str(tag_id))
51
-
52
- start = end + 1
53
-
54
- text = " ".join(tokens)
55
- spans = bio_tags_to_spans(tag_sequence)
56
 
57
  document = CoNLL2003Document(text=text, id=doc_id)
58
 
59
- for label, (start, end) in spans:
60
- start_offset = token_offsets[start][0]
61
- end_offset = token_offsets[end][1]
62
- document.entities.append(LabeledSpan(start=start_offset, end=end_offset, label=label))
63
 
64
  return document
 
2
 
3
  import datasets
4
  import pytorch_ie.data.builder
5
+ from pytorch_ie import AnnotationList, annotation_field
6
+ from pytorch_ie.annotations import LabeledSpan
7
+ from pytorch_ie.documents import TextDocument
8
+ from pytorch_ie.utils.span import tokens_and_tags_to_text_and_labeled_spans
9
 
10
 
11
+ class CoNLL2003Config(datasets.BuilderConfig):
12
+ """BuilderConfig for CoNLL2003"""
13
 
14
  def __init__(self, **kwargs):
15
+ """BuilderConfig for CoNLL2003.
16
  Args:
17
  **kwargs: keyword arguments forwarded to super.
18
  """
 
30
  BASE_DATASET_PATH = "conll2003"
31
 
32
  BUILDER_CONFIGS = [
33
+ CoNLL2003Config(
34
+ name="conll2003", version=datasets.Version("1.0.0"), description="CoNLL2003 dataset"
35
  ),
36
  ]
37
 
 
41
  def _generate_document(self, example, int_to_str):
42
  doc_id = example["id"]
43
  tokens = example["tokens"]
44
+ ner_tags = [int_to_str(tag) for tag in example["ner_tags"]]
45
 
46
+ text, ner_spans = tokens_and_tags_to_text_and_labeled_spans(tokens=tokens, tags=ner_tags)
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  document = CoNLL2003Document(text=text, id=doc_id)
49
 
50
+ for span in sorted(ner_spans, key=lambda span: span.start):
51
+ document.entities.append(span)
 
 
52
 
53
  return document