pattern_schema.json 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. {
  2. "$schema": "http://json-schema.org/draft-07/schema#",
  3. "$id": "pattern_schema.json",
  4. "title": "BiddingKG 正则规则 YAML Schema",
  5. "description": "定义 dl/rules/patterns/*.yaml 的合法结构",
  6. "type": "object",
  7. "definitions": {
  8. "condition": {
  9. "type": "object",
  10. "description": "结构化条件(Phase C 机器可读 guard DSL):guard 列表内 AND 组合,guard_not 列表内 OR 组合(任一命中则跳过规则),or_trigger 列表内 OR 组合(任一命中则规则触发,无需 pattern 匹配)",
  11. "required": ["type"],
  12. "properties": {
  13. "type": {
  14. "type": "string",
  15. "description": "条件类型:regex=切片上 re.search;regex_not=切片上 re.search 不命中;match=切片上 re.match;is_agency/not_is_agency=实体名是否命中代理机构词典;value_lt=values[index] < threshold;notes_in=entity.notes ∈ values;any/of、all/of=嵌套组合;count_eq=len(re.findall(pattern)) == equals",
  16. "enum": ["regex", "regex_not", "match", "is_agency", "not_is_agency", "value_lt", "notes_in", "any", "all", "count_eq"]
  17. },
  18. "on": {
  19. "type": "string",
  20. "description": "求值目标切片:front=实体前文,behind=实体后文,whole=前文+实体+后文,entity_text=实体文本,title=公告标题,content=正文全文,content_head=正文前 size 字,title_content_head=标题+正文前 size 字",
  21. "enum": ["front", "behind", "whole", "entity_text", "title", "content", "content_head", "title_content_head"]
  22. },
  23. "pattern": {
  24. "type": "string",
  25. "description": "条件正则(regex/regex_not/match/count_eq 类型必填)"
  26. },
  27. "size": {
  28. "type": "integer",
  29. "description": "切片截断长度(front 取末 size 字,behind/content_head/title_content_head 取前 size 字;缺省不截断)",
  30. "minimum": 1
  31. },
  32. "index": {
  33. "type": "integer",
  34. "description": "values 数组下标(value_lt 用)",
  35. "minimum": 0,
  36. "maximum": 5
  37. },
  38. "threshold": {
  39. "type": "number",
  40. "description": "比较阈值(value_lt 用)",
  41. "minimum": 0,
  42. "maximum": 1
  43. },
  44. "values": {
  45. "type": "array",
  46. "description": "候选值列表(notes_in 用)",
  47. "items": {"type": "string"},
  48. "minItems": 1
  49. },
  50. "equals": {
  51. "type": "integer",
  52. "description": "期望的 findall 计数(count_eq 用)",
  53. "minimum": 0
  54. },
  55. "of": {
  56. "type": "array",
  57. "description": "嵌套子条件列表(any/all 用)",
  58. "items": {"$ref": "#/definitions/condition"},
  59. "minItems": 1
  60. }
  61. },
  62. "additionalProperties": false
  63. },
  64. "set_prob_action": {
  65. "type": "object",
  66. "description": "概率改写动作(Phase C 机器可读):values[index] = value。index 缺省 = set_label(若有)否则当前 label",
  67. "required": ["value"],
  68. "properties": {
  69. "value": {
  70. "type": "number",
  71. "description": "写入的概率值",
  72. "minimum": 0,
  73. "maximum": 1
  74. },
  75. "index": {
  76. "type": "integer",
  77. "description": "values 数组下标;缺省时取 set_label(若规则声明)否则当前 label",
  78. "minimum": 0,
  79. "maximum": 5
  80. }
  81. },
  82. "additionalProperties": false
  83. }
  84. },
  85. "required": ["version", "category"],
  86. "anyOf": [
  87. {"required": ["patterns"]},
  88. {"required": ["keyword_rules"]}
  89. ],
  90. "properties": {
  91. "version": {
  92. "type": "integer",
  93. "description": "规则文件版本号,当前为 1",
  94. "const": 1
  95. },
  96. "category": {
  97. "type": "string",
  98. "description": "规则类别",
  99. "enum": ["PACKAGE", "TIME", "MONEY", "CONTACT", "BIDWAY", "FAILURE", "RANK", "ROLE", "OTHER"]
  100. },
  101. "description": {
  102. "type": "string",
  103. "description": "规则文件描述"
  104. },
  105. "patterns": {
  106. "type": "array",
  107. "description": "正则规则列表",
  108. "items": {
  109. "type": "object",
  110. "required": ["id", "pattern"],
  111. "properties": {
  112. "id": {
  113. "type": "string",
  114. "description": "规则唯一标识,用于 loader.get_pattern(id) 查找",
  115. "pattern": "^[a-z][a-z0-9_]*$"
  116. },
  117. "description": {
  118. "type": "string",
  119. "description": "规则用途说明"
  120. },
  121. "pattern": {
  122. "type": "string",
  123. "description": "正则表达式字符串"
  124. },
  125. "method": {
  126. "type": "string",
  127. "description": "匹配方法:search=搜索, match=头部匹配, fullmatch=全串匹配",
  128. "enum": ["search", "match", "fullmatch"],
  129. "default": "search"
  130. },
  131. "flags": {
  132. "type": "array",
  133. "description": "正则标志",
  134. "items": {
  135. "type": "string",
  136. "enum": ["IGNORECASE", "MULTILINE", "DOTALL", "UNICODE", "VERBOSE"]
  137. },
  138. "uniqueItems": true
  139. },
  140. "named_groups": {
  141. "type": "array",
  142. "description": "命名捕获组列表(文档用途,不参与编译)",
  143. "items": {"type": "string"}
  144. },
  145. "role": {
  146. "type": "string",
  147. "description": "召回/修正的目标角色标签(ROLE 类规则用):tenderee=招标人, agency=代理机构, winTenderer=中标人, secondTenderer=第二候选人, thirdTenderer=第三候选人, other=其他",
  148. "enum": ["tenderee", "agency", "winTenderer", "secondTenderer", "thirdTenderer", "other"]
  149. },
  150. "prob": {
  151. "type": "number",
  152. "description": "命中后赋予的召回概率权重(ROLE 召回规则用,0-1)",
  153. "minimum": 0,
  154. "maximum": 1
  155. },
  156. "applies_to": {
  157. "type": "string",
  158. "description": "规则匹配的上下文切片位置(ROLE 类规则用):front=实体前文, behind=实体后文, whole=前后文拼接, title=公告标题(doc_title 阶段用)",
  159. "enum": ["front", "behind", "whole", "title"]
  160. },
  161. "priority": {
  162. "type": "integer",
  163. "description": "执行优先级(数字越大越先执行,同文件内用于表达原代码的串行顺序;可选)",
  164. "minimum": 0,
  165. "maximum": 999
  166. },
  167. "stage": {
  168. "type": "string",
  169. "description": "执行阶段(Phase C RoleRuleEngine 用):引擎按 stage 分组、按固定阶段序执行,同阶段内首条命中规则生效(对应原 elif 链短路)。角色修正:global_pre/seq_check/l0/l2/l5_win_yes/notify/l1/l34/l5;金额修正:m1/m0/m_bid/doc_title",
  170. "pattern": "^[a-z][a-z0-9_]*$"
  171. },
  172. "window": {
  173. "type": "object",
  174. "description": "applies_to 切片的机器可读截断(front 取末 size 字,behind 取前 size 字),对应原代码 front[-N:]/behind[:N]",
  175. "required": ["size"],
  176. "properties": {
  177. "size": {
  178. "type": "integer",
  179. "description": "截断长度(字符)",
  180. "minimum": 1
  181. }
  182. },
  183. "additionalProperties": false
  184. },
  185. "model_labels": {
  186. "type": "array",
  187. "description": "修正规则适用的模型预测标签列表(label 改写规则用):0=tenderee,1=agency,2=winTenderer,3=secondTenderer,4=thirdTenderer,5=other",
  188. "items": {"type": "integer", "minimum": 0, "maximum": 5}
  189. },
  190. "set_label": {
  191. "type": "integer",
  192. "description": "命中后改写的角色标签(修正规则用)",
  193. "minimum": 0,
  194. "maximum": 5
  195. },
  196. "set_prob": {
  197. "$ref": "#/definitions/set_prob_action"
  198. },
  199. "guard": {
  200. "type": "array",
  201. "description": "附加条件列表(AND 组合,全部满足规则才生效;Phase C 起参与引擎执行)",
  202. "items": {"$ref": "#/definitions/condition"}
  203. },
  204. "guard_not": {
  205. "type": "array",
  206. "description": "排除条件列表(OR 组合,任一命中则规则不生效)",
  207. "items": {"$ref": "#/definitions/condition"}
  208. },
  209. "or_trigger": {
  210. "type": "array",
  211. "description": "替代触发条件列表(OR 组合,任一命中则规则生效,无需 pattern 匹配)",
  212. "items": {"$ref": "#/definitions/condition"}
  213. },
  214. "entity_filter": {
  215. "type": "object",
  216. "description": "实体过滤条件(doc_title 阶段批量修正用):仅满足全部过滤条件的实体参与规则",
  217. "properties": {
  218. "entity_type": {
  219. "type": "string",
  220. "description": "实体类型,如 money"
  221. },
  222. "notes_in": {
  223. "type": "array",
  224. "description": "entity.notes ∈ 列表",
  225. "items": {"type": "string"},
  226. "minItems": 1
  227. },
  228. "label": {
  229. "type": "integer",
  230. "description": "实体当前标签",
  231. "minimum": 0,
  232. "maximum": 5
  233. }
  234. },
  235. "additionalProperties": false
  236. },
  237. "golden_examples": {
  238. "type": "array",
  239. "description": "黄金样例(正例和负例)",
  240. "items": {
  241. "type": "object",
  242. "required": ["input", "should_match"],
  243. "properties": {
  244. "input": {"type": "string", "description": "测试输入文本"},
  245. "should_match": {"type": "boolean", "description": "true=应匹配, false=不应匹配"},
  246. "expected_group": {
  247. "type": "string",
  248. "description": "期望的匹配组 0 内容(可选)"
  249. },
  250. "expected_named_groups": {
  251. "type": "object",
  252. "description": "期望的命名捕获组键值对(可选)"
  253. }
  254. }
  255. }
  256. }
  257. },
  258. "additionalProperties": false
  259. }
  260. },
  261. "keyword_rules": {
  262. "type": "array",
  263. "description": "关键词分类规则列表(用于 turnMoneySource 等关键词驱动分类)",
  264. "items": {
  265. "type": "object",
  266. "required": ["id", "label", "keywords"],
  267. "properties": {
  268. "id": {
  269. "type": "string",
  270. "pattern": "^[a-z][a-z0-9_]*$"
  271. },
  272. "label": {
  273. "type": "string",
  274. "description": "匹配后输出的分类标签"
  275. },
  276. "keywords": {
  277. "type": "array",
  278. "description": "正向关键词列表(任一命中即归类)",
  279. "items": {"type": "string"},
  280. "minItems": 1
  281. },
  282. "excludes": {
  283. "type": "array",
  284. "description": "排除关键词(任一命中则不归类)",
  285. "items": {"type": "string"}
  286. },
  287. "priority": {
  288. "type": "integer",
  289. "description": "优先级(数字越大越优先,默认 0)",
  290. "default": 0
  291. }
  292. },
  293. "additionalProperties": false
  294. }
  295. },
  296. "classifier_config": {
  297. "type": "object",
  298. "description": "分类器配置(用于关键词分类规则的全局参数)",
  299. "properties": {
  300. "min_matches": {
  301. "type": "integer",
  302. "description": "最少命中数(低于此数返回默认标签)",
  303. "default": 1
  304. },
  305. "max_matches": {
  306. "type": "integer",
  307. "description": "最多命中数(超过此数返回默认标签)",
  308. "default": 99
  309. },
  310. "default_label": {
  311. "type": "string",
  312. "description": "默认标签(未命中或超出范围时返回)",
  313. "default": ""
  314. },
  315. "separator": {
  316. "type": "string",
  317. "description": "多标签连接符",
  318. "default": ","
  319. },
  320. "sort": {
  321. "type": "boolean",
  322. "description": "是否对结果排序",
  323. "default": true
  324. }
  325. },
  326. "additionalProperties": false
  327. },
  328. "probability_ladder": {
  329. "type": "object",
  330. "description": "概率阶梯(ROLE 类规则用):命名原代码中的魔法概率,须与 predictors/role_context.py 中常量一致",
  331. "properties": {
  332. "p_model_threshold": {
  333. "type": "number",
  334. "description": "模型置信阈值:prob < 此值时置 label=5 交给规则召回",
  335. "minimum": 0,
  336. "maximum": 1
  337. },
  338. "p_rule_override": {
  339. "type": "number",
  340. "description": "规则覆盖模型(略高于模型置信)",
  341. "minimum": 0,
  342. "maximum": 1
  343. },
  344. "p_fallback": {
  345. "type": "number",
  346. "description": "兜底召回(略高于标题召回基线)",
  347. "minimum": 0,
  348. "maximum": 1
  349. },
  350. "p_title": {
  351. "type": "number",
  352. "description": "标题召回基线",
  353. "minimum": 0,
  354. "maximum": 1
  355. }
  356. },
  357. "required": ["p_model_threshold", "p_rule_override", "p_fallback", "p_title"],
  358. "additionalProperties": false
  359. }
  360. },
  361. "additionalProperties": false
  362. }