Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import nltk
- import regex
- import re
- nltk.download('wordlist')
- from nltk.corpus import words
- word_list = []
- # for word in words.words():
- # if word.isalpha() and len(word) >= 4:
- # word_list.append(word)
- #
- # print(word_list)
- def get_hint(known_pattern, guessed_letters):
- pattern = "^" + known_pattern.replace("_", ".") + "$"
- regex = re.compile(pattern)
- possible_words = []
- for word in word_list:
- if len(word) == len(known_pattern) and regex.match(word):
- possible_words.append(word)
- letter_count = {}
- for word in possible_words:
- for letter in set(word):
- if letter not in guessed_letters and letter != "_":
- letter_count[letter] = letter_count.get(letter) + 1
- if letter_count:
- return max(letter_count, key=letter_count.get)
- return None
- def hangman_with_ai():
- word_to_guess = random.choice(word_list)
- attemps = 6
- print("Welcome to Hangman!")
- print(f"The word has {len(word_to_guess)} letters!")
- print("Start guessing: ")
- while attemps > 0:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement