Create prepration.py
Browse files- prepration.py +20 -0
prepration.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from itertools import groupby
|
2 |
+
from torch_snippets import *
|
3 |
+
from datasets import load_dataset
|
4 |
+
|
5 |
+
|
6 |
+
ds = load_dataset('darentang/sroie')
|
7 |
+
classes = ds['train'].features['ner_tags'].feature.names
|
8 |
+
|
9 |
+
def make_classes(X):
|
10 |
+
x = AD(X)
|
11 |
+
words_with_labels = wwl = [(w, classes[l]) for w,l in zip(x.words, x.ner_tags)]
|
12 |
+
wwl = [i for i in wwl if i[1] != 'O']
|
13 |
+
wwl = [(w,l.split('-')[1]) for w,l in wwl]
|
14 |
+
wwl = {cls: ' '.join(next(zip(*group))) for cls,group in groupby(wwl, key=lambda x: x[1])}
|
15 |
+
X['fields'] = wwl
|
16 |
+
return X
|
17 |
+
|
18 |
+
|
19 |
+
ds = ds.map(make_classes)
|
20 |
+
ds.push_to_hub('sizhkhy/SROIE', commit_message="add fields as a new key")
|