yangwang825 commited on
Commit
e4832ff
·
1 Parent(s): ce7492b

Create ynat.py

Browse files
Files changed (1) hide show
  1. ynat.py +47 -0
ynat.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import datasets
4
+ import typing as tp
5
+
6
+ _LABELS = [
7
+ 'IT과학',
8
+ '경제',
9
+ '사회',
10
+ '생활문화',
11
+ '세계',
12
+ '스포츠',
13
+ '정치'
14
+ ]
15
+
16
+
17
+ class YNAT(datasets.GeneratorBasedBuilder):
18
+
19
+ def _info(self):
20
+ return datasets.DatasetInfo(
21
+ description=None,
22
+ features=datasets.Features({
23
+ "text": datasets.Value("string"),
24
+ "label": datasets.features.ClassLabel(
25
+ names=_LABELS
26
+ )
27
+ }),
28
+ homepage=None,
29
+ citation=None
30
+ )
31
+
32
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> tp.List[datasets.SplitGenerator]:
33
+ path = os.getcwd()
34
+ train_filepath = os.path.join(path, 'train.json')
35
+ dev_filepath = os.path.join(path, 'dev.json')
36
+ return [
37
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'filepath': train_filepath}),
38
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={'filepath': dev_filepath}),
39
+ ]
40
+
41
+ def _generate_examples(self, filepath):
42
+ with open(filepath, 'r', encoding='utf-8') as fp:
43
+ lines = json.load(fp)
44
+ for idx, line in enumerate(lines):
45
+ text = str(line['text'])
46
+ label = int(_LABELS.index(line['label']))
47
+ yield idx, {'text': text, 'label': label}