Skip to content

Instantly share code, notes, and snippets.

@victor-iyi
Created July 7, 2017 12:00
Show Gist options
  • Save victor-iyi/1043af03ece4cfa4787178a371413cfb to your computer and use it in GitHub Desktop.
Save victor-iyi/1043af03ece4cfa4787178a371413cfb to your computer and use it in GitHub Desktop.
Search list of reasonable words in a scrambled collection of letter
from nltk.corpus import wordnet
import threading
from queue import Queue
from datetime import datetime
import time
class WordSearch:
def __init__(self):
self.combo = set()
def permutate(self, seq):
if not seq: return [seq] # is an empty sequence
temp = []
for k in range(len(seq)):
part = seq[:k] + seq[k+1:]
#print('k=',k,'Part=',part)
for m in self.permutate(part):
temp.append(seq[k:k+1] + m)
#print(m, seq[k:k+1])
self.combo.add(m+seq[k:k+1])
return temp
##print(permutate('vic'))
##print(len(permutate('vic')))
##print(self.combo)
##print('length of combo', len(self.combo))
def findWord(self, sequence, result_length):
possible_words = self.permutate(sequence)
outcome = set()
print('Starting...')
for p in self.combo:
try:
syns = wordnet.synsets(p)
if len(syns) > 0:
if len(p) == result_length:
outcome.add(p)
continue
except Exception as e:
continue
return sorted(outcome)
if __name__ == '__main__':
userInput = input('\n\nEnter the scrambled word: ')
word_length = int(input('Enter the length of the word you need: '))
search = WordSearch()
print('Started:',
datetime.fromtimestamp(time.time()).strftime('%I:%M:%S %p'))
outcome = search.findWord(userInput, word_length)
print(outcome)
print('End:',
datetime.fromtimestamp(time.time()).strftime('%I:%M:%S %p'))
####search = WordSearch()
####print(search.permutate('ope'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment