Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could someone help me figure out how to combine my Python programs? I am writing a word-concordance program that counts the number of words in

Could someone help me figure out how to combine my Python programs?

I am writing a word-concordance program that counts the number of words in a file and then prints the corresponding lines that the word is found on. I also have five different dictionary ADT implementations: ChainingDict, OpenAddrHashDict linear, OpenAddrHashDict quadratic, AVLTree, and Binary_search_tree.

I have the code for each of the dictionary's completed and in their own file along with my word-concordance program in its own file. Now I want to run my word-concordance program and time it for each of the five dictionary ADT implementations. This way I can see how long each dictionary method takes to run my program.

So my question, how would I link my word-concordance program to each one of the dictionary's and test how long it takes to run my program using that specific dictionary? I would like to have just one word-concordance program with 5 pairs of dictionaries with all but one pair commented out, i.e., the dictionary pair I am timing is the only pair uncommented. This way i can just uncomment the dictionary I want to test when needed.

Here is my word-concordance code:

import re from collections import defaultdict

def wordConcordance(): wordConcordanceDict = defaultdict(list)

with open('stop_words.txt') as sw: words = (line.strip() for line in sw) stop_words = set(words)

with open('WarAndPeace.txt') as f: for line_number, line in enumerate(f, 1): words = (re.sub(r'[^\w\s]','',word).lower() for word in line.split()) good_words = (word for word in words if word not in stop_words) for word in good_words: wordConcordanceDict[word].append(line_number)

for word in sorted(wordConcordanceDict): print('{}: {}'.format(word, ' '.join(map(str, wordConcordanceDict[word]))))

wordConcordance()

Here is one of my dictionary's ChainingDict

from entry import Entry from unordered_linked_list import UnorderedList

class ChainingDict(object): """Dictionary implemented using hashing with chaining."""

def __init__(self, capacity = 8): self._capacity = capacity self._table = [] for index in range(self._capacity): self._table.append(UnorderedList()) self._size = 0 self._index = None

def __contains__(self, key): """Returns True if key is in the dictionary or False otherwise.""" self._index = abs(hash(key)) % self._capacity entry = Entry(key, None) return self._table[self._index].search(entry)

def __getitem__(self, key): """Returns the value associated with key or returns None if key does not exist.""" if key in self: entry = Entry(key, None) entry = self._table[self._index].remove(entry) self._table[self._index].add(entry) return entry.getValue() else: return None

def __delitem__(self, key): """Removes the entry associated with key.""" entry = Entry(key, None) if key in self: entry = Entry(key, None) entry = self._table[self._index].remove(entry) self._size -= 1

def __setitem__(self, key, value): """Inserts an entry with key/value if key does not exist or replaces the existing value with value if key exists.""" entry = Entry(key, value) if key in self: entry = self._table[self._index].remove(entry) entry.setValue(value) else: self._size += 1 self._table[self._index].add(entry)

def __len__(self): return self._size

def __str__(self): result = "HashDict: capacity = " + \ str(self._capacity) + ", load factor = " + \ str(len(self) / self._capacity) for i in range(self._capacity): result += " Row " + str(i) + ": " + str(self._table[i]) return result

def __iter__(self): """Iterates over the keys of the dictionary""" for myList in self._table: for entry in myList: yield entry.getKey() raise StopIteration

Here is another dictionary binary_search_tree:

from avl_tree_node import AVLTreeNode from binary_search_tree import BinarySearchTree class AVLTree(BinarySearchTree): def put(self,key,val): if self.root: self._put(key,val,self.root) else: self.root = AVLTreeNode(key,val) self.size = self.size + 1 def _put(self,key,val,currentNode): if key < currentNode.key: if currentNode.hasLeftChild(): self._put(key,val,currentNode.leftChild) else: currentNode.leftChild = AVLTreeNode(key,val,parent=currentNode) self.updateBalance(currentNode.leftChild) elif key > currentNode.key: if currentNode.hasRightChild(): self._put(key,val,currentNode.rightChild) else: currentNode.rightChild = AVLTreeNode(key,val,parent=currentNode) self.updateBalance(currentNode.rightChild) else: currentNode.payload = val self.size -= 1 # since put always adds 1 def updateBalance(self,node): if node.balanceFactor > 1 or node.balanceFactor < -1: self.rebalance(node) return if node.parent != None: if node.isLeftChild(): node.parent.balanceFactor += 1 elif node.isRightChild(): node.parent.balanceFactor -= 1 if node.parent.balanceFactor != 0: self.updateBalance(node.parent) def rotateLeft(self,rotRoot): newRoot = rotRoot.rightChild rotRoot.rightChild = newRoot.leftChild if newRoot.leftChild != None: newRoot.leftChild.parent = rotRoot newRoot.parent = rotRoot.parent if rotRoot.isRoot(): self.root = newRoot else: if rotRoot.isLeftChild(): rotRoot.parent.leftChild = newRoot else: rotRoot.parent.rightChild = newRoot newRoot.leftChild = rotRoot rotRoot.parent = newRoot rotRoot.balanceFactor = rotRoot.balanceFactor + 1 - min(newRoot.balanceFactor, 0) newRoot.balanceFactor = newRoot.balanceFactor + 1 + max(rotRoot.balanceFactor, 0) def rotateRight(self,rotRoot): newRoot = rotRoot.leftChild rotRoot.leftChild = newRoot.rightChild if newRoot.rightChild != None: newRoot.rightChild.parent = rotRoot newRoot.parent = rotRoot.parent if rotRoot.isRoot(): self.root = newRoot else: if rotRoot.isRightChild(): rotRoot.parent.rightChild = newRoot else: rotRoot.parent.leftChild = newRoot newRoot.rightChild = rotRoot rotRoot.parent = newRoot rotRoot.balanceFactor = rotRoot.balanceFactor - 1 + min(-newRoot.balanceFactor, 0) newRoot.balanceFactor = newRoot.balanceFactor - 1 + min(0, rotRoot.balanceFactor) def rebalance(self,node): if node.balanceFactor < 0: if node.rightChild.balanceFactor > 0: self.rotateRight(node.rightChild) self.rotateLeft(node) else: self.rotateLeft(node) elif node.balanceFactor > 0: if node.leftChild.balanceFactor < 0: self.rotateLeft(node.leftChild) self.rotateRight(node) else: self.rotateRight(node) def __str__(self): """Returns a string representation of the tree rotated 90 degrees counter-clockwise. Prints key and balanceFactor""" def strHelper(root, level): resultStr = "" if root: resultStr += strHelper(root.rightChild, level+1) resultStr += "| " * level resultStr += str(root.key) + ":" + str(root.balanceFactor)+" " resultStr += strHelper(root.leftChild, level+1) return resultStr return strHelper(self.root, 0)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Guide To Client Server Databases

Authors: Joe Salemi

2nd Edition

1562763105, 978-1562763107

More Books

Students also viewed these Databases questions

Question

=+country competitive advantages? Why? Support your point of view.

Answered: 1 week ago

Question

=+from: a) a MNEs perspective? and b) the HRM managers perspective?

Answered: 1 week ago