开源一个中文信息抽取工具(持续完善中...)
CNN4IE
项目地址:https://github.com/jiangnanboy/CNN4IE
本项目是本人打算使用CNN的不同变体进行信息抽取,未来会持续加入不同模型。
CNN4IE根据CNN的各种改进版本,对不同模型块进行融合,并将其用于中文信息抽取中。
Intro
目前主要实现中文实体抽取:
训练样本以B、I、O形式进行标注。
Model
模型
- MultiLayerResCNN(src/mlrescnn):多层残差CNN(+CRF)。
Usage
-
相关参数的配置
config.cfg,在训练或预测时加载此文件的位置。
-
训练(支持加载预训练的embedding向量)
from cnn4ie.mlrescnn.train import Train train = Train() train.train_model('config.cfg')
Epoch: 199 | Time: 0m 4s Train Loss: 228.545 | Train PPL: 1.802960293422957e+99 Val. Loss: 433.577 | Val. PPL: 1.9966207577208172e+188 Val. report: precision recall f1-score support 1 1.00 1.00 1.00 4539 2 0.98 0.99 0.99 4926 3 0.90 0.83 0.86 166 4 0.74 0.98 0.84 52 5 0.94 0.77 0.84 120 6 0.76 0.97 0.85 39 7 0.82 0.87 0.85 54 8 0.93 0.74 0.82 68 9 0.95 0.77 0.85 26 10 1.00 0.80 0.89 10 accuracy 0.98 10000 macro avg 0.90 0.87 0.88 10000 weighted avg 0.99 0.98 0.98 10000
-
预测
from cnn4ie.mlrescnn.predict import Predict predict = Predict() predict.load_model_vocab('config_cfg') result = predict.predict('据新华社报道,安徽省六安市被评上十大易居城市!') print(result)
[{'start': 7, 'stop': 13, 'word': '安徽省六安市', 'type': 'LOC'}, {'start': 1, 'stop': 4, 'word': '新华社', 'type': 'ORG'}]
Evaluate
评估采用的是P、R、F1、PPL等。评估方法可利用scikit-learn中的precision_recall_fscore_support或classification_report。
Install
- 安装:pip install CNN4IE
- 下载源码:
git clone https://github.com/jiangnanboy/CNN4IE.git
cd CNN4IE
python setup.py install