From eb3cc0b1291d03c4d49b5d30870869fc5170d3e2 Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Wed, 28 Feb 2018 10:13:25 +0700 Subject: [PATCH 01/16] Look for Words within a Word Searching words that valid in English-words.txt that contain letters from the word that you search. --- look_w.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 look_w.py diff --git a/look_w.py b/look_w.py new file mode 100644 index 0000000..47b4c29 --- /dev/null +++ b/look_w.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Feb 23 13:48:31 2018 + +@author: karja +""" + +import itertools, os +os.chdir("C:\\Users\\karja") + +# 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): + + # Getting words list according to number of letters + mx = [wor[x] for x in range(len(wor)) if len(wor[x]) == num] + + # Create list of words that using itertools from the letters + # and 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 itertools.product(word,repeat = num): + d = {ele[c]:ele.count(ele[c]) for c in range(len(ele))} + 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(ele)) + + # Matching the created list words with the words in words.txt + # and gather the the valid words + a = [a[i] for i in range(len(a)) if a[i] in wor or a[i].capitalize() in wor] + return a From 0d6655ed38056fedf6b8225499f435b16885da23 Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Wed, 28 Feb 2018 10:30:35 +0700 Subject: [PATCH 02/16] Look for Words within a Word Update little and add comment. --- look_w.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/look_w.py b/look_w.py index 47b4c29..92af3e8 100644 --- a/look_w.py +++ b/look_w.py @@ -6,7 +6,9 @@ """ import itertools, os -os.chdir("C:\\Users\\karja") + +# locate your folder that contain words.txt +os.chdir("C:\\Users\\user") # Open words.txt wor = open("words.txt","r") From e441942c1f8fbe741a375708374011769da9ce01 Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Thu, 1 Mar 2018 09:44:57 +0700 Subject: [PATCH 03/16] Look for Words within a Word Update on replacing variable that use in local instead of global. --- look_w.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/look_w.py b/look_w.py index 92af3e8..d2fc493 100644 --- a/look_w.py +++ b/look_w.py @@ -19,7 +19,7 @@ def look_w(word,num): # Getting words list according to number of letters - mx = [wor[x] for x in range(len(wor)) if len(wor[x]) == num] + mx = [wor[x] for x in range(len(wor)) if len(wor[x]) <= num] # Create list of words that using itertools from the letters # and listing all words that relate to the word's letters. @@ -41,5 +41,5 @@ def look_w(word,num): # Matching the created list words with the words in words.txt # and gather the the valid words - a = [a[i] for i in range(len(a)) if a[i] in wor or a[i].capitalize() in wor] + a = [a[i] for i in range(len(a)) if a[i] in mx or a[i].capitalize() in mx] return a From 807c2953447d5ccf6f3e6710d75d7967537f5616 Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Thu, 1 Mar 2018 12:16:37 +0700 Subject: [PATCH 04/16] Look for Words within a Word Small fix, so valid missing words are collected as well --- look_w.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/look_w.py b/look_w.py index d2fc493..43b7870 100644 --- a/look_w.py +++ b/look_w.py @@ -1,10 +1,3 @@ -# -*- coding: utf-8 -*- -""" -Created on Fri Feb 23 13:48:31 2018 - -@author: karja -""" - import itertools, os # locate your folder that contain words.txt @@ -31,7 +24,7 @@ def look_w(word,num): for i in d: try: v = len(d) - if d[i] == b[i]: + if d[i] <= b[i]: z.append('ok') except: pass From 650b03c0294f1b4c7b8ec1f8823896a0c3742bab Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Fri, 2 Mar 2018 10:52:53 +0700 Subject: [PATCH 05/16] Look for Words wthin a Word remove duplicate words --- look_w.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/look_w.py b/look_w.py index 43b7870..c149791 100644 --- a/look_w.py +++ b/look_w.py @@ -32,7 +32,7 @@ def look_w(word,num): if v == len(z): a.append("".join(ele)) - # Matching the created list words with the words in words.txt - # and gather the the valid words - a = [a[i] for i in range(len(a)) if a[i] in mx or a[i].capitalize() in mx] + # Matching the created list words with the words in MX + # (words list from words.txt) and gather the valid words + a = list(set(a[i] for i in range(len(a)) if a[i] in mx or a[i].capitalize() in mx])) return a From ee899dacd4aa0c6ea1b4abb469320b1c77cd385c Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Fri, 2 Mar 2018 10:55:25 +0700 Subject: [PATCH 06/16] Look for Words within a Word fix little bug --- look_w.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/look_w.py b/look_w.py index c149791..a3b943d 100644 --- a/look_w.py +++ b/look_w.py @@ -34,5 +34,5 @@ def look_w(word,num): # Matching the created list words with the words in MX # (words list from words.txt) and gather the valid words - a = list(set(a[i] for i in range(len(a)) if a[i] in mx or a[i].capitalize() in mx])) + a = list(set(a[i] for i in range(len(a)) if a[i] in mx or a[i].capitalize() in mx)) return a From eee4cc81c128eafb7133e97c09491a20a410fea5 Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Fri, 2 Mar 2018 11:08:06 +0700 Subject: [PATCH 07/16] Look for Words within a word The list of words sorted. --- look_w.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/look_w.py b/look_w.py index a3b943d..89ec306 100644 --- a/look_w.py +++ b/look_w.py @@ -34,5 +34,5 @@ def look_w(word,num): # Matching the created list words with the words in MX # (words list from words.txt) and gather the valid words - a = list(set(a[i] for i in range(len(a)) if a[i] in mx or a[i].capitalize() in mx)) + a = sorted(list(set(a[i] for i in range(len(a)) if a[i] in mx or a[i].capitalize() in mx))) return a From 92532f30a7eaa8e4c01e6cd0b42ef7c54a5b5c10 Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Sun, 4 Mar 2018 11:01:30 +0700 Subject: [PATCH 08/16] Look for Words within a Word Get users default (home) directory --- look_w.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/look_w.py b/look_w.py index 89ec306..fb6cf47 100644 --- a/look_w.py +++ b/look_w.py @@ -1,7 +1,11 @@ import itertools, os +from pathlib import Path -# locate your folder that contain words.txt -os.chdir("C:\\Users\\user") +# 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") From 22c31332dd2e8eba1d9b69db46422c742c29b1de Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Wed, 21 Mar 2018 14:50:41 +0700 Subject: [PATCH 09/16] Look for Words within a Word (speedy result). Fix the speed. Faster result even with long words. --- look_w.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/look_w.py b/look_w.py index fb6cf47..ef2d758 100644 --- a/look_w.py +++ b/look_w.py @@ -16,14 +16,14 @@ def look_w(word,num): # Getting words list according to number of letters - mx = [wor[x] for x in range(len(wor)) if len(wor[x]) <= num] + mx = [wor[x] for x in range(len(wor)) if len(wor[x]) == num] - # Create list of words that using itertools from the letters - # and listing all words that relate to the word's letters. + # 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 itertools.product(word,repeat = num): - d = {ele[c]:ele.count(ele[c]) for c in range(len(ele))} + 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: @@ -34,9 +34,8 @@ def look_w(word,num): pass finally: if v == len(z): - a.append("".join(ele)) + a.append("".join(elec)) - # Matching the created list words with the words in MX - # (words list from words.txt) and gather the valid words - a = sorted(list(set(a[i] for i in range(len(a)) if a[i] in mx or a[i].capitalize() in mx))) + # Sorted the words list + a = sorted(list(set(a))) return a From 4ffbb33824cca68f1906808513cffb0f61ea6de6 Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Wed, 21 Mar 2018 19:45:52 +0700 Subject: [PATCH 10/16] Look for Words within a Word Checking total num against the total letters of word. --- look_w.py | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/look_w.py b/look_w.py index ef2d758..e5b2ae6 100644 --- a/look_w.py +++ b/look_w.py @@ -15,27 +15,32 @@ # by number of letters and alphabets def look_w(word,num): - # 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 + # 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 1bf03415c1892b4f3e57aecea8948c86a6cd5d8f Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Thu, 19 Apr 2018 06:00:40 +0700 Subject: [PATCH 11/16] Deleted --- look_w.py | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 look_w.py diff --git a/look_w.py b/look_w.py deleted file mode 100644 index e5b2ae6..0000000 --- a/look_w.py +++ /dev/null @@ -1,46 +0,0 @@ -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 0d7344410053d6b849fe436562e479cfd5eeb3bb Mon Sep 17 00:00:00 2001 From: Stack-of-Pancakes Date: Sun, 29 Apr 2018 14:46:32 -0400 Subject: [PATCH 12/16] 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 13/16] 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 14/16] 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) From 94d8e0e7fceb07da164487359cdbdfc82ee60490 Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Wed, 2 May 2018 13:37:51 +0700 Subject: [PATCH 15/16] Revert some existing one --- look_w.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/look_w.py b/look_w.py index dc654e5..f979bf2 100644 --- a/look_w.py +++ b/look_w.py @@ -1,16 +1,23 @@ -# Open words.txt. Assumes words.txt is in same folder as look_w.py -words = open('words.txt').read().split() +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) -def look_w(word, num): +# 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 - 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())] - + if num <= len(word): + 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'] -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 aa6a7c20044d95b6209f06189c8feb424a9a3c2a Mon Sep 17 00:00:00 2001 From: KarjaKAK Date: Fri, 4 May 2018 13:54:42 +0700 Subject: [PATCH 16/16] Updated Code --- look_w.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/look_w.py b/look_w.py index f979bf2..66f7a96 100644 --- a/look_w.py +++ b/look_w.py @@ -13,7 +13,7 @@ def look_w(word,num): # Looking words that in the words list # by number of letters and alphabets - if num <= len(word): + 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: