hanlp 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hanlp/__init__.py +57 -0
- hanlp/callbacks/__init__.py +3 -0
- hanlp/callbacks/fine_csv_logger.py +71 -0
- hanlp/common/__init__.py +3 -0
- hanlp/common/component.py +36 -0
- hanlp/common/dataset.py +840 -0
- hanlp/common/keras_component.py +519 -0
- hanlp/common/structure.py +69 -0
- hanlp/common/torch_component.py +642 -0
- hanlp/common/transform.py +551 -0
- hanlp/common/transform_tf.py +310 -0
- hanlp/common/vocab.py +506 -0
- hanlp/common/vocab_tf.py +270 -0
- hanlp/components/__init__.py +4 -0
- hanlp/components/amr/__init__.py +3 -0
- hanlp/components/amr/amrbart/__init__.py +3 -0
- hanlp/components/amr/amrbart/bart_amr_generation.py +139 -0
- hanlp/components/amr/amrbart/bart_amr_parser.py +208 -0
- hanlp/components/amr/amrbart/common/__init__.py +3 -0
- hanlp/components/amr/amrbart/common/constant.py +544 -0
- hanlp/components/amr/amrbart/common/penman_interface.py +80 -0
- hanlp/components/amr/amrbart/common/postprocessing.py +514 -0
- hanlp/components/amr/amrbart/data_interface/__init__.py +3 -0
- hanlp/components/amr/amrbart/data_interface/dataset.py +74 -0
- hanlp/components/amr/amrbart/model_interface/__init__.py +3 -0
- hanlp/components/amr/amrbart/model_interface/modeling_bart.py +1894 -0
- hanlp/components/amr/amrbart/model_interface/tokenization_bart.py +475 -0
- hanlp/components/amr/amrbart/preprocess/__init__.py +3 -0
- hanlp/components/amr/amrbart/preprocess/amr_io.py +64 -0
- hanlp/components/amr/amrbart/preprocess/penman_interface.py +80 -0
- hanlp/components/amr/amrbart/preprocess/read_and_process.py +132 -0
- hanlp/components/amr/seq2seq/__init__.py +3 -0
- hanlp/components/amr/seq2seq/dataset/IO.py +35 -0
- hanlp/components/amr/seq2seq/dataset/__init__.py +3 -0
- hanlp/components/amr/seq2seq/dataset/dataset.py +235 -0
- hanlp/components/amr/seq2seq/dataset/linearization.py +408 -0
- hanlp/components/amr/seq2seq/dataset/penman.py +85 -0
- hanlp/components/amr/seq2seq/dataset/postprocessing.py +636 -0
- hanlp/components/amr/seq2seq/dataset/tokenization_bart.py +671 -0
- hanlp/components/amr/seq2seq/dataset/tokenization_t5.py +673 -0
- hanlp/components/amr/seq2seq/evaluation.py +24 -0
- hanlp/components/amr/seq2seq/optim.py +100 -0
- hanlp/components/amr/seq2seq/seq2seq_amr_parser.py +510 -0
- hanlp/components/classifiers/__init__.py +3 -0
- hanlp/components/classifiers/fasttext_classifier.py +95 -0
- hanlp/components/classifiers/transformer_classifier.py +383 -0
- hanlp/components/classifiers/transformer_classifier_hf.py +125 -0
- hanlp/components/classifiers/transformer_classifier_tf.py +193 -0
- hanlp/components/classifiers/transformer_regression_hf.py +99 -0
- hanlp/components/distillation/__init__.py +3 -0
- hanlp/components/distillation/distillable_component.py +54 -0
- hanlp/components/distillation/losses.py +285 -0
- hanlp/components/distillation/schedulers.py +124 -0
- hanlp/components/eos/__init__.py +3 -0
- hanlp/components/eos/ngram.py +311 -0
- hanlp/components/lambda_wrapper.py +29 -0
- hanlp/components/lemmatizer.py +45 -0
- hanlp/components/lm/__init__.py +3 -0
- hanlp/components/lm/mlm.py +112 -0
- hanlp/components/mtl/__init__.py +3 -0
- hanlp/components/mtl/multi_task_learning.py +815 -0
- hanlp/components/mtl/tasks/__init__.py +288 -0
- hanlp/components/mtl/tasks/amr.py +174 -0
- hanlp/components/mtl/tasks/constituency.py +169 -0
- hanlp/components/mtl/tasks/dep.py +166 -0
- hanlp/components/mtl/tasks/dep_2nd.py +111 -0
- hanlp/components/mtl/tasks/lem.py +129 -0
- hanlp/components/mtl/tasks/ner/__init__.py +3 -0
- hanlp/components/mtl/tasks/ner/biaffine_ner.py +107 -0
- hanlp/components/mtl/tasks/ner/tag_ner.py +168 -0
- hanlp/components/mtl/tasks/pos.py +165 -0
- hanlp/components/mtl/tasks/sdp.py +170 -0
- hanlp/components/mtl/tasks/srl/__init__.py +3 -0
- hanlp/components/mtl/tasks/srl/bio_srl.py +126 -0
- hanlp/components/mtl/tasks/srl/rank_srl.py +121 -0
- hanlp/components/mtl/tasks/tok/__init__.py +3 -0
- hanlp/components/mtl/tasks/tok/reg_tok.py +109 -0
- hanlp/components/mtl/tasks/tok/tag_tok.py +205 -0
- hanlp/components/mtl/tasks/ud.py +171 -0
- hanlp/components/ner/__init__.py +3 -0
- hanlp/components/ner/biaffine_ner/__init__.py +3 -0
- hanlp/components/ner/biaffine_ner/biaffine_ner.py +401 -0
- hanlp/components/ner/biaffine_ner/biaffine_ner_model.py +132 -0
- hanlp/components/ner/ner_tf.py +83 -0
- hanlp/components/ner/rnn_ner.py +69 -0
- hanlp/components/ner/transformer_ner.py +240 -0
- hanlp/components/parsers/__init__.py +3 -0
- hanlp/components/parsers/alg.py +761 -0
- hanlp/components/parsers/alg_tf.py +289 -0
- hanlp/components/parsers/biaffine/__init__.py +3 -0
- hanlp/components/parsers/biaffine/biaffine.py +98 -0
- hanlp/components/parsers/biaffine/biaffine_2nd_dep.py +213 -0
- hanlp/components/parsers/biaffine/biaffine_dep.py +575 -0
- hanlp/components/parsers/biaffine/biaffine_model.py +230 -0
- hanlp/components/parsers/biaffine/biaffine_sdp.py +203 -0
- hanlp/components/parsers/biaffine/mlp.py +83 -0
- hanlp/components/parsers/biaffine/structual_attention.py +234 -0
- hanlp/components/parsers/biaffine/variationalbilstm.py +231 -0
- hanlp/components/parsers/biaffine_parser_tf.py +567 -0
- hanlp/components/parsers/biaffine_tf/__init__.py +3 -0
- hanlp/components/parsers/biaffine_tf/alg.py +289 -0
- hanlp/components/parsers/biaffine_tf/layers.py +120 -0
- hanlp/components/parsers/biaffine_tf/model.py +150 -0
- hanlp/components/parsers/chu_liu_edmonds.py +314 -0
- hanlp/components/parsers/conll.py +73 -0
- hanlp/components/parsers/constituency/__init__.py +3 -0
- hanlp/components/parsers/constituency/crf_constituency_model.py +214 -0
- hanlp/components/parsers/constituency/crf_constituency_parser.py +322 -0
- hanlp/components/parsers/constituency/treecrf.py +360 -0
- hanlp/components/parsers/parse_alg.py +312 -0
- hanlp/components/parsers/ud/__init__.py +3 -0
- hanlp/components/parsers/ud/lemma_edit.py +135 -0
- hanlp/components/parsers/ud/tag_decoder.py +136 -0
- hanlp/components/parsers/ud/ud_model.py +139 -0
- hanlp/components/parsers/ud/ud_parser.py +346 -0
- hanlp/components/parsers/ud/udify_util.py +369 -0
- hanlp/components/parsers/ud/util.py +28 -0
- hanlp/components/pipeline.py +179 -0
- hanlp/components/rnn_language_model_tf.py +84 -0
- hanlp/components/srl/__init__.py +3 -0
- hanlp/components/srl/span_bio/__init__.py +3 -0
- hanlp/components/srl/span_bio/baffine_tagging.py +75 -0
- hanlp/components/srl/span_bio/span_bio.py +384 -0
- hanlp/components/srl/span_rank/__init__.py +3 -0
- hanlp/components/srl/span_rank/highway_variational_lstm.py +250 -0
- hanlp/components/srl/span_rank/inference_utils.py +243 -0
- hanlp/components/srl/span_rank/layer.py +388 -0
- hanlp/components/srl/span_rank/span_rank.py +417 -0
- hanlp/components/srl/span_rank/span_ranking_srl_model.py +508 -0
- hanlp/components/srl/span_rank/srl_eval_utils.py +287 -0
- hanlp/components/srl/span_rank/util.py +12 -0
- hanlp/components/sts/__init__.py +3 -0
- hanlp/components/sts/transformer_sts.py +242 -0
- hanlp/components/taggers/__init__.py +3 -0
- hanlp/components/taggers/cnn_tagger_tf.py +125 -0
- hanlp/components/taggers/ngram_conv/__init__.py +3 -0
- hanlp/components/taggers/ngram_conv/ngram_conv_tagger.py +177 -0
- hanlp/components/taggers/pos_tf.py +13 -0
- hanlp/components/taggers/rnn/__init__.py +3 -0
- hanlp/components/taggers/rnn/rnntaggingmodel.py +100 -0
- hanlp/components/taggers/rnn_tagger.py +195 -0
- hanlp/components/taggers/rnn_tagger_tf.py +89 -0
- hanlp/components/taggers/tagger.py +231 -0
- hanlp/components/taggers/tagger_tf.py +38 -0
- hanlp/components/taggers/transformers/__init__.py +3 -0
- hanlp/components/taggers/transformers/metrics_tf.py +15 -0
- hanlp/components/taggers/transformers/transformer_tagger.py +294 -0
- hanlp/components/taggers/transformers/transformer_tagger_tf.py +94 -0
- hanlp/components/taggers/transformers/transformer_transform_tf.py +132 -0
- hanlp/components/taggers/util.py +22 -0
- hanlp/components/tokenizers/__init__.py +3 -0
- hanlp/components/tokenizers/multi_criteria_cws_transformer.py +89 -0
- hanlp/components/tokenizers/tok.py +54 -0
- hanlp/components/tokenizers/tok_tf.py +110 -0
- hanlp/components/tokenizers/transformer.py +299 -0
- hanlp/datasets/__init__.py +3 -0
- hanlp/datasets/classification/__init__.py +3 -0
- hanlp/datasets/classification/sentiment.py +8 -0
- hanlp/datasets/coref/__init__.py +3 -0
- hanlp/datasets/coref/loaders/__init__.py +3 -0
- hanlp/datasets/coref/loaders/conll12coref.py +88 -0
- hanlp/datasets/eos/__init__.py +3 -0
- hanlp/datasets/eos/eos.py +101 -0
- hanlp/datasets/eos/loaders/__init__.py +3 -0
- hanlp/datasets/eos/loaders/nn_eos.py +17 -0
- hanlp/datasets/lm/__init__.py +12 -0
- hanlp/datasets/lm/loaders/__init__.py +3 -0
- hanlp/datasets/lm/loaders/lm_dataset.py +142 -0
- hanlp/datasets/lu/__init__.py +3 -0
- hanlp/datasets/lu/glue.py +25 -0
- hanlp/datasets/ner/__init__.py +3 -0
- hanlp/datasets/ner/conll03.py +11 -0
- hanlp/datasets/ner/loaders/__init__.py +3 -0
- hanlp/datasets/ner/loaders/json_ner.py +155 -0
- hanlp/datasets/ner/loaders/tsv.py +86 -0
- hanlp/datasets/ner/msra.py +34 -0
- hanlp/datasets/ner/resume.py +16 -0
- hanlp/datasets/ner/weibo.py +15 -0
- hanlp/datasets/parsing/__init__.py +3 -0
- hanlp/datasets/parsing/amr.py +390 -0
- hanlp/datasets/parsing/ctb5.py +17 -0
- hanlp/datasets/parsing/ctb7.py +13 -0
- hanlp/datasets/parsing/ctb8.py +44 -0
- hanlp/datasets/parsing/ctb9.py +55 -0
- hanlp/datasets/parsing/loaders/__init__.py +3 -0
- hanlp/datasets/parsing/loaders/_ctb_utils.py +320 -0
- hanlp/datasets/parsing/loaders/conll_dataset.py +106 -0
- hanlp/datasets/parsing/loaders/constituency_dataset.py +211 -0
- hanlp/datasets/parsing/pmt1.py +58 -0
- hanlp/datasets/parsing/ptb.py +48 -0
- hanlp/datasets/parsing/semeval15.py +61 -0
- hanlp/datasets/parsing/semeval16.py +64 -0
- hanlp/datasets/parsing/ud/__init__.py +30 -0
- hanlp/datasets/parsing/ud/ud210.py +1019 -0
- hanlp/datasets/parsing/ud/ud210m.py +15 -0
- hanlp/datasets/parsing/ud/ud23.py +341 -0
- hanlp/datasets/parsing/ud/ud23m.py +12 -0
- hanlp/datasets/parsing/ud/ud27.py +855 -0
- hanlp/datasets/parsing/ud/ud27m.py +15 -0
- hanlp/datasets/pos/__init__.py +3 -0
- hanlp/datasets/pos/ctb5.py +12 -0
- hanlp/datasets/qa/__init__.py +3 -0
- hanlp/datasets/qa/hotpotqa.py +194 -0
- hanlp/datasets/srl/__init__.py +5 -0
- hanlp/datasets/srl/loaders/__init__.py +3 -0
- hanlp/datasets/srl/loaders/conll2012.py +227 -0
- hanlp/datasets/srl/loaders/ontonotes_loader.py +615 -0
- hanlp/datasets/srl/ontonotes5/__init__.py +5 -0
- hanlp/datasets/srl/ontonotes5/_utils.py +677 -0
- hanlp/datasets/srl/ontonotes5/chinese.py +106 -0
- hanlp/datasets/srl/ontonotes5/english.py +91 -0
- hanlp/datasets/sts/__init__.py +3 -0
- hanlp/datasets/sts/stsb.py +36 -0
- hanlp/datasets/tokenization/__init__.py +3 -0
- hanlp/datasets/tokenization/ctb6.py +12 -0
- hanlp/datasets/tokenization/loaders/__init__.py +3 -0
- hanlp/datasets/tokenization/loaders/chunking_dataset.py +48 -0
- hanlp/datasets/tokenization/loaders/multi_criteria_cws/__init__.py +35 -0
- hanlp/datasets/tokenization/loaders/multi_criteria_cws/mcws_dataset.py +98 -0
- hanlp/datasets/tokenization/loaders/txt.py +129 -0
- hanlp/datasets/tokenization/sighan2005/__init__.py +22 -0
- hanlp/datasets/tokenization/sighan2005/as_.py +19 -0
- hanlp/datasets/tokenization/sighan2005/cityu.py +19 -0
- hanlp/datasets/tokenization/sighan2005/msr.py +19 -0
- hanlp/datasets/tokenization/sighan2005/pku.py +19 -0
- hanlp/layers/__init__.py +3 -0
- hanlp/layers/cnn_encoder.py +118 -0
- hanlp/layers/crf/__init__.py +3 -0
- hanlp/layers/crf/crf.py +353 -0
- hanlp/layers/crf/crf_layer_tf.py +165 -0
- hanlp/layers/crf/crf_tf.py +520 -0
- hanlp/layers/dropout.py +159 -0
- hanlp/layers/embeddings/__init__.py +3 -0
- hanlp/layers/embeddings/char_cnn.py +147 -0
- hanlp/layers/embeddings/char_cnn_tf.py +103 -0
- hanlp/layers/embeddings/char_rnn.py +109 -0
- hanlp/layers/embeddings/char_rnn_tf.py +61 -0
- hanlp/layers/embeddings/concat_embedding.py +60 -0
- hanlp/layers/embeddings/contextual_string_embedding.py +216 -0
- hanlp/layers/embeddings/contextual_string_embedding_tf.py +135 -0
- hanlp/layers/embeddings/contextual_word_embedding.py +187 -0
- hanlp/layers/embeddings/embedding.py +133 -0
- hanlp/layers/embeddings/fast_text.py +167 -0
- hanlp/layers/embeddings/fast_text_tf.py +104 -0
- hanlp/layers/embeddings/util.py +110 -0
- hanlp/layers/embeddings/util_tf.py +88 -0
- hanlp/layers/embeddings/word2vec.py +355 -0
- hanlp/layers/embeddings/word2vec_tf.py +197 -0
- hanlp/layers/feed_forward.py +15 -0
- hanlp/layers/feedforward.py +107 -0
- hanlp/layers/scalar_mix.py +156 -0
- hanlp/layers/time_distributed.py +72 -0
- hanlp/layers/transformers/__init__.py +13 -0
- hanlp/layers/transformers/encoder.py +129 -0
- hanlp/layers/transformers/loader_tf.py +24 -0
- hanlp/layers/transformers/pt_imports.py +69 -0
- hanlp/layers/transformers/relative_transformer.py +329 -0
- hanlp/layers/transformers/resource.py +38 -0
- hanlp/layers/transformers/tf_imports.py +6 -0
- hanlp/layers/transformers/utils.py +369 -0
- hanlp/layers/transformers/utils_tf.py +191 -0
- hanlp/layers/weight_normalization.py +224 -0
- hanlp/losses/__init__.py +3 -0
- hanlp/losses/sparse_categorical_crossentropy.py +63 -0
- hanlp/metrics/__init__.py +3 -0
- hanlp/metrics/accuracy.py +227 -0
- hanlp/metrics/amr/__init__.py +3 -0
- hanlp/metrics/amr/smatch_eval.py +101 -0
- hanlp/metrics/chunking/__init__.py +3 -0
- hanlp/metrics/chunking/binary_chunking_f1.py +33 -0
- hanlp/metrics/chunking/bmes_tf.py +47 -0
- hanlp/metrics/chunking/chunking_f1.py +138 -0
- hanlp/metrics/chunking/chunking_f1_tf.py +65 -0
- hanlp/metrics/chunking/conlleval.py +344 -0
- hanlp/metrics/chunking/iobes_tf.py +28 -0
- hanlp/metrics/chunking/sequence_labeling.py +404 -0
- hanlp/metrics/f1.py +64 -0
- hanlp/metrics/metric.py +44 -0
- hanlp/metrics/mtl.py +45 -0
- hanlp/metrics/parsing/__init__.py +3 -0
- hanlp/metrics/parsing/attachmentscore.py +75 -0
- hanlp/metrics/parsing/conllx_eval.py +67 -0
- hanlp/metrics/parsing/labeled_f1.py +103 -0
- hanlp/metrics/parsing/labeled_f1_tf.py +103 -0
- hanlp/metrics/parsing/labeled_score.py +59 -0
- hanlp/metrics/parsing/semdep_eval.py +137 -0
- hanlp/metrics/parsing/span.py +103 -0
- hanlp/metrics/spearman_correlation.py +86 -0
- hanlp/metrics/srl/__init__.py +3 -0
- hanlp/metrics/srl/srlconll.py +39 -0
- hanlp/optimizers/__init__.py +3 -0
- hanlp/optimizers/adamw/__init__.py +71 -0
- hanlp/optimizers/adamw/optimization.py +240 -0
- hanlp/pretrained/__init__.py +23 -0
- hanlp/pretrained/amr.py +72 -0
- hanlp/pretrained/amr2text.py +13 -0
- hanlp/pretrained/classifiers.py +18 -0
- hanlp/pretrained/constituency.py +19 -0
- hanlp/pretrained/dep.py +23 -0
- hanlp/pretrained/eos.py +10 -0
- hanlp/pretrained/fasttext.py +16 -0
- hanlp/pretrained/glove.py +19 -0
- hanlp/pretrained/mtl.py +52 -0
- hanlp/pretrained/ner.py +15 -0
- hanlp/pretrained/pos.py +25 -0
- hanlp/pretrained/rnnlm.py +13 -0
- hanlp/pretrained/sdp.py +21 -0
- hanlp/pretrained/srl.py +9 -0
- hanlp/pretrained/sts.py +10 -0
- hanlp/pretrained/tok.py +51 -0
- hanlp/pretrained/word2vec.py +38 -0
- hanlp/transform/__init__.py +3 -0
- hanlp/transform/conll_tf.py +814 -0
- hanlp/transform/glue_tf.py +44 -0
- hanlp/transform/prepend_space.py +28 -0
- hanlp/transform/table_tf.py +89 -0
- hanlp/transform/tacred_tf.py +106 -0
- hanlp/transform/text_tf.py +101 -0
- hanlp/transform/transformer_tokenizer.py +718 -0
- hanlp/transform/tsv_tf.py +151 -0
- hanlp/transform/txt_tf.py +201 -0
- hanlp/utils/__init__.py +19 -0
- hanlp/utils/component_util.py +197 -0
- hanlp/utils/file_read_backwards/__init__.py +7 -0
- hanlp/utils/file_read_backwards/buffer_work_space.py +202 -0
- hanlp/utils/file_read_backwards/file_read_backwards.py +146 -0
- hanlp/utils/init_util.py +16 -0
- hanlp/utils/io_util.py +719 -0
- hanlp/utils/lang/__init__.py +7 -0
- hanlp/utils/lang/en/__init__.py +3 -0
- hanlp/utils/lang/en/english_tokenizer.py +325 -0
- hanlp/utils/lang/ja/__init__.py +3 -0
- hanlp/utils/lang/ja/bert_tok.py +81 -0
- hanlp/utils/lang/zh/__init__.py +3 -0
- hanlp/utils/lang/zh/char_table.py +53 -0
- hanlp/utils/lang/zh/localization.py +35 -0
- hanlp/utils/log_util.py +182 -0
- hanlp/utils/rules.py +40 -0
- hanlp/utils/span_util.py +729 -0
- hanlp/utils/string_util.py +117 -0
- hanlp/utils/tf_util.py +222 -0
- hanlp/utils/time_util.py +246 -0
- hanlp/utils/torch_util.py +295 -0
- hanlp/version.py +10 -0
- hanlp-2.1.0.dist-info/LICENSE +201 -0
- hanlp-2.1.0.dist-info/METADATA +248 -0
- hanlp-2.1.0.dist-info/RECORD +349 -0
- hanlp-2.1.0.dist-info/WHEEL +5 -0
- hanlp-2.1.0.dist-info/top_level.txt +1 -0
hanlp/__init__.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# -*- coding:utf-8 -*-
|
|
2
|
+
# Author: hankcs
|
|
3
|
+
# Date: 2019-06-13 18:05
|
|
4
|
+
import hanlp.common
|
|
5
|
+
import hanlp.components
|
|
6
|
+
import hanlp.pretrained
|
|
7
|
+
import hanlp.utils
|
|
8
|
+
from hanlp.version import __version__
|
|
9
|
+
|
|
10
|
+
hanlp.utils.ls_resource_in_module(hanlp.pretrained)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def load(save_dir: str, verbose=None, **kwargs) -> hanlp.common.component.Component:
|
|
14
|
+
"""Load a pretrained component from an identifier.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
save_dir (str): The identifier to the saved component. It could be a remote URL or a local path.
|
|
18
|
+
verbose: ``True`` to print loading progress.
|
|
19
|
+
**kwargs: Arguments passed to :func:`hanlp.common.torch_component.TorchComponent.load`, e.g.,
|
|
20
|
+
``devices`` is a useful argument to specify which GPU devices a PyTorch component will use.
|
|
21
|
+
|
|
22
|
+
Examples::
|
|
23
|
+
|
|
24
|
+
import hanlp
|
|
25
|
+
# Load component onto the 0-th GPU.
|
|
26
|
+
hanlp.load(..., devices=0)
|
|
27
|
+
# Load component onto the 0-th and 1-st GPUs using data parallelization.
|
|
28
|
+
hanlp.load(..., devices=[0, 1])
|
|
29
|
+
|
|
30
|
+
.. Note::
|
|
31
|
+
A component can have dependencies on other components or resources, which will be recursively loaded. So it's
|
|
32
|
+
common to see multiple downloading messages per single load.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
hanlp.common.component.Component: A pretrained component.
|
|
36
|
+
|
|
37
|
+
"""
|
|
38
|
+
save_dir = hanlp.pretrained.ALL.get(save_dir, save_dir)
|
|
39
|
+
from hanlp.utils.component_util import load_from_meta_file
|
|
40
|
+
if verbose is None:
|
|
41
|
+
from hanlp_common.constant import HANLP_VERBOSE
|
|
42
|
+
verbose = HANLP_VERBOSE
|
|
43
|
+
return load_from_meta_file(save_dir, 'meta.json', verbose=verbose, **kwargs)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def pipeline(*pipes) -> hanlp.components.pipeline.Pipeline:
|
|
47
|
+
"""Creates a pipeline of components. It's made for bundling `KerasComponents`. For `TorchComponent`, use
|
|
48
|
+
:class:`~hanlp.components.mtl.multi_task_learning.MultiTaskLearning` instead.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
*pipes: Components if pre-defined any.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
hanlp.components.pipeline.Pipeline: A pipeline, which is a list of components in order.
|
|
55
|
+
|
|
56
|
+
"""
|
|
57
|
+
return hanlp.components.pipeline.Pipeline(*pipes)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# -*- coding:utf-8 -*-
|
|
2
|
+
# Author: hankcs
|
|
3
|
+
# Date: 2019-12-05 02:12
|
|
4
|
+
import copy
|
|
5
|
+
from io import TextIOWrapper
|
|
6
|
+
from typing import List
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import tensorflow as tf
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class StreamTableFormatter(object):
|
|
13
|
+
|
|
14
|
+
def __init__(self) -> None:
|
|
15
|
+
super().__init__()
|
|
16
|
+
self.col_widths = None
|
|
17
|
+
|
|
18
|
+
def format_row(self, cells) -> List[str]:
|
|
19
|
+
if not isinstance(cells, list):
|
|
20
|
+
cells = list(cells)
|
|
21
|
+
if not self.col_widths:
|
|
22
|
+
self.col_widths = [0] * len([_ for _ in cells])
|
|
23
|
+
for i, c in enumerate(cells):
|
|
24
|
+
self.col_widths[i] = max(self.col_widths[i], len(self.format_cell(c, self.col_widths[i])))
|
|
25
|
+
return list(self.format_cell(cell, width) for cell, width in zip(cells, self.col_widths))
|
|
26
|
+
|
|
27
|
+
def format_cell(self, cell: str, min_width) -> str:
|
|
28
|
+
if isinstance(cell, (np.float32, np.float)):
|
|
29
|
+
return '{:>{}.4f}'.format(cell, min_width)
|
|
30
|
+
return '{:>{}}'.format(cell, min_width)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class FineCSVLogger(tf.keras.callbacks.History):
|
|
34
|
+
|
|
35
|
+
def __init__(self, filename, separator=',', append=False):
|
|
36
|
+
super().__init__()
|
|
37
|
+
self.append = append
|
|
38
|
+
self.separator = separator
|
|
39
|
+
self.filename = filename
|
|
40
|
+
self.out: TextIOWrapper = None
|
|
41
|
+
self.keys = []
|
|
42
|
+
self.formatter = StreamTableFormatter()
|
|
43
|
+
|
|
44
|
+
def on_train_begin(self, logs=None):
|
|
45
|
+
super().on_train_begin(logs)
|
|
46
|
+
self.out = open(self.filename, 'a' if self.append else 'w')
|
|
47
|
+
|
|
48
|
+
def on_train_end(self, logs=None):
|
|
49
|
+
self.out.close()
|
|
50
|
+
|
|
51
|
+
def on_epoch_end(self, epoch, logs=None):
|
|
52
|
+
super().on_epoch_end(epoch, logs)
|
|
53
|
+
if not self.keys:
|
|
54
|
+
self.keys = sorted(logs.keys())
|
|
55
|
+
|
|
56
|
+
if getattr(self.model, 'stop_training', None):
|
|
57
|
+
# We set NA so that csv parsers do not fail for this last epoch.
|
|
58
|
+
logs = dict([(k, logs[k]) if k in logs else (k, 'NA') for k in self.keys])
|
|
59
|
+
|
|
60
|
+
# feed them twice to decide the actual width
|
|
61
|
+
values = self.formatter.format_row([epoch + 1] + [logs.get(k, 'NA') for k in self.keys])
|
|
62
|
+
headers = self.formatter.format_row(['epoch'] + self.keys)
|
|
63
|
+
# print headers and bars
|
|
64
|
+
self.out.write(self.separator.join(headers) + '\n')
|
|
65
|
+
# bars for markdown style
|
|
66
|
+
bars = [''.join(['-'] * width) for width in self.formatter.col_widths]
|
|
67
|
+
self.out.write(self.separator.join(bars) + '\n')
|
|
68
|
+
|
|
69
|
+
values = self.formatter.format_row([epoch + 1] + [logs.get(k, 'NA') for k in self.keys])
|
|
70
|
+
self.out.write(self.separator.join(values) + '\n')
|
|
71
|
+
self.out.flush()
|
hanlp/common/__init__.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# -*- coding:utf-8 -*-
|
|
2
|
+
# Author: hankcs
|
|
3
|
+
# Date: 2019-08-26 14:45
|
|
4
|
+
import inspect
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from hanlp_common.configurable import Configurable
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Component(Configurable, ABC):
|
|
12
|
+
@abstractmethod
|
|
13
|
+
def predict(self, *args, **kwargs):
|
|
14
|
+
"""Predict on data. This is the base class for all components, including rule based and statistical ones.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
*args: Any type of data subject to sub-classes
|
|
18
|
+
**kwargs: Additional arguments
|
|
19
|
+
|
|
20
|
+
Returns: Any predicted annotations.
|
|
21
|
+
|
|
22
|
+
"""
|
|
23
|
+
raise NotImplementedError('%s.%s()' % (self.__class__.__name__, inspect.stack()[0][3]))
|
|
24
|
+
|
|
25
|
+
def __call__(self, *args, **kwargs):
|
|
26
|
+
"""
|
|
27
|
+
A shortcut for :func:`~hanlp.common.component.predict`.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
*args: Any type of data subject to sub-classes
|
|
31
|
+
**kwargs: Additional arguments
|
|
32
|
+
|
|
33
|
+
Returns: Any predicted annotations.
|
|
34
|
+
|
|
35
|
+
"""
|
|
36
|
+
return self.predict(*args, **kwargs)
|