Skip to main content

ltp_server

基于Python的用FastAPI简单封装的LTP服务

安装

pip install ltp_server

服务端

使用方式

方式一:Python库引用

示例:

from ltp_server import Server
if __name__ == '__main__':
    model_path = r"/root/Data/NLP/Model/LTP"
    # server = Server(model_path=model_path)
    # server.run()
    Server(model_path).run()

方式二:shell命令

示例:

ltp_server --model_path=/root/Data/NLP/Model/LTP

可用选项

参数名 是否可选 默认值 说明
model_path 否 LTP模型路径(绝对路径)
dict_path 是 None 用户词表路径(绝对路径)
max_window 是 4 前向分词最大窗口
host 是 127.0.0.1 服务主机名
port 是 8000 服务监听端口

服务概览

服务功能 服务路由 请求方式
分句 /sent_split POST
增加自定义词语 /add_words POST
分词 /seg POST
词性标注 /pos POST
命名实体识别 /ner POST
语义角色标注 /srl POST
依存句法分析 /dep POST
语义依存分析(树) /sdp POST
语义依存分析(图) /sdpg POST

请求示例

分句

### sent_split
POST http://localhost:8000/sent_split
Content-Type: application/json

{
  "texts": ["曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"]
}

返回值:

{
  "texts": [
    "曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"
  ],
  "sents": [
    "曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"
  ],
  "status": 0
}

增加自定义词语

### add_words
POST http://localhost:8000/add_words
Content-Type: application/json

{
  "words": ["江大桥"]
}

返回值

{
  "status": 0
}

分词

### seg
POST http://localhost:8000/seg
Content-Type: application/json

{
  "texts": ["曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"]
}

返回值

{
  "status": 0,
  "texts": [
    "曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"
  ],
  "res": [
    [
      "曹操",
      "和",
      "司马懿",
      "去",
      "赶集",
      ",",
      "中途",
      "遇",
      "上",
      "关羽",
      ",",
      "一起",
      "吃",
      "了",
      "个",
      "饭",
      "。"
    ]
  ]
}

词性标注

### pos
POST http://localhost:8000/pos
Content-Type: application/json

{
"texts": ["南京市长江大桥"]
}

返回值

{
  "status": 0,
  "texts": [
    "南京市长江大桥"
  ],
  "res": [
    [
      [
        "南京市",
        "ns"
      ],
      [
        "长江",
        "ns"
      ],
      [
        "大桥",
        "n"
      ]
    ]
  ],
  "seg": [
    [
      "南京市",
      "长江",
      "大桥"
    ]
  ]
}

命名实体识别

### ner
POST http://localhost:8000/ner
Content-Type: application/json

{
"texts": ["曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"]
}

返回值

{
  "status": 0,
  "texts": [
    "乔丹是一位出生在纽约的美国职业篮球运动员。"
  ],
  "res": [
    [
      [
        "乔丹",
        "Nh",
        0,
        0
      ],
      [
        "纽约",
        "Ns",
        6,
        6
      ],
      [
        "美国",
        "Ns",
        8,
        8
      ]
    ]
  ],
  "seg": [
    [
      "乔丹",
      "是",
      "一",
      "位",
      "出生",
      "在",
      "纽约",
      "的",
      "美国",
      "职业",
      "篮球",
      "运动员",
      "。"
    ]
  ]
}

语义角色标注

### srl
POST http://localhost:8000/srl
Content-Type: application/json

{
  "texts": ["曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"]
}

返回值

{
  "status": 0,
  "texts": [
    "乔丹是一位出生在纽约的美国职业篮球运动员。"
  ],
  "res": [
    [
      [
        "是",
        1,
        [
          [
            "A0",
            [
              "乔丹"
            ],
            0,
            0
          ],
          [
            "A1",
            [
              "一",
              "位",
              "出生",
              "在",
              "纽约",
              "的",
              "美国",
              "职业",
              "篮球",
              "运动员"
            ],
            2,
            11
          ]
        ]
      ],
      [
        "出生",
        4,
        [
          [
            "A1",
            [
              "在",
              "纽约"
            ],
            5,
            6
          ],
          [
            "A0",
            [
              "职业",
              "篮球",
              "运动员"
            ],
            9,
            11
          ]
        ]
      ]
    ]
  ],
  "seg": [
    [
      "乔丹",
      "是",
      "一",
      "位",
      "出生",
      "在",
      "纽约",
      "的",
      "美国",
      "职业",
      "篮球",
      "运动员",
      "。"
    ]
  ]
}

