From 0d7344410053d6b849fe436562e479cfd5eeb3bb Mon Sep 17 00:00:00 2001 From: Stack-of-Pancakes Date: Sun, 29 Apr 2018 14:46:32 -0400 Subject: [PATCH 1/3] Revert "Deleted" This reverts commit 1bf03415c1892b4f3e57aecea8948c86a6cd5d8f. --- look_w.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 look_w.py diff --git a/look_w.py b/look_w.py new file mode 100644 index 0000000..e5b2ae6 --- /dev/null +++ b/look_w.py @@ -0,0 +1,46 @@ +import itertools, 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 +wor = open("words.txt","r") +wor = [x[:-1] for x in wor] + +# Looking words that in the words list +# by number of letters and alphabets +def look_w(word,num): + + # Checking total num against the total letters of word. + if num <= len(word): + + # Getting words list according to number of letters + mx = [wor[x] for x in range(len(wor)) if len(wor[x]) == num] + + # Listing all words that relate to the word's letters. + a = [] + b = {word[c]:word.count(word[c]) for c in range(len(word))} + for ele in mx: + elec = ele.lower() + d = {elec[c]:elec.count(elec[c]) for c in range(len(elec))} + z=[] + for i in d: + try: + v = len(d) + if d[i] <= b[i]: + z.append('ok') + except: + pass + finally: + if v == len(z): + a.append("".join(elec)) + + # Sorted the words list + a = sorted(list(set(a))) + return a + else: + return "⚔ Exceeding total letters ⚔".upper() From 0c183319b285b45549364d2dbf87f85939828874 Mon Sep 17 00:00:00 2001 From: Stack-of-Pancakes Date: Sun, 29 Apr 2018 14:47:01 -0400 Subject: [PATCH 2/3] Simplify looking for similar words Saw your pull request: https://github.com/dwyl/english-words/pull/38 Wanted to show you there was a simpler way to accomplish what you were doing. Also this solution returns words in the original case. --- look_w.py | 54 ++++++++++++------------------------------------------ 1 file changed, 12 insertions(+), 42 deletions(-) diff --git a/look_w.py b/look_w.py index e5b2ae6..dc654e5 100644 --- a/look_w.py +++ b/look_w.py @@ -1,46 +1,16 @@ -import itertools, os -from pathlib import Path +# Open words.txt. Assumes words.txt is in same folder as look_w.py +words = open('words.txt').read().split() -# 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) +def look_w(word, num): + # Looking words that in the words list + # by number of letters and alphabets + 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())] -# Open words.txt -wor = open("words.txt","r") -wor = [x[:-1] for x in wor] -# Looking words that in the words list -# by number of letters and alphabets -def look_w(word,num): - - # Checking total num against the total letters of word. - if num <= len(word): - - # Getting words list according to number of letters - mx = [wor[x] for x in range(len(wor)) if len(wor[x]) == num] - - # Listing all words that relate to the word's letters. - a = [] - b = {word[c]:word.count(word[c]) for c in range(len(word))} - for ele in mx: - elec = ele.lower() - d = {elec[c]:elec.count(elec[c]) for c in range(len(elec))} - z=[] - for i in d: - try: - v = len(d) - if d[i] <= b[i]: - z.append('ok') - except: - pass - finally: - if v == len(z): - a.append("".join(elec)) - - # Sorted the words list - a = sorted(list(set(a))) - return a - else: - return "⚔ Exceeding total letters ⚔".upper() +# Usage +print(look_w('insane', 6)) # prints ['inanes', 'insane', 'sienna'] +print(look_w('quit', 3)) # prints ['ITU', 'qui', 'Tiu', 'tui', 'UIT', 'uti'] +print(look_w('quit', 4)) # prints ['quit'] +print(look_w('cake', 99)) # prints [] From c0fba7631f25d1ef61fe2fb7530bca0ba14be61e Mon Sep 17 00:00:00 2001 From: Stack-of-Pancakes Date: Sun, 29 Apr 2018 15:24:23 -0400 Subject: [PATCH 3/3] Refactor python code example sys imported and unused. os only used to create cwd which is implied with open try/except block that doesn't handle anything json loaded to create a more expensive defaultdict dict created just for membership testing instead of set membership test performed by fetching key value instead of using in --- read_english_dictionary.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/read_english_dictionary.py b/read_english_dictionary.py index 18efb35..dcf11d4 100644 --- a/read_english_dictionary.py +++ b/read_english_dictionary.py @@ -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)