Exemplo n.º 1
0
def main():
    capacity = int( input( "Enter capacity (-1 for default): " ) )
    if capacity < 0:
        hTable = HashTable()
    else:
        hTable = HashTable( capacity )
    filename = input( "Enter filename: " )

    wordTable = word_count( hTable, filename )
    printSummary( wordTable )

    while True:

        print( "Commands: k[ey] <word> f[ind] <word> q[uit] ? ", end=" " )
        response = input( ":- " )   # the displayed prompt
        query = response.split()

        if len( response ) == 0 or not response[0] in "fkq": 
            print( response + " invalid. Please enter a command and a word." )
            response = ""
            continue

        if query[0] == "k":
            print( "( " + query[1] + " in text ) is " \
                 + str( contains( wordTable, query[1] ) ) + "." )

        if query[0] == "f":
            if contains( wordTable, query[1] ):
                print( query[1] + " appears " \
                     + str( get( wordTable, query[1] ) ) + " times." )
            else:
                print( query[1] + " in not in the text." )

        if query[0] == "q":
            break
    # 
    answer = input( "Do you want to see the entire table?(y/n) " )
    if answer != "y":
        return
    printTable( wordTable )
Exemplo n.º 2
0
def word_count( hTable, filename ):
    """
        Record the frequency of all words in the named file in the hashtable.
        word_count : HashTable String -> HashTable
    """

    # Read the words of the text file into the word count table.
    fd = open( filename )
    for line in fd:
        for word in line.split():
            # using a regular expression argument to strip(),
            # strip out punctuation and convert token to lower-case.
            word = word.strip(",.\"\';:-!?").lower()
            if contains( hTable, word ):
                count = get( hTable, word )
                put( hTable, word, count + 1 )
            else:
                put( hTable, word, 1 )

    fd.close()          # closing the file is a 'good practice'.
    return hTable