Exemple #1
0
def main():
    #capacity = int( input( "Enter capacity (-1 for default): " ) )
    capacity = 10
    if capacity < 0:
        hTable = HashTable()
    else:
        hTable = HashTable(capacity)
    #filename = input( "Enter filename: " )
    filename = 'atotc.txt'
    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)
Exemple #2
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 )
    print( "Average chain length: " + str( imbalance( 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 )
Exemple #3
0
def test_hash_table_generic():
    hashtable = Hashtable(1024)

    # testing for single input
    hashtable.add('ahmad', 25)
    hashtable.add('silent', True)

    assert hashtable.contains('ahmad') is True
    assert hashtable.contains('ahmadd') is False

    # testing for multiple inputs for a single bucket

    hashtable.add('hamad', 29)
    hashtable.add('listen', 'Hey man')

    assert hashtable.contains('ahmad') is True
    assert hashtable.contains('hamad') is True
    assert hashtable.contains('listen') is True
    assert hashtable.contains('silent') is True

    # check for duplicate input
    assert hashtable.add('ahmad', 35) == 'key already exists'

    # testing the values

    assert hashtable.get('ahmad') == 25
    assert hashtable.get('hamad') == 29
    assert hashtable.get('dada') == None
Exemple #4
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
Exemple #5
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