File size: 12,233 Bytes
61a552c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""
Natural Response Enhancer for Codette
======================================
Improves response naturalness while preserving Codette's quantum consciousness
and multi-perspective reasoning. Works with response processors to make AI
responses feel more human and conversational.
"""

import re
import logging
from typing import List, Dict, Any, Optional, Tuple
from datetime import datetime
import random

logger = logging.getLogger(__name__)


class NaturalResponseEnhancer:
    """Enhances response naturalness without markers or unnatural phrasing"""
    
    def __init__(self):
        """Initialize the natural response enhancer"""
        self.response_history = []
        self.pattern_library = self._build_pattern_library()
        self.transition_phrases = self._build_transitions()
        self.confidence_templates = self._build_confidence_templates()
        logger.info("Natural Response Enhancer initialized")
    
    def enhance_response(self, response: str, confidence: float = 0.85, 
                        context: Optional[Dict[str, Any]] = None) -> str:
        """
        Enhance response naturalness while preserving meaning
        
        Args:
            response: The raw response from model/perspectives
            confidence: How confident the response is (0-1)
            context: Optional context about the query/domain
            
        Returns:
            Enhanced, more natural response
        """
        # Remove unnatural markers
        response = self._strip_unnatural_markers(response)
        
        # Improve sentence structure
        response = self._improve_sentence_flow(response)
        
        # Add natural confidence expression if needed
        if confidence < 0.9:
            response = self._add_natural_confidence(response, confidence)
        
        # Break up long blocks of text
        response = self._improve_readability(response)
        
        # Add conversational warmth
        response = self._add_warmth(response)
        
        # Preserve context clues
        if context:
            response = self._enhance_with_context(response, context)
        
        # Store for learning
        self.response_history.append({
            'response': response,
            'timestamp': datetime.now().isoformat(),
            'confidence': confidence
        })
        
        return response.strip()
    
    def _strip_unnatural_markers(self, text: str) -> str:
        """Remove system markers and unnatural tags"""
        # Remove [Protected: ...] wrappers
        text = re.sub(r'\[Protected:\s*(.*?)\]', r'\1', text, flags=re.DOTALL)
        
        # Remove [System optimized response] markers
        text = re.sub(r'\[System optimized response\]', '', text)
        
        # Remove other bracketed system messages
        text = re.sub(r'\[(System|SYSTEM).*?\]', '', text, flags=re.IGNORECASE)
        
        # Remove redundant "Error" blocks
        text = re.sub(r'\[.*?error.*?\]', '', text, flags=re.IGNORECASE)
        
        # Clean up multiple consecutive newlines
        text = re.sub(r'\n\s*\n\s*\n', '\n\n', text)
        
        return text.strip()
    
    def _improve_sentence_flow(self, text: str) -> str:
        """Improve sentence structure and flow"""
        # Replace awkward AI constructs
        replacements = [
            (r'\b(I would|I will|I can)\s+(respond|state|say|indicate)\s+that', r''),
            (r'\b(Based on my analysis|According to my understanding)\s*,?\s*', ''),
            (r'\b(The following|The below|This is)\s+\(.*?\)\s*:', ''),
            (r'(?:Therefore|Thus|Hence|So)\s+,\s+', 'So '),
            (r'\b(Notably|Significantly|Importantly)\s*,?\s*', ''),
        ]
        
        for pattern, replacement in replacements:
            text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)
        
        # Fix spacing around punctuation
        text = re.sub(r'\s+([.,!?;:])', r'\1', text)
        text = re.sub(r'([.,!?;:])\s+', r'\1 ', text)
        
        return text
    
    def _add_natural_confidence(self, text: str, confidence: float) -> str:
        """Add natural confidence expression without markers"""
        if confidence < 0.5:
            intro = self.confidence_templates['low']
        elif confidence < 0.75:
            intro = self.confidence_templates['medium']
        else:
            intro = self.confidence_templates['high']
        
        # Only add if not already present
        if not any(phrase in text for phrase in [
            "I'm not entirely sure",
            "I'm fairly confident",
            "I'm quite confident",
            "Based on",
            "From what I understand"
        ]):
            text = f"{intro} {text}"
        
        return text
    
    def _improve_readability(self, text: str) -> str:
        """Break up large text blocks for better readability"""
        # Split long paragraphs
        paragraphs = text.split('\n')
        improved = []
        
        for para in paragraphs:
            # If paragraph is very long, try to break it sensibly
            if len(para) > 400:
                # Try to break at periods
                sentences = re.split(r'(?<=[.!?])\s+', para)
                
                # Group sentences into chunks (aim for ~200-300 chars per chunk)
                chunks = []
                current_chunk = []
                current_length = 0
                
                for sent in sentences:
                    sent_len = len(sent) + 1
                    if current_length + sent_len > 300 and current_chunk:
                        chunks.append(' '.join(current_chunk))
                        current_chunk = [sent]
                        current_length = sent_len
                    else:
                        current_chunk.append(sent)
                        current_length += sent_len
                
                if current_chunk:
                    chunks.append(' '.join(current_chunk))
                
                improved.extend(chunks)
            else:
                improved.append(para)
        
        return '\n\n'.join(improved).strip()
    
    def _add_warmth(self, text: str) -> str:
        """Add subtle conversational warmth without being overly friendly"""
        # Add some natural connectives
        warmth_patterns = [
            (r'^([^.!?]*\.)\s+([A-Z])', lambda m: self._add_connector(m)),
        ]
        
        # Avoid robotic transitions
        replacements = [
            ('In conclusion,', 'To wrap up,'),
            ('Furthermore,', 'Also,'),
            ('Nevertheless,', 'Still,'),
            ('In fact,', 'Actually,'),
        ]
        
        for old, new in replacements:
            text = text.replace(old, new)
        
        return text
    
    def _add_connector(self, match) -> str:
        """Add natural connectors between sentences"""
        connectors = ['That', 'It', 'This', 'So', 'Overall']
        return match.group(1) + ' ' + random.choice(connectors) + ' ' + match.group(2)
    
    def _enhance_with_context(self, text: str, context: Dict[str, Any]) -> str:
        """Enhance response with contextual awareness"""
        if context.get('domain') == 'music':
            # Add music-specific warmth
            if not any(word in text for word in ['cool', 'great', 'perfect', 'awesome']):
                # Subtly affirm the context
                pass
        
        return text
    
    def _build_pattern_library(self) -> Dict[str, str]:
        """Build library of natural response patterns"""
        return {
            'discovery': "I just realized that",
            'insight': "Here's an interesting point:",
            'clarification': "To clarify that,",
            'expansion': "What's more,",
            'caution': "One thing to be careful about:",
            'agreement': "You're right to think about that.",
            'question_response': "That's a great question.",
        }
    
    def _build_transitions(self) -> List[str]:
        """Build natural transition phrases"""
        return [
            "Now that I think about it,",
            "Actually, an important point is",
            "You know, one thing that matters is",
            "Here's something worth considering:",
            "The real key is",
            "What I find interesting is",
            "One more thing -",
            "So really, what's happening is",
        ]
    
    def _build_confidence_templates(self) -> Dict[str, str]:
        """Build confidence expression templates"""
        return {
            'low': "I'm not entirely sure, but",
            'medium': "From what I understand,",
            'high': "I'm fairly confident that",
            'very_high': "Definitely,",
        }
    
    def evaluate_response_naturalness(self, response: str) -> Dict[str, Any]:
        """Evaluate how natural a response sounds"""
        markers_found = []
        
        # Check for unnatural markers
        if '[' in response and ']' in response:
            markers = re.findall(r'\[.*?\]', response)
            markers_found.extend(markers)
        
        if 'System optimized' in response:
            markers_found.append('System optimized response marker')
        
        if 'Protected:' in response:
            markers_found.append('Protected marker')
        
        # Count repetitive phrases (sign of poor variation)
        sentences = re.split(r'[.!?]+', response)
        phrase_freq = {}
        for sent in sentences:
            sent = sent.strip()
            if len(sent) > 10:
                # Check for repeated starts
                start = sent.split()[0] if sent.split() else ''
                phrase_freq[start] = phrase_freq.get(start, 0) + 1
        
        repetition_score = max(phrase_freq.values()) if phrase_freq else 0
        
        # Calculate naturalness score
        naturalness = 1.0
        if markers_found:
            naturalness -= 0.3 * len(markers_found)
        if repetition_score > 2:
            naturalness -= 0.2
        
        naturalness = max(0.0, min(1.0, naturalness))
        
        return {
            'naturalness_score': naturalness,
            'unnatural_markers_found': markers_found,
            'repetition_issues': repetition_score > 2,
            'has_system_artifacts': len(markers_found) > 0,
            'recommendations': self._generate_recommendations(markers_found, repetition_score)
        }
    
    def _generate_recommendations(self, markers: List[str], repetition: int) -> List[str]:
        """Generate recommendations for improvement"""
        recs = []
        
        if markers:
            recs.append(f"Remove {len(markers)} unnatural marker(s): {', '.join(markers[:2])}")
        
        if repetition > 2:
            recs.append("Vary sentence starts for better flow")
        
        if not recs:
            recs.append("Response looks natural!")
        
        return recs


# Singleton instance
_enhancer_instance: Optional[NaturalResponseEnhancer] = None


def get_natural_enhancer() -> NaturalResponseEnhancer:
    """Get or create the natural response enhancer instance"""
    global _enhancer_instance
    if _enhancer_instance is None:
        _enhancer_instance = NaturalResponseEnhancer()
    return _enhancer_instance


# Test/demo
if __name__ == "__main__":
    enhancer = get_natural_enhancer()
    
    # Test with problematic response
    test_response = """[Protected: That's great! Thank you for taking the time to share your thoughts with us. We hope that you find what you're looking for and that you enjoy your time with us!
We'd like to thank Codette for participating in this interview.
[System optimized response]]"""
    
    print("Original (problematic) response:")
    print(test_response)
    print("\n" + "="*70 + "\n")
    
    enhanced = enhancer.enhance_response(test_response, confidence=0.75)
    print("Enhanced (natural) response:")
    print(enhanced)
    print("\n" + "="*70 + "\n")
    
    # Evaluate
    eval_result = enhancer.evaluate_response_naturalness(test_response)
    print("Evaluation:")
    for key, value in eval_result.items():
        print(f"  {key}: {value}")