Module: Elect::Cleaner

Defined in:
lib/elect/cleaner.rb

Overview

清理器:接收文本,移除 AIGC 水印块。

Class Method Summary collapse

Class Method Details

.clean(text) ⇒ Object

从文本中移除所有水印块。

策略:

1. 用 Pattern::REGEX 全局替换为空串
2. 清理因移除产生的连续空行(>2 连续空行压缩为 2)
3. 清理文件首尾多余空白

返回 [清理后文本, 移除数量]



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elect/cleaner.rb', line 19

def self.clean(text)
  n = Pattern.count(text)
  return [text, 0] if n.zero?

  cleaned = text.gsub(Pattern::REGEX, "")

  # 压缩连续空行(保留最多 2 换行 = 1 空行),兼容 CRLF
  cleaned.gsub!(/(?:\r\n|\n){3,}/, "\n\n")

  # 移除首尾多余空白
  cleaned.strip!

  # 确保文件结尾有且仅有一个换行
  cleaned << "\n" unless cleaned.empty?

  [cleaned, n]
end

.detect(text) ⇒ Object

仅检测不修改,返回水印数量



39
40
41
# File 'lib/elect/cleaner.rb', line 39

def self.detect(text)
  Pattern.count(text)
end

.watermarked?(text) ⇒ Boolean

判断文本是否含水印

Returns:

  • (Boolean)


45
46
47
# File 'lib/elect/cleaner.rb', line 45

def self.watermarked?(text)
  Pattern.watermarked?(text)
end