I need to modify this code to promt the user for a word and display how many times the word appeared in the input file.


class HashTable( object ):
"""A data structure that contains a collection of values
where each value is located by a hashable key.
No two values may have the same key, but more than one
key may have the same value.
"""

__slots__ = ( "table", "size" )

def __init__( self, capacity=100 ):
"""Create a hash table.
The capacity parameter determines its initial size.
"""
self.table = [ None ] * capacity
self.size = 0

def __str__( self ):
"""Return the entire contents of this hash table,
one chain of entries per line.
"""
result = ""
for i in range( len( self.table ) ):
result += str( i ) + ": "
result += str( self.table ) + "\n"
return result

class _Entry( object ):
"""A private class used to hold key/value pairs"""

__slots__ = ( "key", "value" )

def __init__( self, entryKey, entryValue ):
self.key = entryKey
self.value = entryValue

def __str__( self ):
return "(" + str( self.key ) + ", " + str( self.value ) + ")"

def hash_function( val, n ):
"""Compute a hash of the val string that is in range(0,n)."""
# return 0
# return len(val) % n
return hash( val ) % n

def keys( hTable ):
"""Return a list of keys in the given hashTable.
(It would be better to return a sequence, but that
is too advanced.)
"""
result = []
for entry in hTable.table:
if entry != None:
result.append( entry.key )
return result

def _locate( hTable, key ):
"""Find the entry in the hash table for the given key.
If the key is not found, find the first unused location in the table.
(This is called 'open addressing'.)
If the entry for the key is found, that entry is returned.
Otherwise the int index where the key would go in the table is returned.
Finally if the key is not found and no unusued locations are left,
the int -1 is returned.
This function is meant to only be called from within hashtable.
Callers of this function must be prepared to
deal with results of two different types, unless they
are absolutely sure whether the key is in the table
or not.
"""
index = hash_function( key, len( hTable.table ) )
startIndex = index # We must make sure we don't go in circles.
while hTable.table[ index ] != None and hTable.table[ index ].key != key:
index = ( index + 1 ) % len( hTable.table )
if index == startIndex:
return -1
if hTable.table[ index ] == None:
return index
else:
return hTable.table[ index ]

def contains( hTable, key ):
"""Return True iff hTable has an entry with the given key."""
entry = _locate( hTable, key )
return isinstance( entry, _Entry )

def put( hTable, key, value ):
"""Using the given hash table, set the given key to the
given value. If the key already exists, the given value
will replace the previous one already in the table.
If the table is full, an Exception is raised.
"""
entry = _locate( hTable, key )
if isinstance( entry, int ): # It's the index of an unused position.
if entry == -1:
raise Exception( "Hash table is full." )
hTable.table[ entry ] = _Entry( key, value )
hTable.size += 1
else: # pre-existing entry
entry.value = value
return True

def get( hTable, key ):
"""Return the value associated with the given key in
the given hash table.
Precondition: contains(hTable, key)
"""
entry = _locate( hTable, key )
return entry.value


def word_count( filename ):
"""Report on the frequency of different words in the
file named by the argument.
"""
hTable = HashTable( 19106 )

# Read the words of the text file into the word count table.
with open( filename ) as inFile:
for line in inFile:
for word in line.split():
if contains( hTable, word ):
count = get( hTable, word )
put( hTable, word, count + 1 )
else:
put( hTable, word, 1 )

# Display the entire table!
print( "Unique words:", hTable.size )

# Find the most common word in the text file.
total = 0
maxWord = ""
maxCount = 0
for key in keys( hTable ):
thisCount = get( hTable, key )
total += thisCount
if thisCount > maxCount:
maxCount = thisCount
maxWord = key

print( "There are " + str( len( keys( hTable ) ) ) + " words." )
print( "Total words:", total )
print( '"' + maxWord + "\" appeared ", str( maxCount ),
" times, more than any other word." )
answer = input( "Do you want to see the entire table?(y/n) " )
if answer == "y":
printTable( hTable )

def printTable( hTable ):
"""Print the contents of the given hash table.
Each key/value pair is displayed in parentheses, tuple-style.
All pairs appear on a single line.
"""
print( "Word Count Data\n---------------" )
for key in keys( hTable ):
print( "(" + key + "," + str( get( hTable, key ) ) + ")", end="" )
print( "\n" )

def main():
filename = input( "Enter filename: " )
word_count( filename )

main()

Please use CODE tags. Do you have questions?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.