File size: 3,174 Bytes
b6f487c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
MNBVC: Massive Never-ending BT Vast Chinese corpus
"""

import json
import datasets
import numpy as np
import traceback
from .meta import MNBVC_META
from .features import Features


_CITATION = """\
"""

_DESCRIPTION = """\
MNBVC-core: core split of Massive Never-ending BT Vast Chinese corpus
"""

_HOMEPAGE = "https://github.com/esbatmop/MNBVC"

_LICENSE = "MIT"


class MNBVC(datasets.GeneratorBasedBuilder):

    # multi configurations 配置
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name=key, version=datasets.Version("0.0.1"), description=value['description']) for key, value in MNBVC_META.items()]

    def _info(self):
        """
        dataset 的关键属性信息
        """
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=Features[MNBVC_META[self.config.name]['feature_type']],  
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """
        下载数据,组织split
        """
        data_dir = dl_manager.download_and_extract(MNBVC_META[self.config.name]['files'])

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_files": data_dir,
                },
            ),
        ]

    # 生成器:id 和数据元素的元祖
    def _generate_examples(self, data_files):
        id = 0
        features = self._info().features
        feature_keys = set(features.keys())

        def _drop_unused_keys(data):
            rm_keys = []
            for key in data.keys():
                if key not in feature_keys:
                    rm_keys.append(key)
            for key in rm_keys:
                del data[key]
            return data

        try:
            for file_i, data_file in enumerate(data_files):
                with open(data_file, encoding="utf-8") as f:
                    for line_i, line in enumerate(f):
                        id += 1
                        data = json.loads(line)
                        if self.config.name == 'law_judgement':
                            text = data['详情']
                            del data['详情']
                            yield id, {
                                "text": text,
                                "meta": json.dumps(data, ensure_ascii=False),
                            }
                        else:
                            data = _drop_unused_keys(data)
                            if 'simhash' in data:  # for issue https://github.com/huggingface/datasets/issues/6007
                                data['simhash'] = str(data['simhash'])
                            
                            yield id, data
        except Exception as e:
            error_msg = 'oops, we find an error when loading the dataset\n'
            error_msg += f'Dataset: {self.config.name}\n'
            error_msg += f'Data File: {file_i} {data_file}\n'
            error_msg += f'Row: {line_i}'
            print(error_msg)
            traceback.print_exc()

            raise e