-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextcleaner.go
More file actions
179 lines (148 loc) · 4.35 KB
/
textcleaner.go
File metadata and controls
179 lines (148 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package docreader
import (
"regexp"
"strings"
"unicode"
)
// TextCleaner 提供文本清理功能,用于优化大模型理解
type TextCleaner struct {
// TrimSpaces 是否移除行首行尾空格
TrimSpaces bool
// RemoveExtraSpaces 是否移除多余空格(连续空格压缩为一个)
RemoveExtraSpaces bool
// RemoveControlChars 是否移除特殊控制字符
RemoveControlChars bool
// MaxBlankLines 最大连续空行数
// -1: 不限制空行数(保留所有空行)
// 0: 移除所有空行
// 1: 最多保留1个连续空行(压缩多余空行)
// N: 最多保留N个连续空行
MaxBlankLines int
}
// DefaultTextCleaner 返回默认配置的文本清理器
func DefaultTextCleaner() *TextCleaner {
return &TextCleaner{
TrimSpaces: true,
RemoveExtraSpaces: true,
RemoveControlChars: true,
MaxBlankLines: 1, // 最多保留1个连续空行
}
}
// Clean 清理文本内容
func (tc *TextCleaner) Clean(text string) string {
if text == "" {
return ""
}
// 1. 移除控制字符(保留换行符、制表符等有意义的空白字符)
if tc.RemoveControlChars {
text = tc.removeControlChars(text)
}
// 2. 统一换行符为 \n
text = normalizeLineBreaks(text)
// 3. 按行处理
lines := strings.Split(text, "\n")
var cleanedLines []string
consecutiveBlankLines := 0
for _, line := range lines {
// 移除行首行尾空格
if tc.TrimSpaces {
line = strings.TrimSpace(line)
}
// 移除多余空格
if tc.RemoveExtraSpaces && line != "" {
line = tc.removeExtraSpaces(line)
}
// 处理空行
if line == "" {
consecutiveBlankLines++
// MaxBlankLines = -1: 不限制,保留所有空行
if tc.MaxBlankLines == -1 {
cleanedLines = append(cleanedLines, line)
continue
}
// MaxBlankLines = 0: 移除所有空行
if tc.MaxBlankLines == 0 {
continue
}
// MaxBlankLines > 0: 只保留指定数量的连续空行
if consecutiveBlankLines <= tc.MaxBlankLines {
cleanedLines = append(cleanedLines, line)
}
} else {
consecutiveBlankLines = 0
cleanedLines = append(cleanedLines, line)
}
}
// 4. 移除开头和结尾的空行
cleanedLines = trimEmptyLines(cleanedLines)
// 5. 合并结果
result := strings.Join(cleanedLines, "\n")
return result
}
// removeControlChars 移除控制字符,保留必要的空白字符
func (tc *TextCleaner) removeControlChars(text string) string {
var builder strings.Builder
builder.Grow(len(text))
for _, r := range text {
// 保留可打印字符、换行符、制表符、回车符
if unicode.IsPrint(r) || r == '\n' || r == '\t' || r == '\r' {
builder.WriteRune(r)
}
}
return builder.String()
}
// removeExtraSpaces 移除多余的空格(连续空格压缩为一个)
func (tc *TextCleaner) removeExtraSpaces(text string) string {
// 使用正则表达式将多个连续空格替换为单个空格
re := regexp.MustCompile(`[ \t]+`)
return re.ReplaceAllString(text, " ")
}
// normalizeLineBreaks 统一换行符
func normalizeLineBreaks(text string) string {
// 将 \r\n 和 \r 统一替换为 \n
text = strings.ReplaceAll(text, "\r\n", "\n")
text = strings.ReplaceAll(text, "\r", "\n")
return text
}
// trimEmptyLines 移除开头和结尾的空行
func trimEmptyLines(lines []string) []string {
// 移除开头的空行
start := 0
for start < len(lines) && lines[start] == "" {
start++
}
// 移除结尾的空行
end := len(lines)
for end > start && lines[end-1] == "" {
end--
}
if start >= end {
return []string{}
}
return lines[start:end]
}
// CleanText 使用默认配置清理文本的便捷函数
func CleanText(text string) string {
cleaner := DefaultTextCleaner()
return cleaner.Clean(text)
}
// CleanTextMinimal 使用最小清理配置(仅清理基本的空白,保留所有空行)
func CleanTextMinimal(text string) string {
cleaner := &TextCleaner{
TrimSpaces: true,
RemoveExtraSpaces: true,
RemoveControlChars: true,
MaxBlankLines: -1, // 不限制空行数,保留所有空行
}
return cleaner.Clean(text)
}
// CleanTextAggressive 使用激进的清理配置(最大程度压缩空间,移除所有空行)
func CleanTextAggressive(text string) string {
cleaner := &TextCleaner{
TrimSpaces: true,
RemoveExtraSpaces: true,
RemoveControlChars: true,
MaxBlankLines: 0, // 移除所有空行
}
return cleaner.Clean(text)
}