# opencc SQL 参考手册

> opencc v0.1.0（内置 OpenCC 1.4.2）完整 SQL 接口速查：全部 16 套内置配置、转换与诊断函数、系统视图及 opencc.preload 参数。

---

LLMS 索引： [llms.txt](/zh/llms.txt)

---

所有函数与视图均位于固定模式 `opencc` 下。转换、检测与诊断函数在同一版本内为 `IMMUTABLE` 且 `PARALLEL SAFE`，要求数据库编码为 `UTF8`。

## 内置配置一览 {#configs}

| 配置名 | 源 → 目标 | 含地区词汇 | 成熟度 | 快捷函数 |
| --- | --- | --- | --- | --- |
| `s2t` / `t2s` | `zh-Hans` ↔ `zh-Hant` | 否 | `stable` | `opencc.s2t()` / `opencc.t2s()` |
| `s2tw` / `tw2s` | `zh-Hans` ↔ `zh-TW` | 否 | `stable` | `opencc.s2tw()` / `opencc.tw2s()` |
| `s2twp` / `tw2sp` | `zh-Hans` ↔ `zh-TW` | 是 | `stable` | `opencc.s2twp()` / `opencc.tw2sp()` |
| `s2hk` / `hk2s` | `zh-Hans` ↔ `zh-HK` | 否 | `stable` | `opencc.s2hk()` / `opencc.hk2s()` |
| `s2hkp` / `hk2sp` | `zh-Hans` ↔ `zh-HK` | 是 | `developing` | `opencc.s2hkp()` / `opencc.hk2sp()` |
| `t2tw` / `tw2t` | `zh-Hant` ↔ `zh-TW` | 否 | `stable` | `opencc.t2tw()` / `opencc.tw2t()` |
| `t2hk` / `hk2t` | `zh-Hant` ↔ `zh-HK` | 否 | `stable` | `opencc.t2hk()` / `opencc.hk2t()` |
| `t2jp` / `jp2t` | `zh-Hant` ↔ `ja` | 否 | `experimental` | `opencc.t2jp()` / `opencc.jp2t()` |

## 转换函数 {#conversion}

| 名称 | 返回类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `opencc.convert(input text, config text)` | `text` | — | 使用上述 16 套内置配置之一转换文本。 |
| `opencc.localize(input text, target text, source text)` | `text` | `source = 'auto'` | 在 `zh-Hans`、`zh-Hant`、`zh-CN`、`zh-TW`、`zh-HK` 与 `ja` 之间路由转换。需做地区词汇转换时显式指定 `source`。 |
| `opencc.<配置名>(input text)` | `text` | — | 16 套配置的快捷封装（`s2t`、`t2s`、`s2tw`、`tw2s`、`s2twp`、`tw2sp`、`s2hk`、`hk2s`、`s2hkp`、`hk2sp`、`t2tw`、`tw2t`、`t2hk`、`hk2t`、`t2jp`、`jp2t`），等价于 `opencc.convert(input, '<配置名>')`。 |
{.fields meta="type default"}

## 字形检测与歧义诊断 {#inspection}

| 名称 | 返回类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `opencc.detect(input text)` | `text` | — | 启发式判断文本字形：`none`、`neutral`、`simplified`、`traditional`、`mixed` 或 `japanese`。 |
| `opencc.script_stats(input text)` | `record` | — | 返回 `(han integer, simplified integer, traditional integer, kana integer)` 各类字符数。 |
| `opencc.explain(input text, config text)` | `table` | — | 返回 `(seq integer, source text, steps text[], output text)`，展示每段输入在各转换阶段的中间输出。 |
| `opencc.ambiguities(input text, config text)` | `table` | — | 返回 `(pos integer, len integer, source text, output text, candidates text[])`，定位一简对多繁歧义片段。 |
| `opencc.candidates(word text, config text)` | `text[]` | — | 返回词条在指定配置下的全部候选结果（默认项排首位；词典未收录时返回 `{}`）。 |
{.fields meta="type default"}

## 系统视图与运维函数 {#catalog}

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `opencc.configs` | 视图 | — | 列出全部 16 套内置配置（`name`、`source`、`target`、`phrases`、`maturity`、`description`、`loaded`），`loaded` 反映当前后端加载状态。 |
| `opencc.routes` | 视图 | — | 列出 `localize()` 支持的全部 36 条地区路由（`source`、`target`、`chain`、`maturity`）。 |
| `opencc.version()` | `record` | — | `STABLE`。返回 `(extension text, opencc text, dictionary text)`，含内置词典 SHA-256 摘要。 |
| `opencc.dependents()` | `table` | — | `STABLE`。返回 `(kind text, object text, uses text, action text)`，列出直接依赖的物化对象及重建 SQL。 |
{.fields meta="type default"}

## 配置参数（GUC） {#configuration}

| 参数名 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `opencc.preload` | string | `''` | 逗号分隔的预加载配置名列表（或 `'all'`）。与 `shared_preload_libraries = 'opencc'` 配合并在重启后生效，使各后端共享只读词典内存页。 |
{.fields meta="type default"}

```sql
SELECT name, loaded FROM opencc.configs ORDER BY name;
```

---

反链：

- [快速上手](/zh/docs/opencc/getting-started/)
- [opencc](/zh/spec/opencc/)
