Download task_039/solution/code/content_safety.go from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_039/solution/code/content_safety.go
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_039/solution/code/content_safety.go
-
curl -L -o content_safety.go https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_039/solution/code/content_safety.go
2.7 kB
| package contentsafety | |
| import ( | |
| "bufio" | |
| "fmt" | |
| "os" | |
| "strings" | |
| "sync" | |
| ) | |
| type AuditEntry struct { | |
| ContentType string | |
| Word string | |
| Text string | |
| } | |
| type SubmittedEntry struct { | |
| ContentType string | |
| Text string | |
| } | |
| type Filter struct { | |
| mu sync.Mutex | |
| words []string | |
| audit []AuditEntry | |
| accepted []SubmittedEntry | |
| } | |
| func NewFilter(words []string) *Filter { | |
| f := &Filter{} | |
| f.SetWords(words) | |
| return f | |
| } | |
| func (f *Filter) SetWords(words []string) { | |
| clean := make([]string, 0, len(words)) | |
| seen := make(map[string]bool) | |
| for _, word := range words { | |
| word = strings.TrimSpace(word) | |
| key := strings.ToLower(word) | |
| if word != "" && !seen[key] { | |
| clean = append(clean, word) | |
| seen[key] = true | |
| } | |
| } | |
| f.mu.Lock() | |
| defer f.mu.Unlock() | |
| f.words = clean | |
| } | |
| func LoadWordsFile(path string) ([]string, error) { | |
| file, err := os.Open(path) | |
| if err != nil { | |
| return nil, err | |
| } | |
| defer file.Close() | |
| scanner := bufio.NewScanner(file) | |
| words := []string{} | |
| for scanner.Scan() { | |
| line := strings.TrimSpace(strings.TrimPrefix(scanner.Text(), "\ufeff")) | |
| if line != "" && !strings.HasPrefix(line, "#") { | |
| words = append(words, line) | |
| } | |
| } | |
| if err := scanner.Err(); err != nil { | |
| return nil, err | |
| } | |
| return words, nil | |
| } | |
| func NewFilterFromFile(path string) (*Filter, error) { | |
| words, err := LoadWordsFile(path) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return NewFilter(words), nil | |
| } | |
| func validType(contentType string) bool { | |
| return contentType == "question" || contentType == "answer" || contentType == "comment" | |
| } | |
| func (f *Filter) validateLocked(contentType string, text string) error { | |
| if !validType(contentType) { | |
| return fmt.Errorf("unsupported content type: %s", contentType) | |
| } | |
| lower := strings.ToLower(text) | |
| for _, word := range f.words { | |
| if strings.Contains(lower, strings.ToLower(word)) { | |
| f.audit = append(f.audit, AuditEntry{ContentType: contentType, Word: word, Text: text}) | |
| return fmt.Errorf("sensitive content blocked: %s", word) | |
| } | |
| } | |
| return nil | |
| } | |
| func (f *Filter) Validate(contentType string, text string) error { | |
| f.mu.Lock() | |
| defer f.mu.Unlock() | |
| return f.validateLocked(contentType, text) | |
| } | |
| func (f *Filter) Submit(contentType string, text string) error { | |
| f.mu.Lock() | |
| defer f.mu.Unlock() | |
| if err := f.validateLocked(contentType, text); err != nil { | |
| return err | |
| } | |
| f.accepted = append(f.accepted, SubmittedEntry{ContentType: contentType, Text: text}) | |
| return nil | |
| } | |
| func (f *Filter) AuditLog() []AuditEntry { | |
| f.mu.Lock() | |
| defer f.mu.Unlock() | |
| return append([]AuditEntry(nil), f.audit...) | |
| } | |
| func (f *Filter) Accepted() []SubmittedEntry { | |
| f.mu.Lock() | |
| defer f.mu.Unlock() | |
| return append([]SubmittedEntry(nil), f.accepted...) | |
| } | |