-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment.go
More file actions
136 lines (113 loc) · 3.07 KB
/
sentiment.go
File metadata and controls
136 lines (113 loc) · 3.07 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
package main
import (
"strings"
"unicode"
)
// SentimentMatch represents a matched pattern with context
type SentimentMatch struct {
Pattern string
Context string
StartIndex int
ConfidenceScore float64
}
// TextAnalyzer provides methods to analyze text content
type TextAnalyzer struct {
// Configurable thresholds and settings
MinContextLength int
MaxContextLength int
Patterns map[string]float64
Threshold float64
Triggers []string
AnyTriggers bool
}
// NewTextAnalyzer creates a new analyzer with default settings
func NewTextAnalyzer(triggers []string, patterns map[string]float64, threshold float64, anyTrigger bool) *TextAnalyzer {
return &TextAnalyzer{
MinContextLength: 60,
MaxContextLength: 300,
Patterns: patterns,
Threshold: threshold,
Triggers: triggers,
AnyTriggers: anyTrigger,
}
}
// preprocessText normalizes input text for analysis
func (a *TextAnalyzer) preprocessText(text string) string {
// Convert to lowercase
text = strings.ToLower(text)
// Replace multiple spaces with single space
text = strings.Join(strings.Fields(text), " ")
return text
}
// getContext extracts surrounding text for a match
func (a *TextAnalyzer) getContext(text string, start, end int) string {
// Find context boundaries
contextStart := start - a.MinContextLength
if contextStart < 0 {
contextStart = 0
}
contextEnd := end + a.MinContextLength
if contextEnd > len(text) {
contextEnd = len(text)
}
// Find word boundaries
for contextStart > 0 && !unicode.IsSpace(rune(text[contextStart-1])) {
contextStart--
}
for contextEnd < len(text) && !unicode.IsSpace(rune(text[contextEnd])) {
contextEnd++
}
return text[contextStart:contextEnd]
}
// AnalyzeText examines text for patterns and returns matches with context
func (a *TextAnalyzer) AnalyzeText(text string) []SentimentMatch {
text = a.preprocessText(text)
var matches []SentimentMatch
// Search for patterns and collect matches with context
for pattern, baseConfidence := range a.Patterns {
index := 0
for {
oldIndex := index
index = strings.Index(text[index:], pattern)
if index == -1 || index < oldIndex {
break
}
// Get surrounding context
context := a.getContext(text, index, index+len(pattern))
// Create match entry
match := SentimentMatch{
Pattern: pattern,
Context: context,
StartIndex: index,
ConfidenceScore: baseConfidence,
}
matches = append(matches, match)
index++
}
}
return matches
}
func (a *TextAnalyzer) HasTriggers(text string) bool {
text = strings.ToLower(text)
if len(a.Triggers) == 0 {
return true
}
for _, word := range a.Triggers {
if !strings.Contains(text, word) && !a.AnyTriggers {
return false
}
}
return true
}
func (a *TextAnalyzer) Score(text string) (float64, bool) {
if !a.HasTriggers(text) {
return 0, false
}
matches := a.AnalyzeText(text)
// Process matches as needed
var total float64
for _, match := range matches {
total += match.ConfidenceScore
}
return total, total >= a.Threshold
}