How to use the hanlp.common.structure.SerializableDict function in hanlp

To help you get started, we’ve selected a few hanlp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hankcs / HanLP / hanlp / common / transform.py View on Github external
def __init__(self, config: SerializableDict = None, map_x=True, map_y=True, **kwargs) -> None:
        super().__init__()
        self.map_y = map_y
        self.map_x = map_x
        if kwargs:
            if not config:
                config = SerializableDict()
            for k, v in kwargs.items():
                config[k] = v
        self.config = config
        self.output_types = None
        self.output_shapes = None
        self.padding_values = None
github hankcs / HanLP / hanlp / common / component.py View on Github external
def load_vocabs(self, save_dir, filename='vocabs.json'):
        save_dir = get_resource(save_dir)
        vocabs = SerializableDict()
        vocabs.load_json(os.path.join(save_dir, filename))
        for key, value in vocabs.items():
            vocab = Vocab()
            vocab.copy_from(value)
            setattr(self.transform, key, vocab)
github hankcs / HanLP / hanlp / common / component.py View on Github external
def save_vocabs(self, save_dir, filename='vocabs.json'):
        vocabs = SerializableDict()
        for key, value in vars(self.transform).items():
            if isinstance(value, Vocab):
                vocabs[key] = value.to_dict()
        vocabs.save_json(os.path.join(save_dir, filename))
github hankcs / HanLP / hanlp / common / document.py View on Github external
KEY_NER = 'ner'

    def __init__(self, **kwargs) -> None:
        super().__init__()
        self.update(kwargs)

    @property
    def words(self) -> List[str]:
        return self.get(Sentence.KEY_WORDS)

    @words.setter
    def words(self, words: List[str]):
        self[Sentence.KEY_WORDS] = words


class Document(SerializableDict):
    def __init__(self) -> None:
        super().__init__()
        # self.sentences = []
        # self.tokens = []
        # self.part_of_speech_tags = []
        # self.named_entities = []
        # self.syntactic_dependencies = []
        # self.semantic_dependencies = []

    def __missing__(self, key):
        value = []
        self[key] = value
        return value

    def to_dict(self) -> dict:
        return dict((k, v) for k, v in self.items() if v)
github hankcs / HanLP / hanlp / common / component.py View on Github external
def __init__(self, transform: Transform) -> None:
        super().__init__()
        self.model: Optional[tf.keras.Model] = None
        self.config = SerializableDict()
        self.transform = transform
        # share config with transform for convenience, so we don't need to pass args around
        if self.transform.config:
            for k, v in self.transform.config.items():
                self.config[k] = v
        self.transform.config = self.config
github hankcs / HanLP / hanlp / common / document.py View on Github external
# -*- coding:utf-8 -*-
# Author: hankcs
# Date: 2019-12-31 04:16
import json
from typing import List

from hanlp.common.structure import SerializableDict
from hanlp.components.parsers.conll import CoNLLSentence
from hanlp.utils.util import collapse_json


class Sentence(SerializableDict):
    KEY_WORDS = 'words'
    KEY_POS = 'pos'
    KEY_NER = 'ner'

    def __init__(self, **kwargs) -> None:
        super().__init__()
        self.update(kwargs)

    @property
    def words(self) -> List[str]:
        return self.get(Sentence.KEY_WORDS)

    @words.setter
    def words(self, words: List[str]):
        self[Sentence.KEY_WORDS] = words