2 Star 19 Fork 3

fastNLP / fastHan

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

fastHan

简介

fastHan是基于fastNLP与pytorch实现的中文自然语言处理工具,像spacy一样调用方便。

其内核为基于BERT的联合模型,其在13个语料库中进行训练,可处理中文分词、词性标注、依存分析、命名实体识别四项任务。fastHan共有base与large两个版本,分别利用BERT的前四层与前八层。base版本在总参数量150MB的情况下各项任务均有不错表现,large版本则接近甚至超越SOTA模型。

安装指南

fastHan需要以下依赖的包:

torch>=1.0.0

fastNLP>=0.5.0

版本更新:

  • 1.1版本的fastHan与0.5.5版本的fastNLP会导致importerror。如果使用1.1版本的fastHan,请使用0.5.0版本的fastNLP。
  • 1.2版本的fastHan修复了fastNLP版本兼容问题。小于等于1.2版本的fastHan在输入句子的首尾包含空格、换行符时会产生BUG。如果字符串首尾包含上述字符,请使用 strip 函数处理输入字符串。
  • 1.3版本的fastHan自动对输入字符串做 strip 函数处理。
  • 1.4版本的fastHan加入用户词典功能(仅限于分词任务)

可执行如下命令完成安装:

pip install fastHan==1.4

使用教程

fastHan的使用极为简单,只需两步:加载模型、将句子输入模型。

加载模型

执行以下代码可以加载模型:

from fastHan import FastHan
model=FastHan()

此时若用户为首次初始化模型,将自动从服务器中下载参数。

模型默认初始化为base,如果使用large版本,可在初始化时加入如下参数:

model=FastHan(model_type="large")

输入句子

模型对句子进行依存分析、命名实体识别的简单例子如下:

sentence="郭靖是金庸笔下的一名男主。"
answer=model(sentence,target="Parsing")
print(answer)
answer=model(sentence,target="NER")
print(answer)

模型将会输出如下信息:

[[['郭靖', 2, 'top', 'NR'], ['是', 0, 'root', 'VC'], ['金庸', 4, 'nn', 'NR'], ['笔', 5, 'lobj', 'NN'], ['下', 10, 'assmod', 'LC'], ['的', 5, 'assm', 'DEG'], ['一', 8, 'nummod', 'CD'], ['名', 10, 'clf', 'M'], ['男', 10, 'amod', 'JJ'], ['主', 2, 'attr', 'NN'], ['。', 2, 'punct', 'PU']]]
[[['郭靖', 'NR'], ['金庸', 'NR']]]

任务选择

target参数可在'Parsing'、'CWS'、'POS'、'NER'四个选项中取值,模型将分别进行依存分析、分词、词性标注、命名实体识别任务,模型默认进行CWS任务。其中词性标注任务包含了分词的信息,而依存分析任务又包含了词性标注任务的信息。命名实体识别任务相较其他任务独立。

如果分别运行CWS、POS、Parsing任务,模型输出的分词结果等可能存在冲突。如果想获得不冲突的各类信息,请直接运行包含全部所需信息的那项任务。

模型的POS、Parsing任务均使用CTB标签集。NER使用msra标签集。

分词风格

分词风格,指的是训练模型中文分词模块的10个语料库,模型可以区分这10个语料库,设置分词style为S即令模型认为现在正在处理S语料库的分词。所以分词style实际上是与语料库的覆盖面、分词粒度相关的。如本模型默认的CTB语料库分词粒度较细。如果想切换不同的粒度,可以使用模型的 set_cws_style 函数,例子如下:

sentence="一个苹果。"
print(model(sentence,'CWS'))
model.set_cws_style('cnc')
print(model(sentence,'CWS'))

模型将输出如下内容:

[['一', '个', '苹果', '。']]
[['一个', '苹果', '。']]

对语料库的选取参考了下方CWS SOTA模型的论文,共包括:SIGHAN 2005的 MSR、PKU、AS、CITYU 语料库,由山西大学发布的 SXU 语料库,由斯坦福的CoreNLP 发布的 CTB6 语料库,由国家语委公布的 CNC 语料库,由王威廉先生公开的微博树库 WTB,由张梅山先生公开的诛仙语料库 ZX,Universal Dependencies 项目的 UD 语料库。

输入与输出

输入模型的可以是单独的字符串,也可是由字符串组成的列表。如果输入的是列表,模型将一次性处理所有输入的字符串,所以请自行控制 batch size。

模型的输出是在fastHan模块中定义的sentence与token类。模型将输出一个由sentence组成的列表,而每个sentence又由token组成。每个token本身代表一个被分好的词,有pos、head、head_label、ner四项属性,代表了该词的词性、依存关系、命名实体识别信息。

一则输入输出的例子如下所示:

sentence=["我爱踢足球。","林丹是冠军"]
answer=model(sentence,'Parsing')
for i,sentence in enumerate(answer):
    print(i)
    for token in sentence:
        print(token,token.pos,token.head,token.head_label)

上述代码将输出如下内容:

