msra_ner.py 2.8 KB
Newer Older
S
Steffy-zxf 已提交
1
#coding:utf-8
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2019  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.

16 17 18 19
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

20
import os
21
import codecs
22 23
import csv

K
kinghuin 已提交
24
from paddlehub.dataset import InputExample
W
wuzewu 已提交
25
from paddlehub.common.dir import DATA_HOME
K
kinghuin 已提交
26
from paddlehub.dataset.base_nlp_dataset import BaseNLPDataset
W
wuzewu 已提交
27

W
wuzewu 已提交
28
_DATA_URL = "https://bj.bcebos.com/paddlehub-dataset/msra_ner.tar.gz"
29 30


K
kinghuin 已提交
31
class MSRA_NER(BaseNLPDataset):
32 33 34 35 36 37 38
    """
    A set of manually annotated Chinese word-segmentation data and
    specifications for training and testing a Chinese word-segmentation system
    for research purposes.  For more information please refer to
    https://www.microsoft.com/en-us/download/details.aspx?id=52531
    """

39
    def __init__(self):
K
kinghuin 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
        dataset_dir = os.path.join(DATA_HOME, "msra_ner")
        base_path = self._download_dataset(dataset_dir, url=_DATA_URL)
        super(MSRA_NER, self).__init__(
            base_path=base_path,
            train_file="train.tsv",
            dev_file="dev.tsv",
            test_file="test.tsv",
            label_file=None,
            label_list=[
                "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "O"
            ],
        )

    def _read_file(self, input_file, phase=None):
54
        """Reads a tab separated value file."""
55
        with codecs.open(input_file, "r", encoding="UTF-8") as f:
K
kinghuin 已提交
56
            reader = csv.reader(f, delimiter="\t", quotechar=None)
57
            examples = []
58 59
            seq_id = 0
            header = next(reader)  # skip header
60
            for line in reader:
61 62 63
                example = InputExample(
                    guid=seq_id, label=line[1], text_a=line[0])
                seq_id += 1
64 65 66 67 68 69 70
                examples.append(example)

            return examples


if __name__ == "__main__":
    ds = MSRA_NER()
K
kinghuin 已提交
71 72 73 74 75 76 77 78
    print("first 10 dev")
    for e in ds.get_dev_examples()[:10]:
        print("{}\t{}\t{}\t{}".format(e.guid, e.text_a, e.text_b, e.label))
    print("first 10 train")
    for e in ds.get_train_examples()[:10]:
        print("{}\t{}\t{}\t{}".format(e.guid, e.text_a, e.text_b, e.label))
    print("first 10 test")
    for e in ds.get_test_examples()[:10]:
79
        print("{}\t{}\t{}\t{}".format(e.guid, e.text_a, e.text_b, e.label))
K
kinghuin 已提交
80
    print(ds)