Character-level NLP model for masked letter inference, combined with dictionary-aware heuristics and curriculum-based training.
The pipeline predicts missing characters in partially observed words using contextual sequence features and dictionary priors.
- Generates synthetic partially revealed word states from a list of words.
- Encodes letters as:
0-> blank (_)1..26->a..z27->PAD
- Produces tensors used in training:
word_stateposition_contexttarget_positionstarget_charsword_lengthblank_countnext_to_vowel
- Character embedding + context embedding.
- CNN branch for local pattern extraction.
- BiLSTM encoder for sequence context.
- Position prior MLP for per-position letter prior.
- Three context-specific decoders:
- left context
- right context
- both sides context
- Output shape:
[batch, max_len, 26]logits over lettersa..z.
- Loads
MaskedCharacterPredictorweights from a.pthcheckpoint. - Uses dictionary filtering (
words_250000_train.txt) to maintain letter multipliers. - Combines model output with frequency heuristics.
- Handles cold-start prediction via word-length letter frequency statistics.
-
train_model1(words, epochs=10, early_stopping_patience=5)- Trains model with reveal-ratio curriculum schedule.
- Uses validation loss, LR scheduling, and early stopping.
- Saves best checkpoint to
best_model1.pth.
-
simulate_hangman_game(solver1, solver2, word, max_wrong=6, verbose=False)- Simulates one masked-character inference episode.
- Dynamically switches solver:
solver1when reveal ratio is high, otherwisesolver2. - Returns a result dict with success, guesses, wrong guesses, and final state.
Note: the function name is legacy; behavior is generic masked-character solving.
-
Heuristic helpers:
build_lengthwise_frequenciesget_best_first_guessword_matches_patternget_dictionary_filtered_multipliersclean_state_dict
pip install -r requirements.txtfrom solver import Solver
solver = Solver("best_model1.pth")
guess = solver.predict_letter("_e__a___")
print("Next guess:", guess)from solver import Solver
from utilities import simulate_hangman_game
solver1 = Solver("best_model1.pth")
solver2 = Solver("best_model2.pth")
result = simulate_hangman_game(solver1, solver2, "pattern", verbose=True)- Input pattern:
_ e _ _ a _ _ - Model objective: infer the most probable next character.
- Strategy: combine neural logits with dictionary-constrained multipliers.
Solverexpectswords_250000_train.txtto exist in the project root.- If your checkpoint was saved from DataParallel training,
clean_state_dicthandles prefix cleanup.
This project is licensed under the MIT License. See LICENSE for details.