Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for Words within a Word #38

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
23 changes: 23 additions & 0 deletions look_w.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
from pathlib import Path

# Geting users home directory
h_path = str(Path.home()) # + any addtional path to a folder that contain word.txt

# Change directory to h_path where words.txt is located
os.chdir(h_path)

# Open words.txt.
words = open('words.txt').read().split()

def look_w(word,num):
# Looking words that in the words list
# by number of letters and alphabets
if num <= len(word) and num != 0:
return [w for w in words if len(w) == num and
all(w.lower().count(c) <= word.lower().count(c) for c in w.lower())]
else:
return "⚔ Exceeding total letters ⚔".upper()

# Usage
print(look_w('insane', 6)) # prints ['inanes', 'insane', 'sienna']
17 changes: 6 additions & 11 deletions read_english_dictionary.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import json
import os, sys

def load_words():
try:
filename = os.path.dirname(sys.argv[0])+"\\"+"words_dictionary.json"
with open(filename,"r") as english_dictionary:
valid_words = json.load(english_dictionary)
return valid_words
except Exception as e:
return str(e)
with open('words_alpha.txt') as word_file:
valid_words = set(word_file.read().split())

return valid_words


if __name__ == '__main__':
english_words = load_words()
# demo print
print(english_words["fate"])
print('fate' in english_words)