bazi.knowledge¶
可编辑的解释知识。
- bazi.knowledge.Applicability[源代码]¶
知识整理状态,不是命盘判别结果。 unconditional:无命盘前提;described:已记录前提文字,术语可能尚无计算定义; unresolved:前提仍待重建。
Literal['unconditional', 'described', 'unresolved'] 的别名
- bazi.knowledge.SourceState[源代码]¶
条目的来源状态,与前提整理状态和默认展示分别记录。 witnessed:记录为已核定的原文出处;editorial:原作说明; unverified:来源尚未核定,条目不绑定来源见证;repository_attributed:仓内规则实现的归属。 这些标记不表示命盘前提已满足,也不认证现实事件。
Literal['witnessed', 'editorial', 'unverified', 'repository_attributed'] 的别名
- bazi.knowledge.object_id(subject)[源代码]¶
返回天干或十神在知识库中的稳定对象标识;中文别名与英文成员共用标识。
Args:
subject: (Tiangan | Shishen) 讨论对象。
Return: (str) 对象标识;自定义库是否注册该对象由查询时校验。
- class bazi.knowledge.KnowledgeObject(object_id, kind, name)[源代码]¶
具名讨论对象。
- 参数:
object_id (str)
kind (str)
name (str)
- kind: str¶
- name: str¶
- object_id: str¶
- class bazi.knowledge.KnowledgeRole(name, object_id)[源代码]¶
绑定讨论对象的具名角色。
- 参数:
name (str)
object_id (str)
- name: str¶
- object_id: str¶
- class bazi.knowledge.KnowledgeRelation(from_role, to_role, text)[源代码]¶
角色之间有方向的文字陈述,不表示已计算的命盘关系。
- 参数:
from_role (str)
to_role (str)
text (str)
- from_role: str¶
- text: str¶
- to_role: str¶
- class bazi.knowledge.KnowledgeContext(context_id, label, time_scope, limits)[源代码]¶
带时间语境与限度的具名查阅情境。
- 参数:
context_id (str)
label (str)
time_scope (str)
limits (tuple[str, ...])
- context_id: str¶
- label: str¶
- limits: tuple[str, ...]¶
- time_scope: str¶
- class bazi.knowledge.KnowledgeSource(source_id, work, attribution, edition, locator, url, text_layer, lineage, excerpt, supports, limitations, state)[源代码]¶
带署名与证据边界的来源见证。
新来源的 source_id 以 lineage 为前缀;已有 ID 不改名,归组以 lineage 为准。
- 参数:
source_id (str)
work (str)
attribution (str)
edition (str)
locator (str)
url (str)
text_layer (str)
lineage (str)
excerpt (str)
supports (str)
limitations (str)
state (Literal['witnessed', 'editorial', 'unverified', 'repository_attributed'])
- attribution: str¶
- edition: str¶
- excerpt: str¶
- limitations: str¶
- lineage: str¶
- locator: str¶
- source_id: str¶
- state: Literal['witnessed', 'editorial', 'unverified', 'repository_attributed']¶
- supports: str¶
- text_layer: str¶
- url: str¶
- work: str¶
- class bazi.knowledge.LegacyDescription(object_id, field, ordinal, conditions, field_premise)[源代码]¶
旧描述的确切字段、顺序及条件元数据。
复核旧位置时按 object_id、field 选择,再按 ordinal 排序,不与知识库的语料顺序混同。 chart_context_required 记录旧前提;in_good_status / in_bad_status 是旧位置,不是现代知识分类。 conditions 为空不表示前提已解决。
- 参数:
object_id (str)
field (str)
ordinal (int)
conditions (tuple[str, ...])
field_premise (str)
- conditions: tuple[str, ...]¶
- field: str¶
- field_premise: str¶
- object_id: str¶
- ordinal: int¶
- class bazi.knowledge.KnowledgeEntry(claim_id, text, roles, relations, topics, applicability, premise, contexts, sources, source_state, attribution, viewpoint, exceptions, limits, show_by_default, legacy)[源代码]¶
分别记录来源、前提及默认展示选择的解释条目。
show_by_default 只控制默认查询是否返回,不认证知识正确或适用于当前命盘。
- 参数:
claim_id (str)
text (str)
roles (tuple[KnowledgeRole, ...])
relations (tuple[KnowledgeRelation, ...])
topics (tuple[str, ...])
applicability (Literal['unconditional', 'described', 'unresolved'])
premise (str)
contexts (tuple[str, ...])
sources (tuple[str, ...])
source_state (Literal['witnessed', 'editorial', 'unverified', 'repository_attributed'])
attribution (str)
viewpoint (str)
exceptions (tuple[str, ...])
limits (tuple[str, ...])
show_by_default (bool)
legacy (LegacyDescription | None)
- applicability: Literal['unconditional', 'described', 'unresolved']¶
- attribution: str¶
- claim_id: str¶
- contexts: tuple[str, ...]¶
- exceptions: tuple[str, ...]¶
- legacy: LegacyDescription | None¶
- limits: tuple[str, ...]¶
- premise: str¶
- relations: tuple[KnowledgeRelation, ...]¶
- roles: tuple[KnowledgeRole, ...]¶
- show_by_default: bool¶
- source_state: Literal['witnessed', 'editorial', 'unverified', 'repository_attributed']¶
- sources: tuple[str, ...]¶
- text: str¶
- topics: tuple[str, ...]¶
- viewpoint: str¶
- class bazi.knowledge.KnowledgeBase(objects, contexts, sources, entries)[源代码]¶
校验、索引、查询并渲染不可变解释条目。
Note:
JSON 为编辑来源;加载时校验内容并建立索引。
值比较与 hash 包含四份注册表的内容及顺序,不依赖实例身份。
sources 包含完整来源注册表,包括未被条目引用的记录;直接读取 sources[source_id] 查来源,query(source_id=...) 查关联条目。
仅供参考条目不判断命盘适用性。条目的 sources 为空或署名注明出处未核, 均不表示无来源或断言为假;仓内署名也不等于已经核定的古籍见证。
- 参数:
objects (tuple[KnowledgeObject, ...])
contexts (tuple[KnowledgeContext, ...])
sources (tuple[KnowledgeSource, ...])
entries (tuple[KnowledgeEntry, ...])
- property contexts: frozendict[str, KnowledgeContext][源代码]¶
- property entries: frozendict[str, KnowledgeEntry][源代码]¶
- filter_values(dimension)[源代码]¶
返回 query 接受的合法过滤值全集,按 Unicode 字符串序排列。
Note:
包含仅供参考条目独有的值,以及未被条目引用的对象、情境、来源与情境时间范围。
applicability 来自类型定义,空库也保留合法域;合法值不保证默认查询有命中。
Args:
dimension: (str) object_id、context_id、source_id、topic、viewpoint、applicability 或 time_scope。
Return: (tuple[str, ...]) 不重复的合法值;未知维度抛 ValueError,错型抛 TypeError。
- 参数:
dimension (str)
- 返回类型:
tuple[str, ...]
- find_objects(name)[源代码]¶
按名称精确查找对象,重名全部返回,保持注册顺序;无匹配返回空元组。
Args:
name: (str) 非空名称,不做大小写转换、分词或近似匹配,有效名称两侧空白不剥除。
Return: (tuple[KnowledgeObject, ...]) 本知识库中同名的不可变对象;空或全空白名称抛 ValueError,错型抛 TypeError。
- 参数:
name (str)
- 返回类型:
tuple[KnowledgeObject, ...]
- classmethod load(path=None)[源代码]¶
读取指定编辑来源;未给路径时读取 DEFAULT_SOURCE_PATH 的随库语料。
Note:
每次调用重新读取并校验文件,不缓存共享实例。已加载对象保留当时的内容; 来源修改不影响已有对象,须再次加载才能取得更新。
Args:
path: (Path | None) 编辑来源路径;None 使用随库来源。
Return: (KnowledgeBase) 校验后的不可变知识库对象。
- 参数:
path (Path | None)
- 返回类型:
- property objects: frozendict[str, KnowledgeObject][源代码]¶
- query(*, object_id=None, context_id=None, topic=None, source_id=None, viewpoint=None, applicability=None, time_scope=None, include_reference_only=False)[源代码]¶
按查阅条件取交集,返回保留语料顺序的不可变条目。
Note:
默认只选 show_by_default=True;include_reference_only=True 同时选仅供参考条目。
对象查询包含所有绑定角色;未知过滤值抛 ValueError,合法但无匹配时返回空元组。
context_id 是人工选择的查阅情境,不判断命盘是否满足;多个情境标签不隐含 AND/OR。
Return: (tuple[KnowledgeEntry, ...]) 命中的不可变条目。
- 参数:
object_id (str | None)
context_id (str | None)
topic (str | None)
source_id (str | None)
viewpoint (str | None)
applicability (Literal['unconditional', 'described', 'unresolved'] | None)
time_scope (str | None)
include_reference_only (bool)
- 返回类型:
tuple[KnowledgeEntry, ...]
- render(entry, *, show_sources=True, manual_context=False, premise_verdict=None)[源代码]¶
展示原文、前提与限度,可附调用方提供的前提判别。
Note:
本方法不选择条目、不求值前提,也不改变默认展示选择。
Args:
entry: (KnowledgeEntry) 本库条目。
show_sources: (bool) 是否展示来源详情;前提与限度始终保留。
manual_context: (bool) 标注人工给定情境,不表示前提满足。
premise_verdict: (str | None) 调用方的展示文字,须含非空白字符;给定时优先于人工情境标注。
Return: (str) 展示文字。
- 参数:
entry (KnowledgeEntry)
show_sources (bool)
manual_context (bool)
premise_verdict (str | None)
- 返回类型:
str
- render_subject(object_id, *, include_reference_only=False, show_sources=False)[源代码]¶
按主体展示带主题前缀的知识文字,保留语料顺序、前提及限度。
Note:
有旧位置元数据的条目只在其旧主体下展示;其余条目包含所有绑定角色。 query 的对象过滤始终包含全部角色,二者范围不同。
旧字段位置与 ordinal 仍在 entry.legacy 中,不决定此处的语料顺序。
Args:
object_id: (str) 注册对象标识。
include_reference_only: (bool) 是否包含仅供参考条目,不求值其前提。
show_sources: (bool) 是否展示来源详情。
Return: (str) 展示文字;合法对象没有所选主体条目时返回空串。
- 参数:
object_id (str)
include_reference_only (bool)
show_sources (bool)
- 返回类型:
str
- search(keyword, *, object_id=None, context_id=None, topic=None, source_id=None, viewpoint=None, applicability=None, time_scope=None, include_reference_only=False)[源代码]¶
先按 query 过滤,再对条目文字及关联对象名称作 casefold 字面子串检索。
Note:
检索 text、premise、attribution、topics、viewpoint、exceptions、limits 及角色对象的 name。 不检索来源全文、情境标签或对象 ID,不分词、不解释正则、不计算命盘适用性。
七维过滤取交集,校验及参考条目政策与 query 相同,结果保持语料顺序。
Args:
keyword: (str) 非空关键词,空或全空白抛 ValueError,错型抛 TypeError;不剥除有效词两侧空白。
其余参数与 query 相同。
Return: (tuple[KnowledgeEntry, ...]) 命中的不可变条目。
- 参数:
keyword (str)
object_id (str | None)
context_id (str | None)
topic (str | None)
source_id (str | None)
viewpoint (str | None)
applicability (Literal['unconditional', 'described', 'unresolved'] | None)
time_scope (str | None)
include_reference_only (bool)
- 返回类型:
tuple[KnowledgeEntry, ...]
- property sources: frozendict[str, KnowledgeSource][源代码]¶