Module: Elect::Pattern
- Defined in:
- lib/elect/pattern.rb
Overview
水印模式定义与匹配逻辑。
AIGC 水印结构如下(YAML front-matter 风格,用三横线包裹):
---
AIGC:
ContentProducer: '001191110102xxxx5U9H0F10002'
ContentPropagator: '001191110102xxxx5U9H0F10002'
Label: '1'
ProduceID: '7e123539-xxxx-4617-9fdb-d10422064fe9'
PropagateID: '7e123539-xxxx-4617-9fdb-d10422064fe9'
ReservedCode1: '718161f2-xxxx-4528-9e76-b071818e46dd'
ReservedCode2: '718161f2-xxxx-4528-9e76-b071818e46dd'
---
字段值的内容是动态的,因此以结构模式匹配,而非硬编码值。
Constant Summary collapse
- REGEX =
水印块匹配的正则。
约束:
- 以 `---` 开头(行首) - 后面跟着 `AIGC:` 键 - 包含若干缩进的子字段行 - 以 `---` 结尾 - 支持前后空白行允许的子字段名(不限制值内容): ContentProducer / ContentPropagator / Label / ProduceID / PropagateID / ReservedCode1 / ReservedCode2
对非列表中字段同样宽容——只要 AIGC: dict 下有任意子键即匹配, 这样未来新增字段也可被自动识别。
换行兼容:用 \r?\n 同时匹配 LF 和 CRLF。
/ (?: ^---[ \t]*\r?\n [ \t]*AIGC:[ \t]*\r?\n (?:[ \t]+[A-Za-z_][A-Za-z0-9_]*:[ \t]*[^\r\n]*\r?\n)+ [ \t]*---[ \t]*\r?\n? ) /x.freeze
Class Method Summary collapse
-
.count(text) ⇒ Object
统计水印块数量.
-
.extract(text) ⇒ Object
提取所有水印块的文本(供调试用).
-
.match(text) ⇒ Object
匹配水印块,返回 MatchData 或 nil.
-
.watermarked?(text) ⇒ Boolean
判断是否包含水印.
Class Method Details
.count(text) ⇒ Object
统计水印块数量
66 67 68 69 |
# File 'lib/elect/pattern.rb', line 66 def self.count(text) s = sanitize(text) s.scan(REGEX).size end |
.extract(text) ⇒ Object
提取所有水印块的文本(供调试用)
73 74 75 76 |
# File 'lib/elect/pattern.rb', line 73 def self.extract(text) s = sanitize(text) s.scan(REGEX) end |
.match(text) ⇒ Object
匹配水印块,返回 MatchData 或 nil
52 53 54 55 |
# File 'lib/elect/pattern.rb', line 52 def self.match(text) s = sanitize(text) REGEX.match(s) end |
.watermarked?(text) ⇒ Boolean
判断是否包含水印
59 60 61 62 |
# File 'lib/elect/pattern.rb', line 59 def self.watermarked?(text) s = sanitize(text) REGEX.match?(s) end |