This project is an AI-based crossword puzzle generator using Python 3.12. The AI solves constraint satisfaction problems (CSP) to generate complete crossword puzzles based on a given structure and a set of words.
The task is to generate a crossword puzzle by selecting appropriate words from a given vocabulary and filling them into a specified crossword structure. The AI solves this problem by enforcing constraints such as word lengths, shared letter placements, and ensuring word uniqueness.
-
Crossword Structure:
- The crossword structure is a grid where certain squares are filled with letters, while others remain blank. The structure is defined in a text file (
structure.txt
).
- The crossword structure is a grid where certain squares are filled with letters, while others remain blank. The structure is defined in a text file (
-
Vocabulary:
- A list of words (provided in a text file
words.txt
) is used to populate the crossword puzzle.
- A list of words (provided in a text file
-
Constraint Satisfaction Problem (CSP):
- The puzzle is modeled as a CSP with variables representing each sequence of squares (either across or down) that need to be filled.
- Unary constraints ensure the selected word has the correct length.
- Binary constraints ensure overlapping words share the same letter at intersecting squares.
crossword.py
: Defines the core functionality for managing the crossword puzzle structure and variables.generate.py
: Contains the AI logic that solves the crossword puzzle using CSP techniques.
- Clone the repo:
git clone https://github.com/musty-ess/AI-Crossword-Puzzle-Generator.git
- Install Requirements:
pip install pillow
- Run the Program:
python generate.py data/structure1.txt data/words1.txt output.png
This will create a crossword puzzle image (output.png) based on the specified structure and vocabulary.
Inside generate.py
-
enforce_node_consistency()
Enforces node consistency by ensuring that every value (word) in the domain of a variable has the correct length for that variable. -
revise(x, y)
Ensures arc consistency by checking if there is a valid word for variablex
that doesn't conflict with any word in the domain of variabley
. -
ac3()
Enforces arc consistency across the puzzle by processing arcs between all neighboring variables. -
assignment_complete()
Checks if the puzzle is complete by verifying that every variable is assigned a word. -
consistent()
Checks if the current assignment is consistent, i.e., all values are of the correct length, there are no conflicts in overlaps, and all words are unique. -
order_domain_values(var)
Returns a list of possible values for a variable, ordered according to the least-constraining values heuristic. -
select_unassigned_variable()
Selects an unassigned variable, prioritizing variables with the fewest remaining values and most neighbors (degree heuristic). -
backtrack()
Uses backtracking search to solve the puzzle by trying different assignments and inferring potential solutions with constraints.
When you run the program, an example of the output is the following: python generate.py data/structure1.txt data/words1.txt output.png
The program will output a crossword puzzle saved as output.png.
This project demonstrates how to use AI techniques like constraint satisfaction, backtracking, and arc consistency to generate crossword puzzles.