Views
No views yet
1@article{che2020n,
2 title={N-LTP: A Open-source Neural Chinese Language Technology Platform with Pretrained Models},
3 author={Che, Wanxiang and Feng, Yunlong and Qin, Libo and Liu, Ting},
4 journal={arXiv preprint arXiv:2009.11616},
5 year={2020}
6}pip install -U ltp ltp-core ltp-extension -i https://pypi.org/simple # 安装 ltp1import torch
2from ltp import LTP
3
4ltp = LTP("LTP/small") # 默认加载 Small 模型
5
6# 将模型移动到 GPU 上
7if torch.cuda.is_available():
8 # ltp.cuda()
9 ltp.to("cuda")
10
11output = ltp.pipeline(["他叫汤姆去拿外衣。"], tasks=["cws", "pos", "ner", "srl", "dep", "sdp"])
12# 使用字典格式作为返回结果
13print(output.cws) # print(output[0]) / print(output['cws']) # 也可以使用下标访问
14print(output.pos)
15print(output.sdp)
16
17# 使用感知机算法实现的分词、词性和命名实体识别,速度比较快,但是精度略低
18ltp = LTP("LTP/legacy")
19# cws, pos, ner = ltp.pipeline(["他叫汤姆去拿外衣。"], tasks=["cws", "ner"]).to_tuple() # error: NER 需要 词性标注任务的结果
20cws, pos, ner = ltp.pipeline(["他叫汤姆去拿外衣。"], tasks=["cws", "pos", "ner"]).to_tuple() # to tuple 可以自动转换为元组格式
21# 使用元组格式作为返回结果
22print(cws, pos, ner)1use std::fs::File;
2use itertools::multizip;
3use ltp::{CWSModel, POSModel, NERModel, ModelSerde, Format, Codec};
4
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 let file = File::open("data/legacy-models/cws_model.bin")?;
7 let cws: CWSModel = ModelSerde::load(file, Format::AVRO(Codec::Deflate))?;
8 let file = File::open("data/legacy-models/pos_model.bin")?;
9 let pos: POSModel = ModelSerde::load(file, Format::AVRO(Codec::Deflate))?;
10 let file = File::open("data/legacy-models/ner_model.bin")?;
11 let ner: NERModel = ModelSerde::load(file, Format::AVRO(Codec::Deflate))?;
12
13 let words = cws.predict("他叫汤姆去拿外衣。")?;
14 let pos = pos.predict(&words)?;
15 let ner = ner.predict((&words, &pos))?;
16
17 for (w, p, n) in multizip((words, pos, ner)) {
18 println!("{}/{}/{}", w, p, n);
19 }
20
21 Ok(())
22}| 深度学习模型 | 分词 | 词性 | 命名实体 | 语义角色 | 依存句法 | 语义依存 | 速度(句/S) |
|---|---|---|---|---|---|---|---|
| Base | 98.7 | 98.5 | 95.4 | 80.6 | 89.5 | 75.2 | 39.12 |
| Base1 | 99.22 | 98.73 | 96.39 | 79.28 | 89.57 | 76.57 | --.-- |
| Base2 | 99.18 | 98.69 | 95.97 | 79.49 | 90.19 | 76.62 | --.-- |
| Small | 98.4 | 98.2 | 94.3 | 78.4 | 88.3 | 74.7 | 43.13 |
| Tiny | 96.8 | 97.1 | 91.6 | 70.9 | 83.8 | 70.1 | 53.22 |
make bdist