| import requests |
| import json |
| import re |
|
|
| class GrammarQualityFix: |
| """Fix the keyword stuffing issue while maintaining adjective richness""" |
| |
| def __init__(self): |
| self.api_url = "http://localhost:8002" |
| |
| def enhance_with_grammar_constraints(self, description, max_adjectives_per_noun=2): |
| """Enhanced adjective injection with grammar constraints""" |
| try: |
| response = requests.post( |
| f"{self.api_url}/describe/scene", |
| json={ |
| "scene_description": description, |
| "enhance_adjectives": True, |
| "include_spatial": True, |
| "adjective_density": 0.6, |
| "grammar_constraint": True |
| }, |
| timeout=10 |
| ) |
| |
| if response.status_code == 200: |
| result = response.json() |
| raw_output = result["enhanced_description"] |
| |
| |
| cleaned_output = self.fix_broken_grammar(raw_output) |
| return cleaned_output |
| |
| except Exception as e: |
| print(f"API Error: {e}") |
| |
| return description |
| |
| def fix_broken_grammar(self, text): |
| """Fix common grammar issues from adjective stuffing""" |
| |
| text = re.sub(r'(\w+ ){3,}A? (\w+)', self._fix_adjective_clusters, text) |
| |
| |
| sentences = text.split('. ') |
| fixed_sentences = [] |
| |
| for sentence in sentences: |
| if sentence.strip(): |
| |
| sentence = sentence[0].upper() + sentence[1:] if sentence else sentence |
| fixed_sentences.append(sentence) |
| |
| return '. '.join(fixed_sentences) |
| |
| def _fix_adjective_clusters(self, match): |
| """Fix clusters of adjectives before nouns""" |
| words = match.group(0).split() |
| nouns = ['car', 'person', 'building', 'tree', 'water', 'sky', 'mountain'] |
| |
| |
| noun_index = None |
| for i, word in enumerate(words): |
| if word.lower() in nouns or (word.lower() == 'a' and i+1 < len(words) and words[i+1].lower() in nouns): |
| noun_index = i |
| break |
| |
| if noun_index and noun_index > 0: |
| |
| adjectives = words[:noun_index][-2:] |
| rest = words[noun_index:] |
| return ' '.join(adjectives + rest) |
| |
| return match.group(0) |
|
|
| |
| fixer = GrammarQualityFix() |
|
|
| test_scenes = [ |
| "A car driving through a city at night with neon lights", |
| "A person dancing in a room with colorful lighting effects" |
| ] |
|
|
| print("🔧 TESTING GRAMMAR FIXES...") |
| for scene in test_scenes: |
| fixed_output = fixer.enhance_with_grammar_constraints(scene) |
| print(f"Input: {scene}") |
| print(f"Fixed: {fixed_output}") |
| print("---") |
|
|