方寸 API 文档
格律诗词校验与辅助创作接口。
认证
所有 /api/* 请求需通过 X-API-Key Header 传递 API Key:
X-API-Key: fc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
获取 Key
发送邮件至 guoxue_sjtu@163.com,请包含以下信息:
收件人: guoxue_sjtu@163.com
主 题: 方寸 API Key 申请
您好,
我希望申请方寸 API Key,信息如下:
姓名/团队:
用途说明:
预计调用量:
谢谢!
审核通过后将回复包含 API Key 的邮件。
限流
| 端点 | 限制 |
|---|---|
| /api/validate_meter | 60 次/分钟 |
| /api/free_rhyme | 60 次/分钟 |
| /api/dictionary/search | 120 次/分钟 |
| /api/dictionary/allusion | 120 次/分钟 |
| 其他 | 60 次/分钟 |
超限返回 429 Too Many Requests。
错误处理
错误统一返回 JSON:
{ "error": "错误描述" }
| 状态码 | 含义 |
|---|---|
| 400 | 参数校验失败(无效枚举值、参数过长等) |
| 401 | 缺失或无效 API Key |
| 404 | 资源不存在(如韵部名不存在) |
| 429 | 请求频率超限 |
参数约束
| 约束 | 说明 |
|---|---|
| 参数长度 | 所有查询参数上限 2000 字符 |
| 请求体 | 上限 50 KB |
genre | Shi(诗)或 Ci(词) |
book | Pingshuiyun(平水韵)、Cilinzhengyun(词林正韵)或 Zhonghua_Tongyun(中华通韵) |
mode | head、tail、pair |
tone | P(平)或 Z(仄) |
校验格律
检测诗词的平仄和押韵是否符合规则。这是核心接口。
POST
/api/validate_meter
请求体 (JSON)
| 字段 | 类型 | 说明 | |
|---|---|---|---|
| poem_text | string | 必填 | 诗词文本(标点、空格自动忽略,只提取汉字) |
| genre | string | 可选 | Shi 或 Ci,默认 Shi |
| rhyme_book_name | string | 可选 | 韵书名,默认 Pingshuiyun |
| rule_name | string | 可选 | 指定规则名(如 五绝仄起),不传则自动匹配最接近的 |
| ensure_longpu | bool | 可选 | 仅匹配龙谱规则,默认 false |
请求示例
curl -X POST https://write.sjtuguoxue.space/api/validate_meter \
-H "X-API-Key: fc_xxx" \
-H "Content-Type: application/json" \
-d '{
"poem_text": "白日依山尽,黄河入海流。欲穷千里目,更上一层楼。",
"genre": "Shi",
"rhyme_book_name": "Pingshuiyun"
}'
响应示例 — 格律通过
{
"is_valid": true,
"closest_rule": {
"name": "五绝仄起",
"genre": "Shi",
"cipai": "Wujue",
"char_count": 20
},
"errors": [],
"warnings": [],
"rhyme_name": "十一尤",
"rhyme_positions": [9, 19],
"rhyme_chars": ["流", "楼"],
"rhyme_groups": [
{ "positions": [9, 19], "type": "same" }
],
"rhyme_relations": [],
"display_segments": [
{
"start_index": 0,
"text_chars": ["白", "日", "依", "山", "尽", "黄", "河", "入", "海", "流"],
"rule_items": [
{ "tone": "A" }, { "tone": "Z" }, { "tone": "A" },
{ "tone": "P" }, { "tone": "Z" }, { "tone": "P" },
{ "tone": "P" }, { "tone": "A" }, { "tone": "Z" }, { "tone": "P" }
]
}
]
}
响应示例 — 有错误
{
"is_valid": false,
"closest_rule": {
"name": "五绝仄起首句入韵",
"char_count": 20
},
"errors": [
{
"position": 1,
"character": "风",
"error_type": "Tone",
"message": "应为仄, 实为平"
},
{
"position": 6,
"character": "雨",
"error_type": "Tone",
"message": "应为平, 实为仄"
}
],
"rhyme_name": "五微",
"rhyme_chars": ["开", "飞", "归"]
}
响应字段说明
| 字段 | 说明 |
|---|---|
| is_valid | 是否完全通过校验 |
| closest_rule | 匹配到的最接近规则(名称、体裁、词牌、字数) |
| errors[] | 错误列表,每项含位置 (0-indexed)、字符、错误类型 (Tone/Rhyme)、描述 |
| warnings[] | 警告列表(如重复用字) |
| rhyme_name | 推断的韵部名(一韵到底时) |
| rhyme_positions | 押韵位置(0-indexed) |
| rhyme_chars | 押韵字列表 |
| display_segments[] | 分段显示数据。tone:P=平、Z=仄、A=中(可平可仄) |
查字
查询单个汉字的声调和所属韵部。支持繁体自动转简体。
GET
/api/char/lookup
| 参数 | 类型 | 说明 | |
|---|---|---|---|
| char | string | 必填 | 单个汉字 |
| book | string | 可选 | 韵书名。不传则返回所有韵书 |
示例 — 指定韵书
curl "https://write.sjtuguoxue.space/api/char/lookup?char=花&book=Pingshuiyun" \
-H "X-API-Key: fc_xxx"
{
"char": "花",
"tones": ["平", "上"],
"rhyme_categories": [
{ "name": "六麻", "tone_type": "P" }
]
}
示例 — 不指定韵书(返回所有)
{
"char": "花",
"tones": ["平", "上"],
"rhyme_categories": {
"Pingshuiyun": ["六麻"],
"Cilinzhengyun": ["第3部_仄", "第10部_平"]
}
}
tones 为原始声调(平、上、去、入),rhyme_categories 为韵书中的韵部归属。查韵部字表
查看某个韵部包含哪些字。
GET
/api/rhyme/lookup
| 参数 | 类型 | 说明 | |
|---|---|---|---|
| book | string | 可选 | 韵书名,默认 Pingshuiyun |
| category | string | 必填 | 韵部名,如 一东 |
| include | string | 可选 | 包含关联韵部,逗号分隔(如 neighbor) |
示例
curl "https://write.sjtuguoxue.space/api/rhyme/lookup?book=Pingshuiyun&category=一东" \
-H "X-API-Key: fc_xxx"
{
"category_name": "一东",
"tone_type": "P",
"total": 392,
"characters": ["中", "总", "种", "同", "东", "红", "风", ...],
"relations": {
"neighbor": ["二冬"]
}
}
字表按词频降序排列(常用字在前)。
传入
relations 标注邻韵等关系。传入
include=neighbor 时,响应变为 { "primary": {...}, "related": [...] },同时返回邻韵字表。韵部列表
列出韵书中的所有韵部概览。
GET
/api/rhyme/list
| 参数 | 类型 | 说明 | |
|---|---|---|---|
| book | string | 可选 | 韵书名,默认 Pingshuiyun |
| tone | string | 可选 | 按声调过滤:P(平声韵)或 Z(仄声韵) |
示例
curl "https://write.sjtuguoxue.space/api/rhyme/list?book=Pingshuiyun&tone=P" \
-H "X-API-Key: fc_xxx"
{
"book": "Pingshuiyun",
"categories": [
{
"name": "一东",
"tone_type": "P",
"char_count": 392,
"preview": "中总种同衕东梦红风冲"
},
{
"name": "二冬",
"tone_type": "P",
"char_count": 262,
"preview": "从共重龙供冲封鍾鐘钟"
}
]
}
preview 为该韵部最高频的前 10 个字。格律规则列表
列出可用的诗词格律规则或词牌。
GET
/api/rules/list
| 参数 | 类型 | 说明 | |
|---|---|---|---|
| genre | string | 可选 | Shi 或 Ci,默认 Ci |
| search | string | 可选 | 按关键词搜索规则名或词牌名 |
示例 — 搜索五绝
curl "https://write.sjtuguoxue.space/api/rules/list?genre=Shi&search=五绝" \
-H "X-API-Key: fc_xxx"
[
{ "name": "五绝仄起首句入韵", "char_count": 20 },
{ "name": "五绝仄起", "char_count": 20 },
{ "name": "五绝平起", "char_count": 20 },
{ "name": "五绝平起首句入韵", "char_count": 20 }
]
诗 (Shi) 约 16 条规则,词 (Ci) 约 2500+ 条词牌变体。
词语联想
按首字、尾字查诗词用语,或查对仗字。数据来源于古诗词语料统计。
GET
/api/dictionary/search
| 参数 | 类型 | 说明 | |
|---|---|---|---|
| term | string | 必填 | 查询字(支持繁体) |
| mode | string | 可选 | head(以此字开头)、tail(以此字结尾)、pair(对仗字)。默认 head |
| length | string | 可选 | 词语长度(2、3...),all 返回所有长度。默认 2 |
| tone | string | 可选 | 末字声调过滤:P(平)或 Z(仄)。默认不过滤 |
示例 — 首字联想
curl "https://write.sjtuguoxue.space/api/dictionary/search?term=明&mode=head&length=2" \
-H "X-API-Key: fc_xxx"
[
["明月", 2298],
["明朝", 1683],
["明日", 1398],
["明年", 797],
["明时", 296]
]
返回
[词语, 频次] 数组,按频次降序排列。示例 — 对仗字
curl "https://write.sjtuguoxue.space/api/dictionary/search?term=月&mode=pair" \
-H "X-API-Key: fc_xxx"
[
["风", 4450],
["云", 3575],
["花", 1386],
["山", 1340],
["天", 1142]
]
对仗模式返回与查询字在古诗中常对仗的字及频次。
典故检索
检索包含指定字/词的典形词(文学典故),返回释义、典源、同源典形词和古诗例句。数据约 13,000 条。
GET
/api/dictionary/allusion
| 参数 | 类型 | 说明 | |
|---|---|---|---|
| term | string | 必填 | 1-4 个汉字(支持繁体) |
| limit | int | 可选 | 最多返回条数,默认 60,上限 200 |
示例
curl "https://write.sjtuguoxue.space/api/dictionary/allusion?term=月&limit=5" \
-H "X-API-Key: fc_xxx"
[
{
"id": 42,
"w": "月桂",
"d": "借指月中桂树,喻科举及第",
"src": "月中桂",
"src_text": "《酉阳杂俎》载……",
"related": [
{ "w": "折桂", "d": "比喻科举登第" },
{ "w": "蟾宫", "d": "借指月宫" }
],
"examples": ["昔年曾向嫦娥妒,今日相逢恐未真"],
"rc": 5
}
]
响应字段说明
| 字段 | 说明 |
|---|---|
| w | 典形词 |
| d | 释义 |
| src | 典源词(典故名) |
| src_text | 典源内容(截断 ≤300 字) |
| related[] | 同源典形词列表(含 w 和 d) |
| examples[] | 包含该典故的古诗例句 |
| rc | 同源典形词数量(related count) |
排序策略:精确匹配优先 → 匹配位置靠前 → 短词优先(2-4 字适合入诗)→ 同源越多越经典。
单字搜索走倒排索引,多字搜索对各字的 ID 集合取交集后过滤子串匹配。
单字搜索走倒排索引,多字搜索对各字的 ID 集合取交集后过滤子串匹配。
自由诗韵脚检测
对自由诗(古体诗/新诗/歌词等不限格律的诗体)进行轻量韵脚检测,提取韵脚候选字并按共同韵部前向分组。
POST
/api/free_rhyme
| 参数 | 类型 | 说明 | |
|---|---|---|---|
| lines | string[] | 必填 | 诗行数组,每行一个字符串 |
| rhyme_book_name | string | 可选 | 韵书名,默认 Zhonghua_Tongyun,可选 Pingshuiyun |
示例
curl -X POST https://write.sjtuguoxue.space/api/free_rhyme \
-H "Content-Type: application/json" \
-H "X-API-Key: fc_xxx" \
-d '{"lines": ["春眠不觉晓,", "处处闻啼鸟。", "夜来风雨声,", "花落知多少。"]}'
{
"candidates": [
{ "line": 0, "pos": 4, "char": "晓", "categories": ["二巧"] },
{ "line": 1, "pos": 4, "char": "鸟", "categories": ["二巧"] },
{ "line": 2, "pos": 4, "char": "声", "categories": ["十一庚"] },
{ "line": 3, "pos": 4, "char": "少", "categories": ["二巧"] }
],
"groups": [
{ "positions": [
{ "line": 0, "pos": 4 },
{ "line": 1, "pos": 4 },
{ "line": 3, "pos": 4 }
] }
]
}
响应字段说明
| 字段 | 说明 |
|---|---|
| candidates[] | 所有韵脚候选字(标点前或行末的 CJK 字符),含所属韵部 |
| groups[] | 韵脚分组(≥2 个候选字共享韵部),用于同色着色 |
| positions[].line | 所在行号(0-based) |
| positions[].pos | 行内字符位置(0-based) |
分组算法:前向贪心 — 取首个未分组字的韵部为锚集,向后扫描共享韵部的字拉入组,锚集不扩展。仅返回 ≥2 成员的组。