File size: 9,809 Bytes
38c016b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test to analyze the visual representation issue that might cause words to appear extended.
"""

import sys
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent.parent  # Go up from test-integration to backend-py
sys.path.insert(0, str(project_root))

from src.services.crossword_generator_fixed import CrosswordGeneratorFixed

def analyze_visual_word_extensions():
    """Analyze how visual word extensions might occur in the crossword grid."""
    
    print("πŸ” Analyzing Visual Word Extension Issues\n")
    
    generator = CrosswordGeneratorFixed(vector_service=None)
    
    # Create a scenario that might produce the "visual extension" effect
    # Based on the crossword images, it seems like words are appearing to extend
    # when they shouldn't
    
    grid = [["." for _ in range(15)] for _ in range(15)]
    placed_words = []
    
    print("=" * 60)
    print("SCENARIO 1: Creating potential visual extension")
    print("=" * 60)
    
    # Step 1: Place MACHINE
    if generator._can_place_word(grid, "MACHINE", 7, 3, "horizontal"):
        generator._place_word(grid, "MACHINE", 7, 3, "horizontal")
        placed_words.append({
            "word": "MACHINE", "row": 7, "col": 3, "direction": "horizontal", "number": 1
        })
        print("βœ… Placed MACHINE at (7, 3) horizontal")
    
    # Step 2: Place a word that intersects at the end of MACHINE
    # This might create the visual illusion of extension
    if generator._can_place_word(grid, "EXPERT", 5, 9, "vertical"):  # E intersects with E in MACHINE
        generator._place_word(grid, "EXPERT", 5, 9, "vertical")
        placed_words.append({
            "word": "EXPERT", "row": 5, "col": 9, "direction": "vertical", "number": 2
        })
        print("βœ… Placed EXPERT at (5, 9) vertical")
    
    # Step 3: Place another intersecting word that might create confusion
    if generator._can_place_word(grid, "TYPOGRAPHY", 7, 10, "horizontal"):  # Starts right after MACHINE
        generator._place_word(grid, "TYPOGRAPHY", 7, 10, "horizontal")
        placed_words.append({
            "word": "TYPOGRAPHY", "row": 7, "col": 10, "direction": "horizontal", "number": 3
        })
        print("βœ… Placed TYPOGRAPHY at (7, 10) horizontal")
    
    print("\nCurrent grid state:")
    print_detailed_grid(grid, placed_words)
    
    # Analyze what the human eye might see
    print("\n" + "=" * 60)
    print("VISUAL ANALYSIS")
    print("=" * 60)
    
    analyze_row_sequences(grid, placed_words, 7)  # Analyze row 7 specifically
    analyze_column_sequences(grid, placed_words, 9)  # Analyze column 9 specifically
    
    # Test another scenario
    print("\n" + "=" * 60)
    print("SCENARIO 2: Adjacent word placement")
    print("=" * 60)
    
    grid2 = [["." for _ in range(15)] for _ in range(15)]
    placed_words2 = []
    
    # Place words that are adjacent but should be separate
    if generator._can_place_word(grid2, "SCIENCE", 6, 2, "horizontal"):
        generator._place_word(grid2, "SCIENCE", 6, 2, "horizontal")
        placed_words2.append({
            "word": "SCIENCE", "row": 6, "col": 2, "direction": "horizontal", "number": 1
        })
    
    # Try to place a word immediately after with no gap
    if generator._can_place_word(grid2, "TECHNOLOGY", 6, 9, "horizontal"):
        generator._place_word(grid2, "TECHNOLOGY", 6, 9, "horizontal")
        placed_words2.append({
            "word": "TECHNOLOGY", "row": 6, "col": 9, "direction": "horizontal", "number": 2
        })
        print("⚠️ Placed TECHNOLOGY immediately after SCIENCE")
    else:
        print("βœ… Correctly prevented TECHNOLOGY placement immediately after SCIENCE")
    
    print("\nGrid 2 state:")
    print_detailed_grid(grid2, placed_words2)
    
    # Check what boundary checking should prevent
    print("\n" + "=" * 60)
    print("BOUNDARY VALIDATION TEST")
    print("=" * 60)
    
    test_boundary_edge_cases(generator)

def print_detailed_grid(grid, placed_words, highlight_rows=None):
    """Print grid with detailed analysis."""
    print("Grid with word boundaries marked:")
    print("   ", end="")
    for c in range(min(15, len(grid[0]))):
        print(f"{c:2d}", end="")
    print()
    
    for r in range(min(15, len(grid))):
        print(f"{r:2d}: ", end="")
        for c in range(min(15, len(grid[0]))):
            cell = grid[r][c]
            if cell == ".":
                print(" .", end="")
            else:
                print(f" {cell}", end="")
        print()
    
    print("\nPlaced words:")
    for i, word_info in enumerate(placed_words):
        print(f"  {i+1}. {word_info['word']} at ({word_info['row']}, {word_info['col']}) {word_info['direction']}")

def analyze_row_sequences(grid, placed_words, row):
    """Analyze letter sequences in a specific row."""
    print(f"\nAnalyzing row {row} sequences:")
    
    # Extract all letters in the row
    row_letters = []
    for c in range(len(grid[0])):
        if grid[row][c] != ".":
            row_letters.append((c, grid[row][c]))
    
    if not row_letters:
        print("  No letters in this row")
        return
    
    print(f"  Letters found: {row_letters}")
    
    # Find continuous sequences
    sequences = []
    current_seq = []
    
    for i, (col, letter) in enumerate(row_letters):
        if not current_seq or col == row_letters[i-1][0] + 1:
            current_seq.append((col, letter))
        else:
            if len(current_seq) > 1:
                sequences.append(current_seq)
            current_seq = [(col, letter)]
    
    if len(current_seq) > 1:
        sequences.append(current_seq)
    
    print(f"  Continuous sequences: {len(sequences)}")
    for i, seq in enumerate(sequences):
        word = "".join([letter for col, letter in seq])
        start_col = seq[0][0]
        print(f"    Sequence {i+1}: '{word}' starting at column {start_col}")
        
        # Check if this matches intended words
        intended = False
        for word_info in placed_words:
            if (word_info["direction"] == "horizontal" and 
                word_info["row"] == row and 
                word_info["col"] == start_col and
                word_info["word"] == word):
                intended = True
                print(f"      βœ… This is the intended word '{word_info['word']}'")
                break
        
        if not intended and len(word) > 1:
            print(f"      ❌ This appears to be an unintended word formation!")

def analyze_column_sequences(grid, placed_words, col):
    """Analyze letter sequences in a specific column."""
    print(f"\nAnalyzing column {col} sequences:")
    
    # Extract all letters in the column
    col_letters = []
    for r in range(len(grid)):
        if grid[r][col] != ".":
            col_letters.append((r, grid[r][col]))
    
    if not col_letters:
        print("  No letters in this column")
        return
    
    print(f"  Letters found: {col_letters}")
    
    # Find continuous sequences
    sequences = []
    current_seq = []
    
    for i, (row, letter) in enumerate(col_letters):
        if not current_seq or row == col_letters[i-1][0] + 1:
            current_seq.append((row, letter))
        else:
            if len(current_seq) > 1:
                sequences.append(current_seq)
            current_seq = [(row, letter)]
    
    if len(current_seq) > 1:
        sequences.append(current_seq)
    
    print(f"  Continuous sequences: {len(sequences)}")
    for i, seq in enumerate(sequences):
        word = "".join([letter for row, letter in seq])
        start_row = seq[0][0]
        print(f"    Sequence {i+1}: '{word}' starting at row {start_row}")
        
        # Check if this matches intended words
        intended = False
        for word_info in placed_words:
            if (word_info["direction"] == "vertical" and 
                word_info["col"] == col and 
                word_info["row"] == start_row and
                word_info["word"] == word):
                intended = True
                print(f"      βœ… This is the intended word '{word_info['word']}'")
                break
        
        if not intended and len(word) > 1:
            print(f"      ❌ This appears to be an unintended word formation!")

def test_boundary_edge_cases(generator):
    """Test specific boundary edge cases."""
    
    # Test case 1: Words that touch but shouldn't form a continuous sequence
    grid = [["." for _ in range(10)] for _ in range(10)]
    
    # Place first word
    generator._place_word(grid, "CAT", 5, 2, "horizontal")
    
    # Try to place second word immediately adjacent (should be blocked)
    can_place = generator._can_place_word(grid, "DOG", 5, 5, "horizontal")
    print(f"Can place 'DOG' immediately after 'CAT': {can_place}")
    
    if can_place:
        print("❌ PROBLEM: Adjacent words allowed - this could create visual extension!")
        print("Current grid after CAT:")
        for r in range(3, 8):
            print(f"Row {r}: ", end="")
            for c in range(8):
                print(f"{grid[r][c]} ", end="")
            print()
        
        generator._place_word(grid, "DOG", 5, 5, "horizontal") 
        print("After adding DOG:")
        for r in range(3, 8):
            print(f"Row {r}: ", end="")
            for c in range(8):
                print(f"{grid[r][c]} ", end="")
            print()
    else:
        print("βœ… Correctly prevented adjacent word placement")
    
    # Test case 2: Check gap requirement
    can_place_with_gap = generator._can_place_word(grid, "DOG", 5, 6, "horizontal")
    print(f"Can place 'DOG' with 1-cell gap after 'CAT': {can_place_with_gap}")

if __name__ == "__main__":
    analyze_visual_word_extensions()