File size: 6,922 Bytes
d86d5c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8ed1af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d86d5c0
 
b8ed1af
 
d86d5c0
 
b8ed1af
d86d5c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# coding=utf-8
# Copyright 2020 HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Lint as: python3
"""Introduction to the CoNLL-2003 Shared Task: Language-Independent Named Entity Recognition"""

import re

import datasets


logger = datasets.logging.get_logger(__name__)


_CITATION = """\
@Article{SETH2016,
  Title= {SETH detects and normalizes genetic variants in text.},
  Author= {Thomas, Philippe and Rockt{\"{a}}schel, Tim and Hakenberg, J{\"{o}}rg and Lichtblau, Yvonne and Leser, Ulf},
  Journal= {Bioinformatics},
  Year= {2016},
  Month= {Jun},
  Doi= {10.1093/bioinformatics/btw234},  
  Language = {eng},
  Medline-pst = {aheadofprint},
  Pmid = {27256315},
  Url = {http://dx.doi.org/10.1093/bioinformatics/btw234} Titel anhand dieser DOI in Citavi-Projekt übernehmen
}
"""

_DESCRIPTION = """\
This Dataset is used to for the Advanced Machine Learning and XAI course of the DHBW CAS in Heilbronn
"""



class SethConfig(datasets.BuilderConfig):
    """BuilderConfig for Seth Dataset"""

    def __init__(self, **kwargs):
        """BuilderConfig for Seth.

        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(SethConfig, self).__init__(**kwargs)


class Seth(datasets.GeneratorBasedBuilder):
    """Seth dataset."""

    BUILDER_CONFIGS = [
        SethConfig(name="Seth", version=datasets.Version("1.0.0"), description="Seth dataset"),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("int32"),
                    "tokens": datasets.Sequence(datasets.Value("string")),
                    "labels": datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=[
                                "O",
                                "B-Gene",
                                "B-SNP",
                                "I-SNP",
                                "I-Gene",
                                "B-RS",
                                "I-RS"
                            ]
                        )
                    )
                }
            ),
            supervised_keys=None,
            homepage="https://rockt.github.io/SETH/",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        # Dateien herunterladen
        downloaded_files = dl_manager.download({
            "train": "https://raw.githubusercontent.com/Erechtheus/mutationCorpora/master/corpora/IOB/SETH-train.iob",
            "test": "https://raw.githubusercontent.com/Erechtheus/mutationCorpora/master/corpora/IOB/SETH-test.iob",
        })

        # Logging, um die Dateipfade und Inhalte zu überprüfen
        logger.info(f"Train file downloaded to: {downloaded_files['train']}")
        logger.info(f"Test file downloaded to: {downloaded_files['test']}")

        # Inhalte der Dateien anzeigen (optional)
        with open(downloaded_files["train"], 'r') as train_file:
            logger.info(f"First few lines of train file: {train_file.readlines()[:5]}")
        with open(downloaded_files["test"], 'r') as test_file:
            logger.info(f"First few lines of test file: {test_file.readlines()[:5]}")

        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
        ]


    def _generate_examples(self, filepath):
        logger.info("⏳ Generating examples from = %s", filepath)
        with open(filepath, encoding="utf-8") as f:
            guid = 0
            document = {"id":None,
            "tokens":[],
            "labels":[]
            }
            documents = [] # Wird befüllt mit den Documented aus der Datei. Besteht aus einem Key "tokens" und "labels"
            pattern = r"#\d+" # Reg Experassion um eine Documented ID zu detektieren
            for idx, line in enumerate(f):
                match = re.match(pattern, line)
        #Überspringe erste Zeile weil das ein Header ist
                if idx == 0:
                    continue
                # Here ist die Dokumenten ID
                if match:
                    if document["id"] != None:
                        # Speichere altes Dokument bevor ein neues angelegt wird
                        documents.append(document)
                        yield guid,document
                        guid+=1
                        document = {"id":int(line[1:]), # Speichere nur die Nummer ohne die Raute
                                    "tokens":[],
                                    "labels":[]
                                    }
                    else:
                        #Initialisiere neues DOkument
                        document = {"id":int(line[1:]), # Speichere nur die Nummer ohne die Raute
                                    "tokens":[],
                                    "labels":[]
                                    }
                # Hier handeln wir die Sonderfälle ab
                elif len(line.split(",")) >2:
                    # Sonderfall 1: ,,Label
                    if(line.split(",")[0] == "" and line.split(",")[1]==""):
                        document["tokens"].append(",")
                        document["labels"].append(line.split(",")[2].split("\n")[0])
                    # Sonderfall 2:Text,Text,Test,Label -> Label steht immer am schluss
                    else:
                        document["tokens"].append(",".join(line.split(",")[0:-1])) # Bringe die Splits wieder zusammen ohne das Label
                        document["labels"].append(line.split(",")[-1].split("\n")[0])     
                # Sonst gehen wir einfach vom standard aus Word sowie Tag
                else:
                    word_tag = line.split(",")
                # Hier erkennen wir den Ende eines Satzes dieser besteht aus " , "
                    if word_tag[0] == " " and word_tag[1] == " \n":
                        continue
                    document["tokens"].append(word_tag[0])
                    document["labels"].append(word_tag[1].split("\n")[0])
            documents.append(document)
            yield guid,document