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.
Files changed (349) hide show
  1. hanlp/__init__.py +57 -0
  2. hanlp/callbacks/__init__.py +3 -0
  3. hanlp/callbacks/fine_csv_logger.py +71 -0
  4. hanlp/common/__init__.py +3 -0
  5. hanlp/common/component.py +36 -0
  6. hanlp/common/dataset.py +840 -0
  7. hanlp/common/keras_component.py +519 -0
  8. hanlp/common/structure.py +69 -0
  9. hanlp/common/torch_component.py +642 -0
  10. hanlp/common/transform.py +551 -0
  11. hanlp/common/transform_tf.py +310 -0
  12. hanlp/common/vocab.py +506 -0
  13. hanlp/common/vocab_tf.py +270 -0
  14. hanlp/components/__init__.py +4 -0
  15. hanlp/components/amr/__init__.py +3 -0
  16. hanlp/components/amr/amrbart/__init__.py +3 -0
  17. hanlp/components/amr/amrbart/bart_amr_generation.py +139 -0
  18. hanlp/components/amr/amrbart/bart_amr_parser.py +208 -0
  19. hanlp/components/amr/amrbart/common/__init__.py +3 -0
  20. hanlp/components/amr/amrbart/common/constant.py +544 -0
  21. hanlp/components/amr/amrbart/common/penman_interface.py +80 -0
  22. hanlp/components/amr/amrbart/common/postprocessing.py +514 -0
  23. hanlp/components/amr/amrbart/data_interface/__init__.py +3 -0
  24. hanlp/components/amr/amrbart/data_interface/dataset.py +74 -0
  25. hanlp/components/amr/amrbart/model_interface/__init__.py +3 -0
  26. hanlp/components/amr/amrbart/model_interface/modeling_bart.py +1894 -0
  27. hanlp/components/amr/amrbart/model_interface/tokenization_bart.py +475 -0
  28. hanlp/components/amr/amrbart/preprocess/__init__.py +3 -0
  29. hanlp/components/amr/amrbart/preprocess/amr_io.py +64 -0
  30. hanlp/components/amr/amrbart/preprocess/penman_interface.py +80 -0
  31. hanlp/components/amr/amrbart/preprocess/read_and_process.py +132 -0
  32. hanlp/components/amr/seq2seq/__init__.py +3 -0
  33. hanlp/components/amr/seq2seq/dataset/IO.py +35 -0
  34. hanlp/components/amr/seq2seq/dataset/__init__.py +3 -0
  35. hanlp/components/amr/seq2seq/dataset/dataset.py +235 -0
  36. hanlp/components/amr/seq2seq/dataset/linearization.py +408 -0
  37. hanlp/components/amr/seq2seq/dataset/penman.py +85 -0
  38. hanlp/components/amr/seq2seq/dataset/postprocessing.py +636 -0
  39. hanlp/components/amr/seq2seq/dataset/tokenization_bart.py +671 -0
  40. hanlp/components/amr/seq2seq/dataset/tokenization_t5.py +673 -0
  41. hanlp/components/amr/seq2seq/evaluation.py +24 -0
  42. hanlp/components/amr/seq2seq/optim.py +100 -0
  43. hanlp/components/amr/seq2seq/seq2seq_amr_parser.py +510 -0
  44. hanlp/components/classifiers/__init__.py +3 -0
  45. hanlp/components/classifiers/fasttext_classifier.py +95 -0
  46. hanlp/components/classifiers/transformer_classifier.py +383 -0
  47. hanlp/components/classifiers/transformer_classifier_hf.py +125 -0
  48. hanlp/components/classifiers/transformer_classifier_tf.py +193 -0
  49. hanlp/components/classifiers/transformer_regression_hf.py +99 -0
  50. hanlp/components/distillation/__init__.py +3 -0
  51. hanlp/components/distillation/distillable_component.py +54 -0
  52. hanlp/components/distillation/losses.py +285 -0
  53. hanlp/components/distillation/schedulers.py +124 -0
  54. hanlp/components/eos/__init__.py +3 -0
  55. hanlp/components/eos/ngram.py +311 -0
  56. hanlp/components/lambda_wrapper.py +29 -0
  57. hanlp/components/lemmatizer.py +45 -0
  58. hanlp/components/lm/__init__.py +3 -0
  59. hanlp/components/lm/mlm.py +112 -0
  60. hanlp/components/mtl/__init__.py +3 -0
  61. hanlp/components/mtl/multi_task_learning.py +815 -0
  62. hanlp/components/mtl/tasks/__init__.py +288 -0
  63. hanlp/components/mtl/tasks/amr.py +174 -0
  64. hanlp/components/mtl/tasks/constituency.py +169 -0
  65. hanlp/components/mtl/tasks/dep.py +166 -0
  66. hanlp/components/mtl/tasks/dep_2nd.py +111 -0
  67. hanlp/components/mtl/tasks/lem.py +129 -0
  68. hanlp/components/mtl/tasks/ner/__init__.py +3 -0
  69. hanlp/components/mtl/tasks/ner/biaffine_ner.py +107 -0
  70. hanlp/components/mtl/tasks/ner/tag_ner.py +168 -0
  71. hanlp/components/mtl/tasks/pos.py +165 -0
  72. hanlp/components/mtl/tasks/sdp.py +170 -0
  73. hanlp/components/mtl/tasks/srl/__init__.py +3 -0
  74. hanlp/components/mtl/tasks/srl/bio_srl.py +126 -0
  75. hanlp/components/mtl/tasks/srl/rank_srl.py +121 -0
  76. hanlp/components/mtl/tasks/tok/__init__.py +3 -0
  77. hanlp/components/mtl/tasks/tok/reg_tok.py +109 -0
  78. hanlp/components/mtl/tasks/tok/tag_tok.py +205 -0
  79. hanlp/components/mtl/tasks/ud.py +171 -0
  80. hanlp/components/ner/__init__.py +3 -0
  81. hanlp/components/ner/biaffine_ner/__init__.py +3 -0
  82. hanlp/components/ner/biaffine_ner/biaffine_ner.py +401 -0
  83. hanlp/components/ner/biaffine_ner/biaffine_ner_model.py +132 -0
  84. hanlp/components/ner/ner_tf.py +83 -0
  85. hanlp/components/ner/rnn_ner.py +69 -0
  86. hanlp/components/ner/transformer_ner.py +240 -0
  87. hanlp/components/parsers/__init__.py +3 -0
  88. hanlp/components/parsers/alg.py +761 -0
  89. hanlp/components/parsers/alg_tf.py +289 -0
  90. hanlp/components/parsers/biaffine/__init__.py +3 -0
  91. hanlp/components/parsers/biaffine/biaffine.py +98 -0
  92. hanlp/components/parsers/biaffine/biaffine_2nd_dep.py +213 -0
  93. hanlp/components/parsers/biaffine/biaffine_dep.py +575 -0
  94. hanlp/components/parsers/biaffine/biaffine_model.py +230 -0
  95. hanlp/components/parsers/biaffine/biaffine_sdp.py +203 -0
  96. hanlp/components/parsers/biaffine/mlp.py +83 -0
  97. hanlp/components/parsers/biaffine/structual_attention.py +234 -0
  98. hanlp/components/parsers/biaffine/variationalbilstm.py +231 -0
  99. hanlp/components/parsers/biaffine_parser_tf.py +567 -0
  100. hanlp/components/parsers/biaffine_tf/__init__.py +3 -0
  101. hanlp/components/parsers/biaffine_tf/alg.py +289 -0
  102. hanlp/components/parsers/biaffine_tf/layers.py +120 -0
  103. hanlp/components/parsers/biaffine_tf/model.py +150 -0
  104. hanlp/components/parsers/chu_liu_edmonds.py +314 -0
  105. hanlp/components/parsers/conll.py +73 -0
  106. hanlp/components/parsers/constituency/__init__.py +3 -0
  107. hanlp/components/parsers/constituency/crf_constituency_model.py +214 -0
  108. hanlp/components/parsers/constituency/crf_constituency_parser.py +322 -0
  109. hanlp/components/parsers/constituency/treecrf.py +360 -0
  110. hanlp/components/parsers/parse_alg.py +312 -0
  111. hanlp/components/parsers/ud/__init__.py +3 -0
  112. hanlp/components/parsers/ud/lemma_edit.py +135 -0
  113. hanlp/components/parsers/ud/tag_decoder.py +136 -0
  114. hanlp/components/parsers/ud/ud_model.py +139 -0
  115. hanlp/components/parsers/ud/ud_parser.py +346 -0
  116. hanlp/components/parsers/ud/udify_util.py +369 -0
  117. hanlp/components/parsers/ud/util.py +28 -0
  118. hanlp/components/pipeline.py +179 -0
  119. hanlp/components/rnn_language_model_tf.py +84 -0
  120. hanlp/components/srl/__init__.py +3 -0
  121. hanlp/components/srl/span_bio/__init__.py +3 -0
  122. hanlp/components/srl/span_bio/baffine_tagging.py +75 -0
  123. hanlp/components/srl/span_bio/span_bio.py +384 -0
  124. hanlp/components/srl/span_rank/__init__.py +3 -0
  125. hanlp/components/srl/span_rank/highway_variational_lstm.py +250 -0
  126. hanlp/components/srl/span_rank/inference_utils.py +243 -0
  127. hanlp/components/srl/span_rank/layer.py +388 -0
  128. hanlp/components/srl/span_rank/span_rank.py +417 -0
  129. hanlp/components/srl/span_rank/span_ranking_srl_model.py +508 -0
  130. hanlp/components/srl/span_rank/srl_eval_utils.py +287 -0
  131. hanlp/components/srl/span_rank/util.py +12 -0
  132. hanlp/components/sts/__init__.py +3 -0
  133. hanlp/components/sts/transformer_sts.py +242 -0
  134. hanlp/components/taggers/__init__.py +3 -0
  135. hanlp/components/taggers/cnn_tagger_tf.py +125 -0
  136. hanlp/components/taggers/ngram_conv/__init__.py +3 -0
  137. hanlp/components/taggers/ngram_conv/ngram_conv_tagger.py +177 -0
  138. hanlp/components/taggers/pos_tf.py +13 -0
  139. hanlp/components/taggers/rnn/__init__.py +3 -0
  140. hanlp/components/taggers/rnn/rnntaggingmodel.py +100 -0
  141. hanlp/components/taggers/rnn_tagger.py +195 -0
  142. hanlp/components/taggers/rnn_tagger_tf.py +89 -0
  143. hanlp/components/taggers/tagger.py +231 -0
  144. hanlp/components/taggers/tagger_tf.py +38 -0
  145. hanlp/components/taggers/transformers/__init__.py +3 -0
  146. hanlp/components/taggers/transformers/metrics_tf.py +15 -0
  147. hanlp/components/taggers/transformers/transformer_tagger.py +294 -0
  148. hanlp/components/taggers/transformers/transformer_tagger_tf.py +94 -0
  149. hanlp/components/taggers/transformers/transformer_transform_tf.py +132 -0
  150. hanlp/components/taggers/util.py +22 -0
  151. hanlp/components/tokenizers/__init__.py +3 -0
  152. hanlp/components/tokenizers/multi_criteria_cws_transformer.py +89 -0
  153. hanlp/components/tokenizers/tok.py +54 -0
  154. hanlp/components/tokenizers/tok_tf.py +110 -0
  155. hanlp/components/tokenizers/transformer.py +299 -0
  156. hanlp/datasets/__init__.py +3 -0
  157. hanlp/datasets/classification/__init__.py +3 -0
  158. hanlp/datasets/classification/sentiment.py +8 -0
  159. hanlp/datasets/coref/__init__.py +3 -0
  160. hanlp/datasets/coref/loaders/__init__.py +3 -0
  161. hanlp/datasets/coref/loaders/conll12coref.py +88 -0
  162. hanlp/datasets/eos/__init__.py +3 -0
  163. hanlp/datasets/eos/eos.py +101 -0
  164. hanlp/datasets/eos/loaders/__init__.py +3 -0
  165. hanlp/datasets/eos/loaders/nn_eos.py +17 -0
  166. hanlp/datasets/lm/__init__.py +12 -0
  167. hanlp/datasets/lm/loaders/__init__.py +3 -0
  168. hanlp/datasets/lm/loaders/lm_dataset.py +142 -0
  169. hanlp/datasets/lu/__init__.py +3 -0
  170. hanlp/datasets/lu/glue.py +25 -0
  171. hanlp/datasets/ner/__init__.py +3 -0
  172. hanlp/datasets/ner/conll03.py +11 -0
  173. hanlp/datasets/ner/loaders/__init__.py +3 -0
  174. hanlp/datasets/ner/loaders/json_ner.py +155 -0
  175. hanlp/datasets/ner/loaders/tsv.py +86 -0
  176. hanlp/datasets/ner/msra.py +34 -0
  177. hanlp/datasets/ner/resume.py +16 -0
  178. hanlp/datasets/ner/weibo.py +15 -0
  179. hanlp/datasets/parsing/__init__.py +3 -0
  180. hanlp/datasets/parsing/amr.py +390 -0
  181. hanlp/datasets/parsing/ctb5.py +17 -0
  182. hanlp/datasets/parsing/ctb7.py +13 -0
  183. hanlp/datasets/parsing/ctb8.py +44 -0
  184. hanlp/datasets/parsing/ctb9.py +55 -0
  185. hanlp/datasets/parsing/loaders/__init__.py +3 -0
  186. hanlp/datasets/parsing/loaders/_ctb_utils.py +320 -0
  187. hanlp/datasets/parsing/loaders/conll_dataset.py +106 -0
  188. hanlp/datasets/parsing/loaders/constituency_dataset.py +211 -0
  189. hanlp/datasets/parsing/pmt1.py +58 -0
  190. hanlp/datasets/parsing/ptb.py +48 -0
  191. hanlp/datasets/parsing/semeval15.py +61 -0
  192. hanlp/datasets/parsing/semeval16.py +64 -0
  193. hanlp/datasets/parsing/ud/__init__.py +30 -0
  194. hanlp/datasets/parsing/ud/ud210.py +1019 -0
  195. hanlp/datasets/parsing/ud/ud210m.py +15 -0
  196. hanlp/datasets/parsing/ud/ud23.py +341 -0
  197. hanlp/datasets/parsing/ud/ud23m.py +12 -0
  198. hanlp/datasets/parsing/ud/ud27.py +855 -0
  199. hanlp/datasets/parsing/ud/ud27m.py +15 -0
  200. hanlp/datasets/pos/__init__.py +3 -0
  201. hanlp/datasets/pos/ctb5.py +12 -0
  202. hanlp/datasets/qa/__init__.py +3 -0
  203. hanlp/datasets/qa/hotpotqa.py +194 -0
  204. hanlp/datasets/srl/__init__.py +5 -0
  205. hanlp/datasets/srl/loaders/__init__.py +3 -0
  206. hanlp/datasets/srl/loaders/conll2012.py +227 -0
  207. hanlp/datasets/srl/loaders/ontonotes_loader.py +615 -0
  208. hanlp/datasets/srl/ontonotes5/__init__.py +5 -0
  209. hanlp/datasets/srl/ontonotes5/_utils.py +677 -0
  210. hanlp/datasets/srl/ontonotes5/chinese.py +106 -0
  211. hanlp/datasets/srl/ontonotes5/english.py +91 -0
  212. hanlp/datasets/sts/__init__.py +3 -0
  213. hanlp/datasets/sts/stsb.py +36 -0
  214. hanlp/datasets/tokenization/__init__.py +3 -0
  215. hanlp/datasets/tokenization/ctb6.py +12 -0
  216. hanlp/datasets/tokenization/loaders/__init__.py +3 -0
  217. hanlp/datasets/tokenization/loaders/chunking_dataset.py +48 -0
  218. hanlp/datasets/tokenization/loaders/multi_criteria_cws/__init__.py +35 -0
  219. hanlp/datasets/tokenization/loaders/multi_criteria_cws/mcws_dataset.py +98 -0
  220. hanlp/datasets/tokenization/loaders/txt.py +129 -0
  221. hanlp/datasets/tokenization/sighan2005/__init__.py +22 -0
  222. hanlp/datasets/tokenization/sighan2005/as_.py +19 -0
  223. hanlp/datasets/tokenization/sighan2005/cityu.py +19 -0
  224. hanlp/datasets/tokenization/sighan2005/msr.py +19 -0
  225. hanlp/datasets/tokenization/sighan2005/pku.py +19 -0
  226. hanlp/layers/__init__.py +3 -0
  227. hanlp/layers/cnn_encoder.py +118 -0
  228. hanlp/layers/crf/__init__.py +3 -0
  229. hanlp/layers/crf/crf.py +353 -0
  230. hanlp/layers/crf/crf_layer_tf.py +165 -0
  231. hanlp/layers/crf/crf_tf.py +520 -0
  232. hanlp/layers/dropout.py +159 -0
  233. hanlp/layers/embeddings/__init__.py +3 -0
  234. hanlp/layers/embeddings/char_cnn.py +147 -0
  235. hanlp/layers/embeddings/char_cnn_tf.py +103 -0
  236. hanlp/layers/embeddings/char_rnn.py +109 -0
  237. hanlp/layers/embeddings/char_rnn_tf.py +61 -0
  238. hanlp/layers/embeddings/concat_embedding.py +60 -0
  239. hanlp/layers/embeddings/contextual_string_embedding.py +216 -0
  240. hanlp/layers/embeddings/contextual_string_embedding_tf.py +135 -0
  241. hanlp/layers/embeddings/contextual_word_embedding.py +187 -0
  242. hanlp/layers/embeddings/embedding.py +133 -0
  243. hanlp/layers/embeddings/fast_text.py +167 -0
  244. hanlp/layers/embeddings/fast_text_tf.py +104 -0
  245. hanlp/layers/embeddings/util.py +110 -0
  246. hanlp/layers/embeddings/util_tf.py +88 -0
  247. hanlp/layers/embeddings/word2vec.py +355 -0
  248. hanlp/layers/embeddings/word2vec_tf.py +197 -0
  249. hanlp/layers/feed_forward.py +15 -0
  250. hanlp/layers/feedforward.py +107 -0
  251. hanlp/layers/scalar_mix.py +156 -0
  252. hanlp/layers/time_distributed.py +72 -0
  253. hanlp/layers/transformers/__init__.py +13 -0
  254. hanlp/layers/transformers/encoder.py +129 -0
  255. hanlp/layers/transformers/loader_tf.py +24 -0
  256. hanlp/layers/transformers/pt_imports.py +69 -0
  257. hanlp/layers/transformers/relative_transformer.py +329 -0
  258. hanlp/layers/transformers/resource.py +38 -0
  259. hanlp/layers/transformers/tf_imports.py +6 -0
  260. hanlp/layers/transformers/utils.py +369 -0
  261. hanlp/layers/transformers/utils_tf.py +191 -0
  262. hanlp/layers/weight_normalization.py +224 -0
  263. hanlp/losses/__init__.py +3 -0
  264. hanlp/losses/sparse_categorical_crossentropy.py +63 -0
  265. hanlp/metrics/__init__.py +3 -0
  266. hanlp/metrics/accuracy.py +227 -0
  267. hanlp/metrics/amr/__init__.py +3 -0
  268. hanlp/metrics/amr/smatch_eval.py +101 -0
  269. hanlp/metrics/chunking/__init__.py +3 -0
  270. hanlp/metrics/chunking/binary_chunking_f1.py +33 -0
  271. hanlp/metrics/chunking/bmes_tf.py +47 -0
  272. hanlp/metrics/chunking/chunking_f1.py +138 -0
  273. hanlp/metrics/chunking/chunking_f1_tf.py +65 -0
  274. hanlp/metrics/chunking/conlleval.py +344 -0
  275. hanlp/metrics/chunking/iobes_tf.py +28 -0
  276. hanlp/metrics/chunking/sequence_labeling.py +404 -0
  277. hanlp/metrics/f1.py +64 -0
  278. hanlp/metrics/metric.py +44 -0
  279. hanlp/metrics/mtl.py +45 -0
  280. hanlp/metrics/parsing/__init__.py +3 -0
  281. hanlp/metrics/parsing/attachmentscore.py +75 -0
  282. hanlp/metrics/parsing/conllx_eval.py +67 -0
  283. hanlp/metrics/parsing/labeled_f1.py +103 -0
  284. hanlp/metrics/parsing/labeled_f1_tf.py +103 -0
  285. hanlp/metrics/parsing/labeled_score.py +59 -0
  286. hanlp/metrics/parsing/semdep_eval.py +137 -0
  287. hanlp/metrics/parsing/span.py +103 -0
  288. hanlp/metrics/spearman_correlation.py +86 -0
  289. hanlp/metrics/srl/__init__.py +3 -0
  290. hanlp/metrics/srl/srlconll.py +39 -0
  291. hanlp/optimizers/__init__.py +3 -0
  292. hanlp/optimizers/adamw/__init__.py +71 -0
  293. hanlp/optimizers/adamw/optimization.py +240 -0
  294. hanlp/pretrained/__init__.py +23 -0
  295. hanlp/pretrained/amr.py +72 -0
  296. hanlp/pretrained/amr2text.py +13 -0
  297. hanlp/pretrained/classifiers.py +18 -0
  298. hanlp/pretrained/constituency.py +19 -0
  299. hanlp/pretrained/dep.py +23 -0
  300. hanlp/pretrained/eos.py +10 -0
  301. hanlp/pretrained/fasttext.py +16 -0
  302. hanlp/pretrained/glove.py +19 -0
  303. hanlp/pretrained/mtl.py +52 -0
  304. hanlp/pretrained/ner.py +15 -0
  305. hanlp/pretrained/pos.py +25 -0
  306. hanlp/pretrained/rnnlm.py +13 -0
  307. hanlp/pretrained/sdp.py +21 -0
  308. hanlp/pretrained/srl.py +9 -0
  309. hanlp/pretrained/sts.py +10 -0
  310. hanlp/pretrained/tok.py +51 -0
  311. hanlp/pretrained/word2vec.py +38 -0
  312. hanlp/transform/__init__.py +3 -0
  313. hanlp/transform/conll_tf.py +814 -0
  314. hanlp/transform/glue_tf.py +44 -0
  315. hanlp/transform/prepend_space.py +28 -0
  316. hanlp/transform/table_tf.py +89 -0
  317. hanlp/transform/tacred_tf.py +106 -0
  318. hanlp/transform/text_tf.py +101 -0
  319. hanlp/transform/transformer_tokenizer.py +718 -0
  320. hanlp/transform/tsv_tf.py +151 -0
  321. hanlp/transform/txt_tf.py +201 -0
  322. hanlp/utils/__init__.py +19 -0
  323. hanlp/utils/component_util.py +197 -0
  324. hanlp/utils/file_read_backwards/__init__.py +7 -0
  325. hanlp/utils/file_read_backwards/buffer_work_space.py +202 -0
  326. hanlp/utils/file_read_backwards/file_read_backwards.py +146 -0
  327. hanlp/utils/init_util.py +16 -0
  328. hanlp/utils/io_util.py +719 -0
  329. hanlp/utils/lang/__init__.py +7 -0
  330. hanlp/utils/lang/en/__init__.py +3 -0
  331. hanlp/utils/lang/en/english_tokenizer.py +325 -0
  332. hanlp/utils/lang/ja/__init__.py +3 -0
  333. hanlp/utils/lang/ja/bert_tok.py +81 -0
  334. hanlp/utils/lang/zh/__init__.py +3 -0
  335. hanlp/utils/lang/zh/char_table.py +53 -0
  336. hanlp/utils/lang/zh/localization.py +35 -0
  337. hanlp/utils/log_util.py +182 -0
  338. hanlp/utils/rules.py +40 -0
  339. hanlp/utils/span_util.py +729 -0
  340. hanlp/utils/string_util.py +117 -0
  341. hanlp/utils/tf_util.py +222 -0
  342. hanlp/utils/time_util.py +246 -0
  343. hanlp/utils/torch_util.py +295 -0
  344. hanlp/version.py +10 -0
  345. hanlp-2.1.0.dist-info/LICENSE +201 -0
  346. hanlp-2.1.0.dist-info/METADATA +248 -0
  347. hanlp-2.1.0.dist-info/RECORD +349 -0
  348. hanlp-2.1.0.dist-info/WHEEL +5 -0
  349. 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,3 @@
1
+ # -*- coding:utf-8 -*-
2
+ # Author: hankcs
3
+ # Date: 2019-12-05 02:10
@@ -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()
@@ -0,0 +1,3 @@
1
+ # -*- coding:utf-8 -*-
2
+ # Author: hankcs
3
+ # Date: 2019-08-26 14:45
@@ -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)