Skip to content

Commit

Permalink
Ready for building
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenewald committed Jan 2, 2022
1 parent e5fd42e commit 9bd595a
Show file tree
Hide file tree
Showing 30 changed files with 196 additions and 79 deletions.
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[build-system]
requires = [
"setuptools>=42",
"wheel"
"wheel",
"numpy",
"tqdm",
"opencv-python",
]
build-backend = "setuptools.build_meta"
Empty file removed setup.cfg
Empty file.
33 changes: 33 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import setuptools

#with open("README.md", "r", encoding="utf-8") as fh:
# long_description = fh.read()

from unittest import TestLoader
def my_test_suite():
test_loader = TestLoader()
test_suite = test_loader.discover('tests', pattern='test_*.py')
return test_suite
if(__name__=='__main__'):
setuptools.setup(
name="pyradix-stevenewald",
version="0.0.1",
author="Steven Ewald",
author_email="[email protected]",
description="Python implementation of Radix Tree: memory and speed optimized language detection",
long_description="",
long_description_content_type="text/markdown",
url="https://github.com/stevenewald/pyradix",
project_urls={
"Bug Tracker": "https://github.com/stevenewald/pyradix/issues",
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
package_dir={"": "src"},
packages=setuptools.find_packages(where="src"),
python_requires=">=3.6",
test_suite='setup.my_test_suite',
)
Binary file removed src/py-radix/__pycache__/image_utils.cpython-310.pyc
Binary file not shown.
Binary file removed src/py-radix/__pycache__/radix_utils.cpython-310.pyc
Binary file not shown.
Binary file removed src/py-radix/__pycache__/tree_utils.cpython-310.pyc
Binary file not shown.
Binary file not shown.
48 changes: 23 additions & 25 deletions src/py-radix/__init__.py → src/pyradix/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import image_utils as iu
import tree_utils as tu
import radix_utils as ru
import wordcheck_utils as wordcheck #not used currently but useful
import time
from pyradix.image_utils import *
from pyradix.tree_utils import *
from pyradix.radix_utils import *
from pyradix.wordcheck_utils import *
#from english_words import english_words_lower_alpha_set
#from threading import Thread
import numpy as np
import cv2

def initArray(wordlist):
u = 0
Expand Down Expand Up @@ -40,36 +37,37 @@ def validWords(it, tn, accumlet, accum, i, z, moves, accumpos, arr):
if(lett[0:1]==lettF):
tempmoves = moves.copy()
tempmoves.append(let)
pos = let
posns = [let]
if(len(lett)>1):
pos = returnComb(lett, possibleLetters(let[0], let[1], tempmoves), 1, tempmoves, arr)
if(pos[0]>-1 and pos[1]>-1):
if(nodes.isword):
accum.append(accumlet+lett)
if(len(accumlet+lett)>2):
accumpos.append((tempmoves, accumlet+lett))
validWords(it-1, nodes, accumlet+lett, accum, pos[0], pos[1], tempmoves, accumpos, arr)
posns = []
returnComb(lett, possibleLetters(let[0], let[1], tempmoves), 1, tempmoves, arr, posns)
for pos in posns:
if(pos[0]>-1 and pos[1]>-1):
if(nodes.isword):
accum.append(accumlet+lett)
if(len(accumlet+lett)>2):
accumpos.append((tempmoves, accumlet+lett))
validWords(it-1, nodes, accumlet+lett, accum, pos[0], pos[1], tempmoves, accumpos, arr)
return (accum, accumpos)

def returnComb(word, PL, it, moves, arr):
def returnComb(word, PL, it, moves, arr, posns):
if(it>(len(word))):
return (-1, -1)
return
lett_to_find = word[it:it+1]
for let in PL:
newL = arr[let[0]][let[1]]
if(newL==lett_to_find):
moves.append(let)
if(it==(len(word)-1)):
return let
posns.append(let)
else:
return returnComb(word, possibleLetters(let[0], let[1], moves), it+1, moves, arr)
return (-1, -1)
returnComb(word, possibleLetters(let[0], let[1], moves), it+1, moves, arr, posns)
def sortByLen(e):
return len(e[1])
def finalWords(showImages, wordlist): #alphabet array
def wordPathfinding(showImages, topnode, wordlist): #alphabet array
arr = initArray(wordlist)
if(showImages):
bg = iu.create_imgs(wordlist)
bg = create_imgs(wordlist)
words = []
frames = []
wordl = []
Expand All @@ -80,7 +78,7 @@ def finalWords(showImages, wordlist): #alphabet array
for uvi in uv:
if(showImages):
uvi[0].append((i2,z2))
var = iu.create_arrows(bg, uvi[0], uvi[1])
var = create_arrows(bg, uvi[0], uvi[1])
if(wordl.count(var[1])<1):
frames.append((var[0], var[1]))
wordl.append(var[1])
Expand All @@ -103,7 +101,7 @@ def finalWords(showImages, wordlist): #alphabet array
k+=1
otl = np.unique(otl).tolist()
otl.sort(key=len)
if(len(otl)>15): # reduce length of list
otl = otl[len(otl)-15:len(otl)]
#if(len(otl)>15): # reduce length of list
#otl = otl[len(otl)-15:len(otl)]

return otl
Binary file added src/pyradix/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file added src/pyradix/__pycache__/radix_utils.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion src/py-radix/image_utils.py → src/pyradix/image_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import cv2


def create_arrows_sub(src2, moves2, word):
frames = []
currentImg = np.zeros((560, 560, 3), np.uint8)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/py-radix/radix_utils.py → src/pyradix/radix_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import tree_utils as tu
import pyradix.tree_utils as tu

def optimize_tree(tree):
anyReduxed = False
Expand Down
13 changes: 11 additions & 2 deletions src/py-radix/tree_utils.py → src/pyradix/tree_utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import copy
class node:
def __init__(self, type, isword, subnodes):
self.type = type
self.subnodes = subnodes
self.isword = isword

def words_to_tree(file_location):
def copy(self):
copied = copy.deepcopy(self)
return copied

def words_to_tree(dict):
topnode = node(str,False,[])
dict = get_dict(file_location)
for word in dict:
if(word==""):
continue
topnode.subnodes = create_new_node(word, 1, topnode.subnodes)
return topnode

def add_word(topnode, word): #DOES NOT WORK WITH RADIX TREE
if(len(word)>0):
topnode.subnodes = create_new_node(word, 1, topnode.subnodes)

def create_new_node(word, letters, subnodes):
sub2 = find_index(subnodes,word, letters)
if(len(word)==letters):
Expand Down Expand Up @@ -41,6 +49,7 @@ def get_dict(file_location):
file = open(file_location, "r") #or use english3.txt for smaller dictionary
content = file.read()
dict = content.split("\n")
file.close()
return dict

def count_nodes(topnode):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import tree_utils as tu
import radix_utils as ru
import pyradix.tree_utils as tu
import pyradix.radix_utils as ru
def check_if_word(node, word, radix):
if(word==""):
return True
return check_if_word_sub(word, node, 1, radix)

def check_if_word_sub(word, node, letters, radix): #actually not used in new version, but useful regardless
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions src/pyradix_stevenewald.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Metadata-Version: 2.1
Name: pyradix-stevenewald
Version: 0.0.1
Summary: Python implementation of Radix Tree: memory and speed optimized language detection
Home-page: https://github.com/stevenewald/pyradix
Author: Steven Ewald
Author-email: [email protected]
License: UNKNOWN
Project-URL: Bug Tracker, https://github.com/stevenewald/pyradix/issues
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown

UNKNOWN

12 changes: 12 additions & 0 deletions src/pyradix_stevenewald.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pyproject.toml
setup.py
src/pyradix/__init__.py
src/pyradix/image_utils.py
src/pyradix/radix_utils.py
src/pyradix/tree_utils.py
src/pyradix/wordcheck_utils.py
src/pyradix/wordgame_radix_footage.py
src/pyradix_stevenewald.egg-info/PKG-INFO
src/pyradix_stevenewald.egg-info/SOURCES.txt
src/pyradix_stevenewald.egg-info/dependency_links.txt
src/pyradix_stevenewald.egg-info/top_level.txt
1 change: 1 addition & 0 deletions src/pyradix_stevenewald.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions src/pyradix_stevenewald.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyradix
Binary file added tests/__pycache__/test_use_case.cpython-310.pyc
Binary file not shown.
Binary file added tests/__pycache__/test_word_check.cpython-310.pyc
Binary file not shown.
29 changes: 29 additions & 0 deletions tests/test_use_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
import time
import pyradix
from tqdm import tqdm
class testUseCase(unittest.TestCase):
def test_usecase1(self):
dict1 = pyradix.get_dict("C:/Users/steve/Desktop/wordgame_nongit/Dictionaries/english_large.txt")
with tqdm(total=2) as pbar:
pbar.set_description("Loading test trees")
pbar.update(1)
radix = pyradix.words_to_tree(dict1)
pyradix.optimize_tree(radix)
pbar.update(2)
pbar.close()
wordlist = "abcdefghijklmnop"
words = pyradix.wordPathfinding(False, radix, wordlist)
words = words[len(words)-15:len(words)]
self.assertGreater(len(words), 10)
nonradix = pyradix.words_to_tree(dict1)
wordlistx = ["testing", "gjkajkgr", "sgrlkgsjns", "egjrgjrdgk", "w2rs", "1", "", "klgksl", "test", "something", "apdpdfn"]
for word in wordlistx:
pyradix.add_word(nonradix, word)
for word in wordlistx:
val = pyradix.check_if_word(nonradix, word, False)
self.assertTrue(val)


if __name__ == '__main__':
unittest.main()
106 changes: 59 additions & 47 deletions tests/test_word_check.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,59 @@
topnode = tu.words_to_tree("C:/Users/steve/Desktop/wordgame_nongit/Dictionaries/english_small.txt")
wordlist = "abcdefghijklmnop"
triesize = str(tu.count_nodes(topnode))
startTime = time.time()
pw1 = finalWords(False, wordlist)
endTime = time.time()
topnode = ru.optimize_tree(topnode)
st2 = time.time()
pw2 = finalWords(True, wordlist)
et2 = time.time()
nodes2 = tu.count_nodes(topnode)
ext1 = ((endTime-startTime)*1000)
ext2 = ((et2-st2)*1000)
sizechange = abs(10000*(nodes2-int(triesize))/int(triesize))
timechange = (10000*(ext2-ext1)/ext1)
print("Trie vertices: " + triesize)
print("Trie execution time: " + str(round(ext1)/1000) + " seconds")
print("=========================")
print("Radix tree vertices: " + str(nodes2))
print("Radix tree execution time: " + str(round(ext2)/1000) + " seconds")
print("=========================")
print("Changes from implementing radix tree")
print("Reduction in size: -" + str(round(sizechange)/100) + "%")
if(timechange>0):
print("Increase in execution time: +" + str(round(abs(timechange))/100) + "%")
else:
print("Decrease in execution time: -" + str(round(abs(timechange))/100) + "%")
print("=========================")
print(pw1)
if(False): #only if testing words
numvw = 0
for word in dict:
word2 = word + "e"
check1 = word2 in dict
check2 = check_if_word(word2, True)
if(check1 and (not check2)):
print(word)
elif((not check1) and check2):
print(word)
numvw+=1
if(numvw%1000==0):
print(numvw)
print("Word check done")
if(not(pw1==pw2)):
print("ERROR: TRIE AND RADIX TREE UNEQUAL")
print(len(pw1))
print(len(pw2))
import unittest
import time
import pyradix
from tqdm import tqdm
class testWordCheck(unittest.TestCase):
dict1 = pyradix.get_dict("C:/Users/steve/Desktop/wordgame_nongit/Dictionaries/english_large.txt")
wordlistF = ["ttufpaepnerignss", "ponmlkjihgfedcba", "ciolmhrfefednslo", "abcdefghijklmnop"]
with tqdm(total=2) as pbar:
pbar.set_description("Loading test trees")
nonradix = pyradix.words_to_tree(dict1)
pbar.update(1)
radix = pyradix.words_to_tree(dict1)
pyradix.optimize_tree(radix)
pbar.update(2)
pbar.close()
def test_testvalidity(self):
self.assertGreater(len(self.dict1), 10000)
def test_nonradix_word_equality(self):
with tqdm(total=(len(self.dict1))) as pbar:
pbar.set_description("Testing non-radix validity")
for word in self.dict1:
if(word==""):
continue
val = pyradix.check_if_word(self.nonradix, word, False)
self.assertTrue(val)
pbar.update(1)
pbar.close()
def test_radix_word_equality(self):
with tqdm(total=(len(self.dict1))) as pbar:
pbar.set_description("Testing radix validity")
for word in self.dict1:
if(word==""):
continue
val = pyradix.check_if_word(self.radix, word, True)
self.assertTrue(val)
pbar.update(1)
pbar.close()
def test_result_equality(self):
with tqdm(total=len(self.wordlistF)) as pbar:
pbar.set_description("Testing non-radix and radix equality")
for wordlist in self.wordlistF:
pw1 = pyradix.wordPathfinding(False, self.nonradix, wordlist)
pw2 = pyradix.wordPathfinding(False, self.radix, wordlist)
self.assertEqual(pw1, pw2)
pbar.update(1)
pbar.close()
def test_trie_size(self):
with tqdm(total=2) as pbar:
pbar.set_description("Validating non-radix and radix size")
size = pyradix.count_nodes(self.nonradix)
pbar.update(1)
size2 = pyradix.count_nodes(self.radix)
pbar.update(1)
self.assertEqual(size, 1027814)
self.assertEqual(size2, 478056)
pbar.close()

if __name__ == '__main__':
unittest.main()

0 comments on commit 9bd595a

Please sign in to comment.