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