AnalyzeAndRedact 分析并脱敏,返回脱敏报告
(text string)
| 129 | |
| 130 | // AnalyzeAndRedact 分析并脱敏,返回脱敏报告 |
| 131 | func (r *PIIRedactor) AnalyzeAndRedact(text string) (*RedactionResult, string) { |
| 132 | if r.detector == nil { |
| 133 | return &RedactionResult{}, text |
| 134 | } |
| 135 | |
| 136 | ctx := context.Background() |
| 137 | matches, err := r.detector.Detect(ctx, text) |
| 138 | if err != nil { |
| 139 | return &RedactionResult{ |
| 140 | Error: err.Error(), |
| 141 | }, text |
| 142 | } |
| 143 | |
| 144 | if len(matches) == 0 { |
| 145 | return &RedactionResult{}, text |
| 146 | } |
| 147 | |
| 148 | result := &RedactionResult{ |
| 149 | OriginalLength: len(text), |
| 150 | PIIFound: true, |
| 151 | MatchedTypes: make(map[PIIType]int), |
| 152 | TotalMatches: len(matches), |
| 153 | Matches: matches, |
| 154 | } |
| 155 | |
| 156 | // 统计每种PII类型的数量 |
| 157 | for _, match := range matches { |
| 158 | result.MatchedTypes[match.Type]++ |
| 159 | } |
| 160 | |
| 161 | // 执行脱敏 |
| 162 | redactedText := r.Redact(text) |
| 163 | result.RedactedLength = len(redactedText) |
| 164 | |
| 165 | return result, redactedText |
| 166 | } |
| 167 | |
| 168 | // RedactionResult 脱敏结果报告 |
| 169 | type RedactionResult struct { |