ayuhamaro commited on
Commit
7cea515
·
1 Parent(s): 41c9ff7

Create nlp-model-tune.py

Browse files
Files changed (1) hide show
  1. nlp-model-tune.py +170 -0
nlp-model-tune.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 HuggingFace Datasets Authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # Lint as: python3
17
+ import datasets
18
+
19
+
20
+ _DESCRIPTION = ""
21
+ _HOMEPAGE_URL = ""
22
+ _CITATION = None
23
+ _TRAIN_URL = "https://huggingface.co/datasets/ayuhamaro/nlp-model-tune/blob/main/train"
24
+
25
+
26
+ class WeiboNERCorpus(datasets.GeneratorBasedBuilder):
27
+ VERSION = datasets.Version("1.0.0")
28
+
29
+ def _info(self):
30
+ return datasets.DatasetInfo(
31
+ description=_DESCRIPTION,
32
+ features=datasets.Features(
33
+ {
34
+ "id": datasets.Value("string"),
35
+ "tokens": datasets.Sequence(datasets.Value("string")),
36
+ "ner_tags": datasets.Sequence(
37
+ datasets.features.ClassLabel(
38
+ names=[
39
+ "O",
40
+ "B-CARDINAL",
41
+ "B-DATE",
42
+ "B-EVENT",
43
+ "B-FAC",
44
+ "B-GPE",
45
+ "B-LANGUAGE",
46
+ "B-LAW",
47
+ "B-LOC",
48
+ "B-MONEY",
49
+ "B-NORP",
50
+ "B-ORDINAL",
51
+ "B-ORG",
52
+ "B-PERCENT",
53
+ "B-PERSON",
54
+ "B-PRODUCT",
55
+ "B-QUANTITY",
56
+ "B-TIME",
57
+ "B-WORK_OF_ART",
58
+ "I-CARDINAL",
59
+ "I-DATE",
60
+ "I-EVENT",
61
+ "I-FAC",
62
+ "I-GPE",
63
+ "I-LANGUAGE",
64
+ "I-LAW",
65
+ "I-LOC",
66
+ "I-MONEY",
67
+ "I-NORP",
68
+ "I-ORDINAL",
69
+ "I-ORG",
70
+ "I-PERCENT",
71
+ "I-PERSON",
72
+ "I-PRODUCT",
73
+ "I-QUANTITY",
74
+ "I-TIME",
75
+ "I-WORK_OF_ART",
76
+ "E-CARDINAL",
77
+ "E-DATE",
78
+ "E-EVENT",
79
+ "E-FAC",
80
+ "E-GPE",
81
+ "E-LANGUAGE",
82
+ "E-LAW",
83
+ "E-LOC",
84
+ "E-MONEY",
85
+ "E-NORP",
86
+ "E-ORDINAL",
87
+ "E-ORG",
88
+ "E-PERCENT",
89
+ "E-PERSON",
90
+ "E-PRODUCT",
91
+ "E-QUANTITY",
92
+ "E-TIME",
93
+ "E-WORK_OF_ART",
94
+ "S-CARDINAL",
95
+ "S-DATE",
96
+ "S-EVENT",
97
+ "S-FAC",
98
+ "S-GPE",
99
+ "S-LANGUAGE",
100
+ "S-LAW",
101
+ "S-LOC",
102
+ "S-MONEY",
103
+ "S-NORP",
104
+ "S-ORDINAL",
105
+ "S-ORG",
106
+ "S-PERCENT",
107
+ "S-PERSON",
108
+ "S-PRODUCT",
109
+ "S-QUANTITY",
110
+ "S-TIME",
111
+ "S-WORK_OF_ART"
112
+ ]
113
+ )
114
+ ),
115
+ },
116
+ ),
117
+ supervised_keys=None,
118
+ homepage=_HOMEPAGE_URL,
119
+ citation=_CITATION,
120
+ )
121
+
122
+ def _split_generators(self, dl_manager):
123
+ train_path = dl_manager.download_and_extract(_TRAIN_URL)
124
+ return [
125
+ datasets.SplitGenerator(
126
+ name=datasets.Split.TRAIN,
127
+ gen_kwargs={"data_path": train_path},
128
+ )
129
+ ]
130
+
131
+ def _generate_examples(self, data_path):
132
+ sentence_counter = 0
133
+ with open(data_path, encoding="utf-8") as f:
134
+ current_words = []
135
+ current_labels = []
136
+ for row in f:
137
+ row = row.rstrip()
138
+ row_split = row.split("\t")
139
+ if len(row_split) == 2:
140
+ token, label = row_split
141
+ current_words.append(token)
142
+ current_labels.append(label)
143
+ else:
144
+ if not current_words:
145
+ continue
146
+ assert len(current_words) == len(current_labels), "word len doesnt match label length"
147
+ sentence = (
148
+ sentence_counter,
149
+ {
150
+ "id": str(sentence_counter),
151
+ "tokens": current_words,
152
+ "ner_tags": current_labels,
153
+ },
154
+ )
155
+ sentence_counter += 1
156
+ current_words = []
157
+ current_labels = []
158
+ yield sentence
159
+
160
+ # if something remains:
161
+ if current_words:
162
+ sentence = (
163
+ sentence_counter,
164
+ {
165
+ "id": str(sentence_counter),
166
+ "tokens": current_words,
167
+ "ner_tags": current_labels,
168
+ },
169
+ )
170
+ yield sentence