依存句法分析

### dep
POST http://localhost:8000/dep
Content-Type: application/json

{
  "texts": ["曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"]
}

返回值

{
  "status": 0,
  "texts": [
    "乔丹是一位出生在纽约的美国职业篮球运动员。"
  ],
  "res": [
    [
      [
        1,
        "乔丹",
        2,
        "是",
        "SBV"
      ],
      [
        2,
        "是",
        0,
        "ROOT",
        "HED"
      ],
      [
        3,
        "一",
        4,
        "位",
        "ATT"
      ],
      [
        4,
        "位",
        12,
        "运动员",
        "ATT"
      ],
      [
        5,
        "出生",
        12,
        "运动员",
        "ATT"
      ],
      [
        6,
        "在",
        5,
        "出生",
        "CMP"
      ],
      [
        7,
        "纽约",
        6,
        "在",
        "POB"
      ],
      [
        8,
        "的",
        5,
        "出生",
        "RAD"
      ],
      [
        9,
        "美国",
        12,
        "运动员",
        "ATT"
      ],
      [
        10,
        "职业",
        12,
        "运动员",
        "ATT"
      ],
      [
        11,
        "篮球",
        12,
        "运动员",
        "ATT"
      ],
      [
        12,
        "运动员",
        2,
        "是",
        "VOB"
      ],
      [
        13,
        "。",
        2,
        "是",
        "WP"
      ]
    ]
  ],
  "seg": [
    [
      "乔丹",
      "是",
      "一",
      "位",
      "出生",
      "在",
      "纽约",
      "的",
      "美国",
      "职业",
      "篮球",
      "运动员",
      "。"
    ]
  ]
}

语义依存分析(树)

### sdp
POST http://localhost:8000/sdp
Content-Type: application/json

{
  "texts": ["曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"]
}

返回值

{
  "status": 0,
  "texts": [
    "曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"
  ],
  "res": [
    [
      [
        1,
        "曹操",
        4,
        "去",
        "AGT"
      ],
      [
        1,
        "曹操",
        5,
        "赶集",
        "AGT"
      ],
      [
        2,
        "和",
        3,
        "司马懿",
        "mRELA"
      ],
      [
        3,
        "司马懿",
        4,
        "去",
        "AGT"
      ],
      [
        4,
        "去",
        0,
        "ROOT",
        "Root"
      ],
      [
        5,
        "赶集",
        4,
        "去",
        "eSUCC"
      ],
      [
        6,
        ",",
        5,
        "赶集",
        "mPUNC"
      ],
      [
        7,
        "中途",
        8,
        "遇",
        "MANN"
      ],
      [
        8,
        "遇",
        5,
        "赶集",
        "eSUCC"
      ],
      [
        9,
        "上",
        8,
        "遇",
        "mDEPD"
      ],
      [
        10,
        "关羽",
        8,
        "遇",
        "DATV"
      ],
      [
        11,
        ",",
        8,
        "遇",
        "mPUNC"
      ],
      [
        12,
        "一起",
        13,
        "吃",
        "MANN"
      ],
      [
        13,
        "吃",
        8,
        "遇",
        "eSUCC"
      ],
      [
        14,
        "了",
        13,
        "吃",
        "mDEPD"
      ],
      [
        15,
        "个",
        16,
        "饭",
        "MEAS"
      ],
      [
        16,
        "饭",
        13,
        "吃",
        "PAT"
      ],
      [
        17,
        "。",
        13,
        "吃",
        "mPUNC"
      ]
    ]
  ],
  "seg": [
    [
      "曹操",
      "和",
      "司马懿",
      "去",
      "赶集",
      ",",
      "中途",
      "遇",
      "上",
      "关羽",
      ",",
      "一起",
      "吃",
      "了",
      "个",
      "饭",
      "。"
    ]
  ]
}

语义依存分析(图)

### sdpg
POST http://localhost:8000/sdpg
Content-Type: application/json

{
  "texts": ["曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"]
}

返回值

{
  "status": 0,
  "texts": [
    "曹操和司马懿去赶集,中途遇上关羽,一起吃了个饭。"
  ],
  "res": [
    [
      [
        1,
        "曹操",
        4,
        "去",
        "AGT"
      ],
      [
        1,
        "曹操",
        5,
        "赶集",
        "AGT"
      ],
      [
        2,
        "和",
        3,
        "司马懿",
        "mRELA"
      ],
      [
        3,
        "司马懿",
        4,
        "去",
        "AGT"
      ],
      [
        4,
        "去",
        0,
        "ROOT",
        "Root"
      ],
      [
        5,
        "赶集",
        4,
        "去",
        "eSUCC"
      ],
      [
        6,
        ",",
        5,
        "赶集",
        "mPUNC"
      ],
      [
        7,
        "中途",
        8,
        "遇",
        "MANN"
      ],
      [
        8,
        "遇",
        5,
        "赶集",
        "eSUCC"
      ],
      [
        9,
        "上",
        8,
        "遇",
        "mDEPD"
      ],
      [
        10,
        "关羽",
        8,
        "遇",
        "DATV"
      ],
      [
        11,
        ",",
        8,
        "遇",
        "mPUNC"
      ],
      [
        12,
        "一起",
        13,
        "吃",
        "MANN"
      ],
      [
        13,
        "吃",
        8,
        "遇",
        "eSUCC"
      ],
      [
        14,
        "了",
        13,
        "吃",
        "mDEPD"
      ],
      [
        15,
        "个",
        16,
        "饭",
        "MEAS"
      ],
      [
        16,
        "饭",
        13,
        "吃",
        "PAT"
      ],
      [
        17,
        "。",
        13,
        "吃",
        "mPUNC"
      ]
    ]
  ],
  "seg": [
    [
      "曹操",
      "和",
      "司马懿",
      "去",
      "赶集",
      ",",
      "中途",
      "遇",
      "上",
      "关羽",
      ",",
      "一起",
      "吃",
      "了",
      "个",
      "饭",
      "。"
    ]
  ]
}

客户端

使用方式

方式一:Python库使用

示例如下:

from ltp_server import Client

if __name__ == '__main__':
    client = Client()
    texts = ["乔丹是一位出生在纽约的美国职业篮球运动员。"]

    print(client.sent_split(texts))
    print(client.seg(texts))
    print(client.pos(texts))
    print(client.ner(texts))
    print(client.srl(texts))
    print(client.dep(texts))
    print(client.sdp(texts))
    print(client.sdpg(texts))

请求结果:

