免费中文分词(文本处理):依存句法分析返回结构解读(sentences 各字段详解)
免费中文分词文本处理中文NLPAPI教程ShowAPI # 免费中文分词(文本处理):依存句法分析返回结构解读(sentences 各字段详解)
> 接口:依存句法分析(2663-8)|是否免费:是(含档位限制)|返回格式:JSON|适用人群:中高级开发者、语言学研究|阅读时间:约 8 分钟
## 核心要点
- 依存句法分析(2663-8)返回 `sentences` 数组,每项是一个依存结点,含 `id` / `lemma` / `cpos` / `head_word_id` / `deprel` / `pos`。
- `head_word_id` 指向父结点 id(根结点为 0),`deprel` 表示它与父结点的依存关系——二者是还原句法树的关键。
- `cpos` 是粗粒度词性、`pos` 是细粒度词性,`lemma` 是原词/词干。
## Why
「谁修饰谁」是理解句子结构的核心。依存句法把一个句子拆成「词→词」的有向关系树:主谓、动宾、定中一目了然。它在关系抽取、智能问答、作文批改里都是底层能力。ShowAPI 把这个分析做成了免费接口,返回的是结构化的依存结点,你拿回去自己就能画树、做规则。
## What
**接口速览**
| 项 | 值 |
|------|------|
| 接口地址 | `https://route.showapi.com/2663-8` |
| 必填参数 | `text` |
| 返回 | `sentences`: [{id, lemma, cpos, head_word_id, deprel, pos}] |
**字段含义**
| 字段 | 含义 |
|------|------|
| `id` | 结点 id(本句内唯一) |
| `lemma` | 词语的原词/词干 |
| `cpos` | 词性(粗粒度) |
| `pos` | 词性(细粒度) |
| `head_word_id` | 头结点(父结点)id;根结点默认为 0 |
| `deprel` | 本词与头结点的依存关系标签 |
## How
拿到依存结点后,用 `head_word_id` 还原句法树:
```python
import requests
resp = requests.post(
"https://route.showapi.com/2663-8",
params={"appKey": "YOUR_APPKEY"},
data={"text": "小明吃苹果"},
timeout=10,
).json()
body = resp["showapi_res_body"]
if body.get("ret_code") != 0:
raise RuntimeError(body.get("remark"))
nodes = {n["id"]: n for n in body.get("sentences", [])}
for nid, n in nodes.items():
head = nodes.get(n["head_word_id"])
head_word = head["lemma"] if head else "ROOT"
print(f"{n['lemma']} --{n['deprel']}--> {head_word}")
```
**cURL**
```bash
curl -X POST "https://route.showapi.com/2663-8?appKey=YOUR_APPKEY" \
-H "content-type: application/x-www-form-urlencoded" \
--data-urlencode "text=小明吃苹果"
```
## 返回示例与解析
```json
{
"showapi_res_body": {
"ret_code": 0,
"remark": "成功",
"sentences": [
{ "id": "1", "lemma": "小明", "cpos": "n", "head_word_id": "2", "deprel": "nsubj", "pos": "nh" },
{ "id": "2", "lemma": "吃", "cpos": "v", "head_word_id": "0", "deprel": "root", "pos": "v" },
{ "id": "3", "lemma": "苹果", "cpos": "n", "head_word_id": "2", "deprel": "dobj", "pos": "n" }
]
}
}
```
- `吃` 的 `head_word_id` 为 `0`,是整句的根(ROOT)。
- `小明` 通过 `nsubj`(名词主语)依附于 `吃`;`苹果` 通过 `dobj`(直接宾语)依附于 `吃`。
- `deprel` 标签体系以实际返回为准(常见如 nsubj/dobj/root 等)。
## 进阶 / 边界
- **画树**:以 `head_word_id==0` 的结点为根,递归把每个结点挂到其父结点下,即可得到完整依存树。
- **词性粒度**:`cpos` 粗、`pos` 细,做过滤时按需选用。
- **免费档限制**:默认档位下 `sentences` 可能为空,见 [免费档位与调用策略](https://www.showapi.com/guides/cnseg-free-tier-2663)。
## FAQ
**Q1:怎么找到句子的根?**
找 `head_word_id` 为 `0` 的结点,它就是依存树的根。
**Q2:deprel 标签是什么意思?**
表示本词与父结点的语法关系(如 nsubj 主语、dobj 宾语),具体标签以实际返回为准。
**Q3:cpos 和 pos 有什么区别?**
`cpos` 是粗粒度词性,`pos` 是细粒度(如 `n` vs `nh`),按需使用。
**Q4:多句话怎么处理?**
按接口返回的分句结构处理;具体是否自动分句以实际返回为准。
**Q5:免费档返回空 sentences 是失败吗?**
多为档位限制,先核对额度。
## 相关能力 / 下一步阅读
- [免费中文分词(文本处理):语义距离接入点边界](https://www.showapi.com/guides/cnseg-distance-2663)
- [免费中文分词(文本处理):返回结构与公共字段全解](https://www.showapi.com/guides/cnseg-response-2663)
- [免费中文分词(文本处理):5 分钟快速接入](https://www.showapi.com/guides/cnseg-quickstart-2663)
> 本系列共 14 篇:查看[免费中文分词(文本处理)API 指南总目录](https://www.showapi.com/guides/cnseg-guides-2663)