msra_ner.py 2.7 KB
Newer Older
Z
Zeyu Chen 已提交
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
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

import collections
import os
import warnings

from paddle.io import Dataset
from paddle.dataset.common import md5file
from paddle.utils.download import get_path_from_url
from paddlenlp.utils.env import DATA_HOME

from .dataset import TSVDataset

__all__ = ['MSRA_NER']


class MSRA_NER(TSVDataset):
K
kinghuin 已提交
30
    URL = "https://paddlenlp.bj.bcebos.com/datasets/msra_ner.tar.gz"
Z
Zeyu Chen 已提交
31
    MD5 = None
S
smallv0221 已提交
32 33 34 35
    META_INFO = collections.namedtuple(
        'META_INFO', ('file', 'md5', 'field_indices', 'num_discard_samples'))
    SPLITS = {
        'train': META_INFO(
Z
Zeyu Chen 已提交
36 37 38 39
            os.path.join('msra_ner', 'train.tsv'),
            '67d3c93a37daba60ef43c03271f119d7',
            (0, 1),
            1, ),
S
smallv0221 已提交
40
        'test': META_INFO(
Z
Zeyu Chen 已提交
41 42 43 44 45 46
            os.path.join('msra_ner', 'test.tsv'),
            '2f27ae68b5f61d6553ffa28bb577c8a7',
            (0, 1),
            1, ),
    }

S
smallv0221 已提交
47
    def __init__(self, mode='train', root=None, **kwargs):
Z
Zeyu Chen 已提交
48
        default_root = os.path.join(DATA_HOME, 'msra')
S
smallv0221 已提交
49 50
        filename, data_hash, field_indices, num_discard_samples = self.SPLITS[
            mode]
Z
Zeyu Chen 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        fullname = os.path.join(default_root,
                                filename) if root is None else os.path.join(
                                    os.path.expanduser(root), filename)
        if not os.path.exists(fullname) or (data_hash and
                                            not md5file(fullname) == data_hash):
            if root is not None:  # not specified, and no need to warn
                warnings.warn(
                    'md5 check failed for {}, download {} data to {}'.format(
                        filename, self.__class__.__name__, default_root))
            path = get_path_from_url(self.URL, default_root, self.MD5)
            fullname = os.path.join(default_root, filename)
        super(MSRA_NER, self).__init__(
            fullname,
            field_indices=field_indices,
            num_discard_samples=num_discard_samples,
            **kwargs)

    def get_labels(self):
        """
        Return labels of the GlueCoLA object.
        """
        return ["B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "O"]