{'texts': ['乔丹是一位出生在纽约的美国职业篮球运动员。'], 'res': ['乔丹是一位出生在纽约的美国职业篮球运动员。'], 'status': 0}
{'status': 0, 'texts': ['乔丹是一位出生在纽约的美国职业篮球运动员。'], 'res': [['乔丹', '是', '一', '位', '出生', '在', '纽约', '的', '美国', '职业', '篮球', '运动员', '。']]}
{'status': 0, 'texts': ['乔丹是一位出生在纽约的美国职业篮球运动员。'], 'res': [[['乔丹', 'nh'], ['是', 'v'], ['一', 'm'], ['位', 'q'], ['出生', 'v'], ['在', 'p'], ['纽约', 'ns'], ['的', 'u'], ['美国', 'ns'], ['职业', 'n'], ['篮球', 'n'], ['运动员', 'n'], ['。', 'wp']]], 'seg': [['乔丹', '是', '一', '位', '出生', '在', '纽约', '的', '美国', '职业', '篮球', '运动员', '。']]}
{'status': 0, 'texts': ['乔丹是一位出生在纽约的美国职业篮球运动员。'], 'res': [[['乔丹', 'Nh', 0, 0], ['纽约', 'Ns', 6, 6], ['美国', 'Ns', 8, 8]]], 'seg': [['乔丹', '是', '一', '位', '出生', '在', '纽约', '的', '美国', '职业', '篮球', '运动员', '。']]}
{'status': 0, 'texts': ['乔丹是一位出生在纽约的美国职业篮球运动员。'], 'res': [[['是', 1, [['A0', ['乔丹'], 0, 0], ['A1', ['一', '位', '出生', '在', '纽约', '的', '美国', '职业', '篮球', '运动员'], 2, 11]]], ['出生', 4, [['A1', ['在', '纽约'], 5, 6], ['A0', ['职业', '篮球', '运动员'], 9, 11]]]]], 'seg': [['乔丹', '是', '一', '位', '出生', '在', '纽约', '的', '美国', '职业', '篮球', '运动员', '。']]}
{'status': 0, 'texts': ['乔丹是一位出生在纽约的美国职业篮球运动员。'], 'res': [[[1, '乔丹', 2, '是', 'SBV'], [2, '是', 0, 'ROOT', 'HED'], [3, '一', 4, '位', 'ATT'], [4, '位', 12, '运动员', 'ATT'], [5, '出生', 12, '运动员', 'ATT'], [6, '在', 5, '出生', 'CMP'], [7, '纽约', 6, '在', 'POB'], [8, '的', 5, '出生', 'RAD'], [9, '美国', 12, '运动员', 'ATT'], [10, '职业', 12, '运动员', 'ATT'], [11, '篮球', 12, '运动员', 'ATT'], [12, '运动员', 2, '是', 'VOB'], [13, '。', 2, '是', 'WP']]], 'seg': [['乔丹', '是', '一', '位', '出生', '在', '纽约', '的', '美国', '职业', '篮球', '运动员', '。']]}
{'status': 0, 'texts': ['乔丹是一位出生在纽约的美国职业篮球运动员。'], 'res': [[[1, '乔丹', 2, '是', 'EXP'], [2, '是', 0, 'ROOT', 'Root'], [3, '一', 4, '位', 'MEAS'], [4, '位', 12, '运动员', 'MEAS'], [5, '出生', 12, '运动员', 'rEXP'], [6, '在', 7, '纽约', 'mRELA'], [7, '纽约', 5, '出生', 'LOC'], [8, '的', 5, '出生', 'mDEPD'], [9, '美国', 12, '运动员', 'FEAT'], [10, '职业', 11, '篮球', 'FEAT'], [10, '职业', 12, '运动员', 'FEAT'], [11, '篮球', 12, '运动员', 'FEAT'], [12, '运动员', 2, '是', 'LINK'], [13, '。', 2, '是', 'mPUNC']]], 'seg': [['乔丹', '是', '一', '位', '出生', '在', '纽约', '的', '美国', '职业', '篮球', '运动员', '。']]}
{'status': 0, 'texts': ['乔丹是一位出生在纽约的美国职业篮球运动员。'], 'res': [[[1, '乔丹', 2, '是', 'EXP'], [2, '是', 0, 'ROOT', 'Root'], [3, '一', 4, '位', 'MEAS'], [4, '位', 12, '运动员', 'MEAS'], [5, '出生', 12, '运动员', 'rEXP'], [6, '在', 7, '纽约', 'mRELA'], [7, '纽约', 5, '出生', 'LOC'], [8, '的', 5, '出生', 'mDEPD'], [9, '美国', 12, '运动员', 'FEAT'], [10, '职业', 11, '篮球', 'FEAT'], [10, '职业', 12, '运动员', 'FEAT'], [11, '篮球', 12, '运动员', 'FEAT'], [12, '运动员', 2, '是', 'LINK'], [13, '。', 2, '是', 'mPUNC']]], 'seg': [['乔丹', '是', '一', '位', '出生', '在', '纽约', '的', '美国', '职业', '篮球', '运动员', '。']]}

方式二:自己通过http请求调用

略

参考

Release files for ltp-server 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ltp-server 0.2.1
File Size Uploaded
ltp_server-0.2.1.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ltp-server 0.2.1
File Interpreter ABI Platform
ltp_server-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.2 kB

Release files / ltp_server-0.2.1.tar.gz

Download URL ltp_server-0.2.1.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a0aa2973530aeb446afae52c6e3d668c168b38800b07999ecd0d8b94bb4fbba5
BLAKE2b-256 checksum
How to use checksums
e5d399726f699336d665f1a02f71b56247eb06ad2a0d02445a2620381a35ea65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Release files / ltp_server-0.2.1-py3-none-any.whl

Download URL ltp_server-0.2.1-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78437acf2462b66487b574235bd1aed1522a3eadf62c94bae0e859bb60603755
BLAKE2b-256 checksum
How to use checksums
3bca044ffe38cd7b952c900281919edc95c6c3d6beaddc3a47440fdd86850a01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page