Question

Hey I have a task which consists of two part. Part A asks for writing a program of WORD & LINE CONCORDANCE APPLICATION in python which I have completed it.

Now the second part has given 5 dictionary implementation codes namely as: (ChainingDict, OpenAddrHashDict with linear probing, OpenAddrHashDict with quadratic probing, and 2 tree-based dictionaries from lab 12 (BST-based dictionary implementation) and asks for my above program WORD & LINE CONCORDANCE APPLICATION  to use these implemented code and show the time taken by each dictionary implemented code.

Here is the image of second part:

Part B. DICTIONARY COMPARISON We have 5 dictionary implementations from lab 10: ChainingDiet, OpenAddr HashDict with linear p

Now I need help to how to use the implemented code in my WORD & LINE CONCORDANCE APPLICATION in order to time the 5 dictionary implemented code separately.

Please it would be better if you show me the code of one dictionary implementation (open_addr_hash_Dictionary) with my WORD & LINE CONCORDANCE APPLICATION and show me that how much time it took.

I will attach all the necessary files here.

Here is my Word & Line Concordance Application which was asked in first part which I implemented it:

import re
from collections import defaultdict
def concordance():
    wordConcordanceDict = defaultdict(list)

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

    with open('small_file.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]))))

concordance()

Here is the python open_addr_hash_Dictionary python file which should be used with my word & line concordance code to show it's time execution:

from entry import Entry

class OpenAddrHashDict(object):

EMPTY = None
DELETED = True

def __init__(self, capacity = 8,
hashFunction = hash,
linear = True):
self._table = [OpenAddrHashDict.EMPTY] * capacity
self._size = 0
self._hash = hashFunction
self._homeIndex = -1
self._actualIndex = -1
self._linear = linear
self._probeCount = 0

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

def __delitem__(self, key):
"""Removes the entry associated with key."""
if key in self:
self._table[self._actualIndex] = OpenAddrHashDict.DELETED
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:
self._table[self._actualIndex] = entry
else:
self._table[self._actualIndex] = entry
self._size += 1
  
def __contains__(self, key):
"""Return True if key is in the dictionary; return
False otherwise"""
entry = Entry(key, None)
self._probeCount = 0
# Get the home index
self._homeIndex = abs(self._hash(key)) % len(self._table)
rehashAttempt = 0
index = self._homeIndex

# Stop searching when an empty cell is encountered
while rehashAttempt < len(self._table):
self._probeCount += 1
if self._table[index] == OpenAddrHashDict.EMPTY:
self._actualIndex = index
return False
elif self._table[index] == entry:
self._actualIndex = index
return True
  
# Increment the index and wrap around to first
# position if necessary
rehashAttempt += 1
if self._linear:
index = (self._homeIndex + rehashAttempt) % len(self._table)
else:
# Quadratic probing
index = (self._homeIndex + (rehashAttempt ** 2+ rehashAttempt) // 2) % len(self._table)

# An empty cell is found, so store the item
return False

def __len__(self):
return self._size

def __str__(self):
resultStr = "{"
for item in self._table:
if not item in (OpenAddrHashDict.EMPTY,OpenAddrHashDict.DELETED):
resultStr = resultStr + " " + str(item)
return resultStr + " }"

def __iter__(self):
"""Iterates over the keys of the dictionary"""
pass

def homeIndex(self):
return self._homeIndex

def probeCount(self):
return self._probeCount

def loadFactor(self):
return len(self._table)/self._size
  
  
def main():
"""Uses an example data set from Chapter 19."""
table = OpenAddrHashDict(8, lambda x : x)
for item in (range(10, 71, 10)):
table[item] = ""
print( "item:", item, "Home:", table.homeIndex(), "Probes:", table.probeCount(), \
"Load factor:", table.loadFactor())
print( table)
table[2] = ""
print( "item:", item, "Home:", table.homeIndex(), "Probes:", table.probeCount(), \
"Load factor:", table.loadFactor())
print( table)
return table

if __name__ == "__main__": t = main()

  

Here is the entry python file which is imported in Open_addr_hash_Dictionary file above.

class Entry(object):
def __init__(self, key, value):
self._key = key
self._value = value

def getKey(self):
return self._key

def getValue(self):
return self._value

def setValue(self, newValue):
self._value = newValue
  
def __eq__(self, other):
if not isinstance(other, Entry):
return False
return self._key == other._key

def __str__(self):
return str(self._key) + ":" + str(self._value)

Here is the stop_words_small text which is used in my word & line concordance application as stopping word:

a
about
be
by
can
do
i
in
is
it
of
on
the
this
to
was

Here is the proj7small text which is also used in my word & line concordance application to find the main words and their line number:

This is a sample data (text) file to
be processed by your word concordance program.

The real data file is much bigger

Please I repeat I need a code that uses my WORD & LINE CONCORDANCE APPLICATION and open_addr_hash_Dictionary to show me the execution time of open_addr_hash_Dictionary.

Thanks,

  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Part 2 of the given question says to measure time taken by various dictionary implementations. Basically, it means you need to replace the defaultdict used in your WORD & LINE CONCORDANCE APPLICATION with all these given dictionary implementations, one by one.

As you have asked to explain with open_addr_hash_Dictionary, I've used it in the code below.

def concordance():
    wordConcordanceDict = OpenAddrHashDict(100000)

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

    with open('small_file.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:
                # it doesn't work as defaultdict. So we have to check
                # if key is present in dictionary or not
                try:
                    # key is present
                    wordConcordanceDict[word].append(line_number)
                except:
                    # key is not present, so we create a new key
                    wordConcordanceDict[word] = [line_number]
    
    # the given implementation of dictionary don't have a method to get all keys/ items as there in python dictionaries.
    # instead, there's a function to get all key, value pairs as a string
    # we call that method and convert it to a dictionary
    wordConcordanceDict = re.sub(r', ',',',wordConcordanceDict.__str__()).split()[1:-1]
    wordConcordanceDict = dict(x.split(":") for x in wordConcordanceDict)

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


# import regex
import re

import OpenAddrHashDict
from open_addr_hash_dictionary import OpenAddrHashDict

# import time module
import time

# start time
start = time.time()

# given function call
concordance()

# end time
end = time.time()

# display time taken
print(end - start)

Comments have been added, wherever I made some edits.

What you have to do for remaining dictionary implementations?

Import all those dictionaries to word concordance program. Replace current dictionary with all the dictionary implementations that you need to time one by one.You might need to change the last line in concordance() function based on the implementation of the corresponding dictionary that you are using.

Add a comment
Know the answer?
Add Answer to:
Hey I have a task which consists of two part. Part A asks for writing a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project Description Allow the user to specify M (the size of the hash table) then, (1)...

    Project Description Allow the user to specify M (the size of the hash table) then, (1) add items found in text file "Project1.txt" to h, an initially-empty instance of a HASH (reject all items that have a key that duplicates the key of a previously added item—item keys must be unique); (2) display h (see format shown below); (3) delete 4 randomly-chosen items from the h; and finally, (4) display h. Note M must be a prime number, so your...

  • In this part, you will complete the code to solve a maze.

    - Complete the code to solve a maze- Discuss related data structures topicsProgramming-----------In this part, you will complete the code to solve a maze.Begin with the "solveMaze.py" starter file.This file contains comment instructions that tell you where to add your code.Each maze resides in a text file (with a .txt extension).The following symbols are used in the mazes:BARRIER = '-' # barrierFINISH = 'F' # finish (goal)OPEN = 'O' # open stepSTART = 'S' # start stepVISITED = '#' #...

  • Solve the code below: CODE: """ Code for handling sessions in our web application """ from bottle import request, response import uuid import json import model import dbsche...

    Solve the code below: CODE: """ Code for handling sessions in our web application """ from bottle import request, response import uuid import json import model import dbschema COOKIE_NAME = 'session' def get_or_create_session(db): """Get the current sessionid either from a cookie in the current request or by creating a new session if none are present. If a new session is created, a cookie is set in the response. Returns the session key (string) """ def add_to_cart(db, itemid, quantity): """Add an...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

  • Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...

    attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i need python one!!!!!!!!! the part below is interface for the range search tree which don’t need to modify it. Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you...

  • Hey guys I need help with this question with 3 sub-problems. f test remove short synonyms () Define the remove shorti s...

    Hey guys I need help with this question with 3 sub-problems. f test remove short synonyms () Define the remove shorti synonyms function which is passed a dictionary as a parameter- The keys of the parameter dictionary are words and the corresponding values are 1ists of synonyms (synonyms are words which have the same or nearly the same meaning). The function romoves all the eynonyme which have ous than 8 charactors from each corresponding list of synonyms-As well, the funet...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into...

    Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into hash table we insert at an index calculated by the key modulo the array size, what would happen if we instead did key mod (array_size*2), or key mod (array_size/2)? (Describe both cases). Theory answer Here Change your hashtable from the labs/assignments to be an array of linkedlists – so now insertion is done into a linkedlist at that index. Implement insertion and search. This...

  • please use Java!! We are storing the inventory for our fruit stand in a hash table....

    please use Java!! We are storing the inventory for our fruit stand in a hash table. The attached file shows all the possible key/fruit combinations that can be inserted into your hash table. These are the real price lookup values for the corresponding fruit. Problem Description In this programming assignment, you will implement a hash table class fruitHash. Your hash table must include the following methods: 1) hashFunction(key) This method takes a key as an argument and returns the address...

  • In problem 3, you will write a function, findvertically (), which will determine whether or not...

    In problem 3, you will write a function, findvertically (), which will determine whether or not a given word exists in a word search puzzle vertically. In word search puzzles, words can be written upwards or downwards. For example, "BEAK" appears at [1] [3] written downwards, while "BET" appears at [2] [2] written upwards: [["C", "A", "T", "X", "R"], ["D", "T", "E", "B", "L"], ["A" "R" "B", "E", "Z"], ["X", "O", "E", "A", "U"], ["J", "S", "O", "K", "W"]] Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT