コード例 #1
0
def compareRandStrings(verbatim=False, trials=30, n=10, seed=None):
    # For each trial, generate 2 strings, compute ED, track average ED.
    avg = 0
    for t in range(trials):
        s1, s2 = getRandStrings(n, seed)
        dist, edits = ED(s1, s2)
        avg += dist
        edited = makeEdits(s1, edits)
        if edited != s2:
            print('Failed Test!!!')
    avg = avg / trials
    if verbatim:
        print('Performed ' + str(trials) + ' trials.')
        print('Compared strings of size ' + str(n))
        print('Average ED: ' + str(avg))
        print('Average ED as Fraction of String Length: ' + str(avg / n))
    return avg
コード例 #2
0
def edTests(verbatim=False):
    # Set up the tests.
    string1 = ['spam', 'libate', '', 'abc', 'aaa']
    string2 = ['pims', 'flub', 'abc', '', 'bbb']
    dists = [3, 5, 3, 3, 3]
    labels = ['Class Ex 1', 'Class Ex 2', 'All Inserts', 'All Deletes', \
              'All Subs']

    # Track the number passed.
    numPassed = 0

    # Perform the tests.
    for sInd in range(len(string1)):
        s1 = string1[sInd]
        s2 = string2[sInd]
        print('Performing Test ' + str(sInd + 1))
        print('Test Description: ' + labels[sInd])
        if verbatim:
            print('\tString 1: ' + s1 + ',\tString 2: ' + s2)
        dist, edits = ED(s1, s2)
        if verbatim:
            print('\tReported Distance: ' + str(dist))
            print('\tReported Edits:')
            for ed in edits:
                print('\t\t' + str(ed))
        edited = makeEdits(s1, edits)
        passed = (edited == s2) and (dists[sInd] == dist)
        if verbatim:
            print('\tTarget String: ' + s2)
            print('\tEdited String: ' + edited)
        if passed:
            print('Test ' + str(sInd + 1) + ' Passed!')
            print()
            numPassed += 1
        else:
            print('Test ' + str(sInd + 1) + ' Failed!')
            print()

    # Print number passed.
    print('Passed ' + str(numPassed) + '/' + str(len(string1)) + ' Tests')