方寸 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_meter60 次/分钟
/api/free_rhyme60 次/分钟
/api/dictionary/search120 次/分钟
/api/dictionary/allusion120 次/分钟
其他60 次/分钟

超限返回 429 Too Many Requests

错误处理

错误统一返回 JSON:

{ "error": "错误描述" }
状态码含义
400参数校验失败(无效枚举值、参数过长等)
401缺失或无效 API Key
404资源不存在(如韵部名不存在)
429请求频率超限

参数约束

约束说明
参数长度所有查询参数上限 2000 字符
请求体上限 50 KB
genreShi(诗)或 Ci(词)
bookPingshuiyun(平水韵)、Cilinzhengyun(词林正韵)或 Zhonghua_Tongyun(中华通韵)
modeheadtailpair
toneP(平)或 Z(仄)

校验格律

检测诗词的平仄和押韵是否符合规则。这是核心接口。

POST /api/validate_meter

请求体 (JSON)

字段类型说明
poem_textstring必填诗词文本(标点、空格自动忽略,只提取汉字)
genrestring可选ShiCi,默认 Shi
rhyme_book_namestring可选韵书名,默认 Pingshuiyun
rule_namestring可选指定规则名(如 五绝仄起),不传则自动匹配最接近的
ensure_longpubool可选仅匹配龙谱规则,默认 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[]分段显示数据。toneP=平、Z=仄、A=中(可平可仄)

查字

查询单个汉字的声调和所属韵部。支持繁体自动转简体。

GET /api/char/lookup
参数类型说明
charstring必填单个汉字
bookstring可选韵书名。不传则返回所有韵书

示例 — 指定韵书

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
参数类型说明
bookstring可选韵书名,默认 Pingshuiyun
categorystring必填韵部名,如 一东
includestring可选包含关联韵部,逗号分隔(如 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
参数类型说明
bookstring可选韵书名,默认 Pingshuiyun
tonestring可选按声调过滤: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
参数类型说明
genrestring可选ShiCi,默认 Ci
searchstring可选按关键词搜索规则名或词牌名

示例 — 搜索五绝

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
参数类型说明
termstring必填查询字(支持繁体)
modestring可选head(以此字开头)、tail(以此字结尾)、pair(对仗字)。默认 head
lengthstring可选词语长度(23...),all 返回所有长度。默认 2
tonestring可选末字声调过滤: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
参数类型说明
termstring必填1-4 个汉字(支持繁体)
limitint可选最多返回条数,默认 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[]同源典形词列表(含 wd
examples[]包含该典故的古诗例句
rc同源典形词数量(related count)
排序策略:精确匹配优先 → 匹配位置靠前 → 短词优先(2-4 字适合入诗)→ 同源越多越经典。
单字搜索走倒排索引,多字搜索对各字的 ID 集合取交集后过滤子串匹配。

自由诗韵脚检测

对自由诗(古体诗/新诗/歌词等不限格律的诗体)进行轻量韵脚检测,提取韵脚候选字并按共同韵部前向分组。

POST /api/free_rhyme
参数类型说明
linesstring[]必填诗行数组,每行一个字符串
rhyme_book_namestring可选韵书名,默认 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 成员的组。