ArneBinder commited on
Commit
12bc171
·
verified ·
1 Parent(s): b3bbb55

use pie-modules instead of pytorch-ie

Browse files

see https://github.com/ArneBinder/pie-datasets/pull/204 for further information

Files changed (3) hide show
  1. README.md +3 -3
  2. conll2003.py +29 -6
  3. requirements.txt +2 -1
README.md CHANGED
@@ -15,13 +15,13 @@ and the following annotation layers:
15
 
16
  - `entities` (annotation type: `LabeledSpan`, target: `text`)
17
 
18
- See [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/annotations.py) for the annotation type definitions.
19
 
20
  ## Document Converters
21
 
22
  The dataset provides document converters for the following target document types:
23
 
24
- - `pytorch_ie.documents.TextDocumentWithLabeledSpans`
25
 
26
- See [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/documents.py) for the document type
27
  definitions.
 
15
 
16
  - `entities` (annotation type: `LabeledSpan`, target: `text`)
17
 
18
+ See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/annotations.py) for the annotation type definitions.
19
 
20
  ## Document Converters
21
 
22
  The dataset provides document converters for the following target document types:
23
 
24
+ - `pie_modules.documents.TextDocumentWithLabeledSpans`
25
 
26
+ See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/documents.py) for the document type
27
  definitions.
conll2003.py CHANGED
@@ -1,17 +1,40 @@
1
  from dataclasses import dataclass
 
2
 
3
  import datasets
4
- from pytorch_ie.annotations import LabeledSpan
5
- from pytorch_ie.core import AnnotationList, annotation_field
6
- from pytorch_ie.documents import TextDocument, TextDocumentWithLabeledSpans
7
- from pytorch_ie.utils.span import tokens_and_tags_to_text_and_labeled_spans
8
 
9
  from pie_datasets import GeneratorBasedBuilder
10
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  @dataclass
13
- class CoNLL2003Document(TextDocument):
14
- entities: AnnotationList[LabeledSpan] = annotation_field(target="text")
15
 
16
 
17
  class Conll2003(GeneratorBasedBuilder):
 
1
  from dataclasses import dataclass
2
+ from typing import List, Sequence, Tuple
3
 
4
  import datasets
5
+ from pie_core import AnnotationLayer, annotation_field
6
+ from pie_modules.annotations import LabeledSpan
7
+ from pie_modules.documents import TextBasedDocument, TextDocumentWithLabeledSpans
8
+ from pie_modules.utils.sequence_tagging import tag_sequence_to_token_spans
9
 
10
  from pie_datasets import GeneratorBasedBuilder
11
 
12
 
13
+ def tokens_and_tags_to_text_and_labeled_spans(
14
+ tokens: Sequence[str], tags: Sequence[str]
15
+ ) -> Tuple[str, Sequence[LabeledSpan]]:
16
+ start = 0
17
+ token_offsets: List[Tuple[int, int]] = []
18
+ for token in tokens:
19
+ end = start + len(token)
20
+ token_offsets.append((start, end))
21
+ # we add a space after each token
22
+ start = end + 1
23
+
24
+ text = " ".join(tokens)
25
+
26
+ spans: List[LabeledSpan] = []
27
+ for label, (start, end) in tag_sequence_to_token_spans(tag_sequence=tags):
28
+ spans.append(
29
+ LabeledSpan(start=token_offsets[start][0], end=token_offsets[end][1], label=label)
30
+ )
31
+
32
+ return text, spans
33
+
34
+
35
  @dataclass
36
+ class CoNLL2003Document(TextBasedDocument):
37
+ entities: AnnotationLayer[LabeledSpan] = annotation_field(target="text")
38
 
39
 
40
  class Conll2003(GeneratorBasedBuilder):
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- pie-datasets>=0.8.1,<0.9.0
 
 
1
+ pie-datasets>=0.10.11,<0.11.0
2
+ pie-modules>=0.15.9,<0.16.0