msra_ner.py 2.8 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 30 31
# 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):
    URL = "https://bj.bcebos.com/paddlehub-dataset/msra_ner.tar.gz"
    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
        'dev': META_INFO(
Z
Zeyu Chen 已提交
41 42 43 44
            os.path.join('msra_ner', 'dev.tsv'),
            'ec772f3ba914bca5269f6e785bb3375d',
            (0, 1),
            1, ),
S
smallv0221 已提交
45
        'test': META_INFO(
Z
Zeyu Chen 已提交
46 47 48 49 50 51
            os.path.join('msra_ner', 'test.tsv'),
            '2f27ae68b5f61d6553ffa28bb577c8a7',
            (0, 1),
            1, ),
    }

S
smallv0221 已提交
52
    def __init__(self, mode='train', root=None, **kwargs):
Z
Zeyu Chen 已提交
53
        default_root = os.path.join(DATA_HOME, 'msra')
S
smallv0221 已提交
54 55
        filename, data_hash, field_indices, num_discard_samples = self.SPLITS[
            mode]
Z
Zeyu Chen 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        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"]