-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeedback_validator.py
More file actions
266 lines (220 loc) · 9.54 KB
/
feedback_validator.py
File metadata and controls
266 lines (220 loc) · 9.54 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: feedback_validator.py ║
Version: 0.0.47 ║
Last Modified: 2026-04-15 18:43:54 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 100.0/100 ║
• Total Lines: 269 ║
• Open Issues: TODOs: 1, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
"""
#!/usr/bin/env python3
"""
Example Python Script Template for Feedback Validation
This script demonstrates how to create a custom feedback validation plugin
that can be called from ThemisDB's feedback system via subprocess or API.
Usage:
python3 feedback_validator.py validate <feedback_json>
python3 feedback_validator.py stats
The script reads feedback data from stdin (JSON) and outputs validation result.
"""
import json
import sys
import re
from typing import Dict, Any, List
from enum import Enum
class ValidationResult(Enum):
"""Validation result types"""
ACCEPT = "accept"
REJECT = "reject"
FLAG = "flag"
MODIFY = "modify"
class FeedbackValidator:
"""
Custom feedback validator
Implement your validation logic here. This example includes:
- Spam keyword detection
- PII detection (email, phone)
- Quality scoring
- Custom business rules
"""
def __init__(self, config: Dict[str, Any] = None):
self.config = config or {}
self.spam_keywords = self.config.get('spam_keywords', [
'buy now', 'click here', 'casino', 'lottery',
'free money', 'work from home'
])
self.validation_count = 0
self.rejected_count = 0
def validate(self, feedback: Dict[str, Any]) -> Dict[str, Any]:
"""
Validate feedback and return result
Args:
feedback: Feedback data with fields:
- question: str
- answer: str
- correction: str (optional)
- comment: str (optional)
- user_id: str
- adapter_id: str
- is_positive: bool
Returns:
Validation response with:
- result: str (accept/reject/flag/modify)
- reason: str (optional)
- confidence: float (0-1)
- plugin_data: dict (optional)
"""
self.validation_count += 1
result = {
'result': ValidationResult.ACCEPT.value,
'confidence': 1.0,
'plugin_data': {}
}
# Check 1: Spam keywords
if self._contains_spam(feedback):
result['result'] = ValidationResult.REJECT.value
result['reason'] = 'Contains spam keywords'
result['confidence'] = 0.9
self.rejected_count += 1
return result
# Check 2: PII detection
pii_found = self._detect_pii(feedback)
if pii_found:
result['result'] = ValidationResult.FLAG.value
result['reason'] = f'Contains PII: {", ".join(pii_found)}'
result['confidence'] = 0.95
result['plugin_data']['pii_types'] = pii_found
return result
# Check 3: Quality score
quality_score = self._calculate_quality(feedback)
result['plugin_data']['quality_score'] = quality_score
if quality_score < 0.3:
result['result'] = ValidationResult.REJECT.value
result['reason'] = 'Low quality feedback'
result['confidence'] = quality_score
self.rejected_count += 1
elif quality_score < 0.6:
result['result'] = ValidationResult.FLAG.value
result['reason'] = 'Medium quality - needs review'
result['confidence'] = quality_score
else:
result['confidence'] = quality_score
return result
def _contains_spam(self, feedback: Dict[str, Any]) -> bool:
"""Check if feedback contains spam keywords"""
text = ' '.join([
feedback.get('question', ''),
feedback.get('answer', ''),
feedback.get('comment', '')
]).lower()
return any(keyword in text for keyword in self.spam_keywords)
def _detect_pii(self, feedback: Dict[str, Any]) -> List[str]:
"""Detect PII in feedback"""
pii_found = []
text = ' '.join([
feedback.get('question', ''),
feedback.get('answer', ''),
feedback.get('comment', '')
])
# Email pattern
if re.search(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', text):
pii_found.append('email')
# Phone pattern (various formats)
if re.search(r'\d{3}[-.\s]?\d{3}[-.\s]?\d{4}', text):
pii_found.append('phone')
# SSN pattern (XXX-XX-XXXX)
if re.search(r'\d{3}-\d{2}-\d{4}', text):
pii_found.append('ssn')
# Credit card pattern
if re.search(r'\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}', text):
pii_found.append('credit_card')
return pii_found
def _calculate_quality(self, feedback: Dict[str, Any]) -> float:
"""Calculate quality score (0-1)"""
# Configuration constants
MIN_LENGTH_SHORT = 5
MIN_LENGTH_NORMAL = 10
MAX_LENGTH = 5000
MIN_REPETITION = 10
score = 1.0
question = feedback.get('question', '')
answer = feedback.get('answer', '')
correction = feedback.get('correction', '')
is_positive = feedback.get('is_positive', True)
# Length checks
if len(question) < MIN_LENGTH_NORMAL:
score -= 0.2
if len(answer) < MIN_LENGTH_NORMAL:
score -= 0.2
# Too short overall
if len(question) < MIN_LENGTH_SHORT or len(answer) < MIN_LENGTH_SHORT:
score -= 0.3
# Negative feedback without correction
if not is_positive and len(correction) < MIN_LENGTH_SHORT:
score -= 0.3
# Excessive repetition
if re.search(rf'(.)\1{{{MIN_REPETITION},}}', question + answer):
score -= 0.4
# Too long (copy-paste spam)
if len(question) > MAX_LENGTH or len(answer) > MAX_LENGTH:
score -= 0.3
return max(0.0, min(1.0, score))
def get_statistics(self) -> Dict[str, Any]:
"""Get plugin statistics"""
return {
'validation_count': self.validation_count,
'rejected_count': self.rejected_count,
'rejection_rate': self.rejected_count / max(1, self.validation_count),
'spam_keywords_count': len(self.spam_keywords)
}
def main():
"""Main entry point"""
if len(sys.argv) < 2:
print(json.dumps({
'error': 'Missing command',
'usage': 'feedback_validator.py validate|stats'
}), file=sys.stderr)
sys.exit(1)
command = sys.argv[1]
# Load config if available
config = {}
try:
with open('feedback_validator_config.json', 'r') as f:
config = json.load(f)
except FileNotFoundError:
pass
validator = FeedbackValidator(config)
if command == 'validate':
# Read feedback from stdin
try:
feedback_data = json.load(sys.stdin)
except json.JSONDecodeError as e:
print(json.dumps({
'error': f'Invalid JSON input: {str(e)}'
}), file=sys.stderr)
sys.exit(1)
# Validate
result = validator.validate(feedback_data)
# Output result
print(json.dumps(result, indent=2))
elif command == 'stats':
# Get statistics
stats = validator.get_statistics()
print(json.dumps(stats, indent=2))
else:
print(json.dumps({
'error': f'Unknown command: {command}',
'valid_commands': ['validate', 'stats']
}), file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()