0
我 PN 2 nsubj
爱 VV 0 root
踢 VV 2 ccomp
足球 NN 3 dobj
。 PU 2 punct
1
林丹 NR 2 top
是 VC 0 root
冠军 NN 2 attr
! PU 2 punct

可在分词风格中选择'as'、'cityu'进行繁体字分词,这两项为繁体语料库。

此外,由于各项任务共享词表、词嵌入,即使不切换模型的分词风格,模型对繁体字、英文字母、数字均具有一定识别能力。

切换设备

可使用模型的 set_device 函数,令模型在cuda上运行或切换回cpu,示例如下:

model.set_device('cuda:0')
model.set_device('cpu')

词典分词

用户可以使用模型的 add_user_dict 函数添加自定义词典,该词典会影响模型在分词任务中的权重分配。进行分词任务时,首先利用词典进行正向、反向最大匹配法进行分词,并将词典方法的分词结果乘上权重系数融入到深度学习模型的结果中。该函数的参数可以是由词组成的list,也可以是文件路径(文件中的内容是由'\n'分隔开的词)。

用户可使用 set_user_dict_weight 函数设置权重系数(若不设置,默认为0.05)。我们在大规模的训练语料库中发现0.05-0.1即可取得较好的结果。条件允许的情况下,用户也可以自行设置验证集、测试集,找到最适合自己任务的权重系数。

添加完用户词典后,需要在调用模型时令 use_dict 参数为True。再次申明,词典功能目前仅在'CWS'任务中有效。

用户可调用 remove_user_dict 移除之前添加的用户词典。

使用用户词典影响分词的一则例子如下:

sentence="奥利奥利奥"
print(model(sentence))
model.add_user_dict(["奥利","奥利奥"])
model.set_user_dict_weight(0.05)
print(model(sentence,use_dict=True))

输出为:

[['奥利奥利奥']]
[['奥利', '奥利奥']]

模型表现

准确率测试

模型在以下数据集进行训练和准确性测试:

  • CWS:AS, CITYU, CNC, CTB, MSR, PKU, SXU, UDC, WTB, ZX
  • NER:MSRA、OntoNotes
  • POS & Parsing:CTB9

注:模型在训练NER OntoNotes时将其标签集转换为与MSRA一致。

模型在ctb分词语料库的前800句进行了速度测试,平均每句有45.2个字符。测试环境为私人电脑, Intel Core i5-9400f + NVIDIA GeForce GTX 1660ti,batch size取8。经测试依存分析运行速度较慢,其他各项任务运行速度大致相同。base模型上依存分析任务使用GPU效果不佳,是因为依存分析利用POS的结果需要大量CPU计算,GPU带来的加速效果小于信息传递的负担。

最终模型取得的表现如下:

任务 CWS Parsing POS NER MSRA NER OntoNotes 速度(句/s),cpu 速度(句/s),gpu
SOTA模型 97.1 85.66,81.71 93.15 95.25 79.92 —— ——
base模型 97.27 81.22,76.71 94.88 94.33 82.86 25-55 22-111
large模型 97.41 85.52,81.38 95.66 95.50 83.82 14-28 21-97

注:模型在句首加入语料库标签来区分输入句子的任务及语料库,最初测试计算F值时将语料库标签也算入在内,导致CWS、POS分值偏高。现在已修复此处错误并更新了表格中的CWS及POS项。评分略有下降(CWS平均下降0.11,POS平均下降0.29),但仍超越SOTA模型。

表格中单位为百分数。CWS的成绩是10项任务的平均成绩。Parsing中的两个成绩分别代表Fudep和Fldep。SOTA模型的数据来自笔者对网上资料及论文的查阅,如有缺漏请指正,不胜感激。这五项SOTA表现分别来自如下五篇论文:

  1. Huang W, Cheng X, Chen K, et al. Toward Fast and Accurate Neural Chinese Word Segmentation with Multi-Criteria Learning.[J]. arXiv: Computation and Language, 2019.
  2. Hang Yan, Xipeng Qiu, and Xuanjing Huang. "A Graph-based Model for Joint Chinese Word Segmentation and Dependency Parsing." Transactions of the Association for Computational Linguistics 8 (2020): 78-92.
  3. Meng Y, Wu W, Wang F, et al. Glyce: Glyph-vectors for Chinese Character Representations[J]. arXiv: Computation and Language, 2019.
  4. Diao S, Bai J, Song Y, et al. ZEN: Pre-training Chinese Text Encoder Enhanced by N-gram Representations[J]. arXiv: Computation and Language, 2019.
  5. Jie Z, Lu W. Dependency-Guided LSTM-CRF for Named Entity Recognition[C]. international joint conference on natural language processing, 2019: 3860-3870.

关于更多模型结构和训练的内容,可以查看此篇论文

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] 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.

简介

中文处理工具 展开 收起
Python
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/fastnlp/fastHan.git
git@gitee.com:fastnlp/fastHan.git
fastnlp
fastHan
fastHan
master

搜索帮助