Search code examples
pythonalgorithmdefined

NameError: name is not defined, even when I define the name


I'm writing a program that parses through a text matching a pattern to the text using the BoyerMoore algorithm. I managed to get the code to find all matches, and print out the position of the matches.

Now I also tried to get the length of the match_table and the number of comparisons. I'm quite unfamiliar with python, and the program tells me "NameError: name 'comparison' is not defined, even though it is used in the def boyer_moore. The program apparently doesn't save the variables for later use. I'm sorry if this sounds confusing, I'm quite new to python. The text that is being matched to can be found here

def make_bad_match_table(pattern):

length = len(pattern)
table = {}
for i, c in enumerate(pattern):
    if i == length-1 and not c in table:
        table[c] = length
    else:
        table[c] = length - i - 1

return table




def boyer_moore(pattern, text):
    comparison = 0
    match_table = []
    pattern_length = len(pattern)
    text_length = len(text)
    if pattern_length > text_length:
        return match_table

    table = make_bad_match_table(pattern)
    index = pattern_length - 1
    pattern_index = pattern_length - 1

    while index < text_length:
        if pattern[pattern_index] == text[index]:
            if pattern_index == 0:
                match_table.append(index)
                pattern_index = pattern_length - 1
                index += (pattern_length * 2 - 1)
                comparison +=1
            else:
                pattern_index -= 1
                index -= 1
        else:
            index += table.get(text[index], pattern_length)
            pattern_index = pattern_length - 1

    return match_table
    return comparison

if __name__ == '__main__':

    file = open("CNN.txt", "r")
    target = file.read()
    pattern = "NASA"
    print(pattern,boyer_moore(pattern, target))
    print(len(match_table))
    print(comparison)

Solution

  • 1.Firstly instead of

    return match_table
    return comparison
    

    just do

    return (match_table, comparison)
    

    2.Instead of this:

    print(pattern,boyer_moore(pattern, target))
    print(len(match_table))
    print(comparison)
    

    just do:

     match_table, comparsion = boyer_moore(pattern, target)
     print(pattern)
     print(comparison, len(match_table))
    

    Hope it may help you now.