I am writing a python script that looks for all possible combinations of a given string of letters and then look them up in an English dictionary to generate words from list of strings.
Example input: roaispnba Example output: soap brain
This is the code I got for now:
import sys
import itertools
list_of_letters = sys.argv[2].lower()
def iterator(list_of_letters):
for word in itertools.permutations( list_of_letters ):
output = ''.join(word)
with open('words_alpha.txt') as file:
for line in file:
if output in line:
print line
When I give it a short string (e.g. niarb) then it will look search in the dictionary and come up with the word "brain". However, it is not be able to separate the two words "soap" and "brain" when I give it the whole string.
Please note that what I need is:
- Get all possible combinations of the random string
- See if any of the combinations will result in an English word or maybe to or three
So, two questions:
- How can I make such code able to find the two separate words
- This code probably look horrible and inefficient for programmers, any advice in doing such a task in a better/neater way?
Thaaanks!
Note:
Here is a similar question on SO but in C#: Find words in wordlist from random string of characters
Update:
Here is the link for the dictionary used (Github): https://github.com/dwyl/english-words/blob/master/words_alpha.txt
and here is a sample:
braies
brayette
braying
brail
brailed
brailing
braille
brailled
brailler
brailles
braillewriter
brailling
braillist
brails
brain
brainache
braincap
braincase
brainchild