def buildBTree(): #will read from file and create a binary tree of words and there embeddings numWords = 0 #foo item used to create the tree outside the loop #item will be deleted once the tree is created fooNode = WN.Word("fooItem 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17") myTree = bst.BST(fooNode) buildStart = dt.now() #1theFile = open('H:\Javis USB\CS2302\lab5\glove.6B.50d.txt', encoding = 'utf-8') theFile = open('G:\Javis USB\CS2302\lab5\glove.6B.50d.txt', encoding='utf-8') #theFile = open('reduced.txt', encoding = 'utf-8') for line in theFile: numWords += 1 tempNode = WN.Word(line) bst.Insert(myTree, tempNode) bst.Delete(myTree, fooNode) buildEnd = dt.now() - buildStart theFile.close() myTreeHeight = bst.MaxHeight(myTree) print() print('Binary Search Tree stats:') print('Number of nodes: ' + str(numWords)) print('max height is: ' + str(myTreeHeight)) print('Running time for binary search tree construction: ' + str(buildEnd)) print() print('Reading word file to determine similarities') print() similarites_bst(myTree)
def readIntoBST(): tree = None with open(embed_file, encoding="utf8") as ef: content = ef.readline() #goes through every line on the file while content: word = content.split() #creates Word Embedding with information from file n = WordEmbedding.WordEmbedding(word[0], word[1:]) #inserts on bst tree = bst.Insert(tree, n) content = ef.readline() print("Done") return tree
def buildBST(filename): try: T = None # file uses utf8 encoding f = open(filename, "r", encoding="utf8") for line in f: # tokenize each line into a list of strings tokens = line.split(" ") # if the value stored at the index begins with an alphabetical letter if tokens[0].isalpha(): T = bst.Insert(T, tokens[0], tokens[1:]) f.close() # close the file to save memory return T # return bst except IOError: print("File", filename, "not found!\n")
TreeToList(T, List) print('Question 4') print('The new list created from a tree is: ') print(*List, sep=', ') print() def Question5(T): print('Question 5') items = [] Height = bst.MaxHeight(T) for i in range(Height): items.append('Key at depth ' + str(i) + ': ') AtDepth(T, 0, items) for i in range(len(items)): print(items[i]) print() T = None A = [70, 50, 90, 130, 80, 40, 60, 10] for a in A: T = bst.Insert(T, a) Question1(T) Question2(T) Question3() Question4(T) Question5(T)