filter words

This commit is contained in:
Andros Fenollosa 2016-11-26 17:12:18 +01:00
parent d2501f3e29
commit 2b7ba61912

View File

@ -3,26 +3,63 @@
import operator import operator
import os import os
words = open('words.txt') # Start Functions
def clear(): def clear():
'''
Clear console
'''
os.system('cls' if os.name=='nt' else 'clear') os.system('cls' if os.name=='nt' else 'clear')
def print_words(pos=False):
'''
Print words resolves
Arguments:
pos - View numbers
'''
print_pos = ' '
print_pos_bar = ' '
print_pos_letters = ' '
for i in range(num_letters):
if pos:
print_pos = print_pos + str(i + 1) + ' '
print_pos_bar = print_pos_bar + '| '
print_pos_letters = print_pos_letters + word_resolve[i] + ' '
print(print_pos)
print(print_pos_bar)
print(print_pos_letters)
print('')
print('')
def best_letter(word_resolve): def best_letter(word_resolve):
'''
Search best letter
Arguments:
word_resolve - list with word resolve
'''
# Read all words and count letters # Read all words and count letters
letters = dict() letters = dict()
ignore = ('\xb1', '\xc3', '\xb6', '.', '\xae') ignore = ('\xb1', '\xc3', '\xb6', '.', '\xae')
words = open('words.txt')
# Read alls words for dictionary # Read alls words for dictionary
for word in words.readlines(): for word in words.readlines():
# Just consider which have the same length # Just consider which have the same length
if len(word_resolve) == len(word): if len(word_resolve) == len(word.strip()):
l_word = list(word.strip().lower()) l_word = list(word.strip().lower())
# Check word_resolve is same letters
fit = True
for i in range(len(l_word)):
if word_resolve[i] != l_word[i] and word_resolve[i] != '_':
fit = False
# Count the letters # Count the letters
for letter in l_word: if fit:
if letter.lower() not in ignore: for letter in l_word:
if letter in letters: if letter.lower() not in ignore:
letters[letter] = letters[letter] + 1 if letter in letters:
else: letters[letter] = letters[letter] + 1
letters[letter] = 1 elif not letter in letters_used:
letters[letter] = 1
# Sort # Sort
sorted_letters = sorted( sorted_letters = sorted(
letters.items(), key=operator.itemgetter(1), reverse=True letters.items(), key=operator.itemgetter(1), reverse=True
@ -31,6 +68,9 @@ def best_letter(word_resolve):
# Best letter # Best letter
return sorted_letters[0][0] return sorted_letters[0][0]
# End Functions
# Start
clear() clear()
print('Hangman Bot 1.0v') print('Hangman Bot 1.0v')
print(' _________ ') print(' _________ ')
@ -44,6 +84,7 @@ print('| ')
# Get num letters and make list resolve # Get num letters and make list resolve
num_letters = input('Number of letters: ') num_letters = input('Number of letters: ')
clear() clear()
play = True
word_resolve = list() word_resolve = list()
letters_used = list() letters_used = list()
for pos in range(num_letters): for pos in range(num_letters):
@ -52,13 +93,14 @@ for pos in range(num_letters):
print('') print('')
print('Okay, come on!') print('Okay, come on!')
print('') print('')
print('')
# Logic # Logic
while True: while play:
# Get best letter # Get best letter
best_option = best_letter(word_resolve) best_option = best_letter(word_resolve)
# The guard not to give it back # The guard not to give it back
letters_used.append(best_option) letters_used.append(best_option)
print_words()
# Print best letter # Print best letter
print('Test with the letter> {letter}'.format( print('Test with the letter> {letter}'.format(
letter=best_option.upper() letter=best_option.upper()
@ -68,20 +110,22 @@ while True:
question_success = raw_input('I successful? (yes o no): ').lower() question_success = raw_input('I successful? (yes o no): ').lower()
clear() clear()
if question_success == 'no': if question_success == 'no':
clear()
print('') print('')
print('Ups!') print('Ups!')
print('')
print('')
elif question_success == 'yes': elif question_success == 'yes':
print_pos = ' '
print_pos_bar = ' '
print_pos_letters = ' '
for i in range(num_letters):
print_pos = print_pos + str(i + 1) + ' '
print_pos_bar = print_pos_bar + '| '
print_pos_letters = print_pos_letters + word_resolve[i] + ' '
print(print_pos)
print(print_pos_bar)
print(print_pos_letters)
print('') print('')
good_pos = raw_input('Tell me that positions (Example> 2 4 7): ') print_words(True)
good_pos = raw_input('Tell me that positions (Example> 2 4 7): ').split(' ')
clear()
for pos in good_pos:
word_resolve[int(pos) - 1] = best_option
# Game over
end = True
for letter in word_resolve:
if letter != '_':
end = False
if end:
play = False
print('Game over :)')