<filename>XMLReduction.py
def xml_reduce(file, size, font):
    text = ""
    line = file.readline()
    #while the end of the file has not been reached
    while line != "":
        #if the line starts with "<textbox"
        if (line[:9] == "<textbox ") and (line.find(" />") != -1): pass
        elif line[:9] == "<textbox ":
            result = process_textbox(file, size, font)
            #if characters contained in textbox are valid add it to text
            if result[1]: text += result[0]
            elif result[2][:10] != "</textbox>": move_to_end_of_textbox(file)
        line = file.readline()
    return text

def xml_reduce_debug(file, size, font):
    text = ""
    line = file.readline()
    #while the end of the file has not been reached
    while line != "":
        #if the line starts with "<textbox"
        if (line[:9] == "<textbox ") and (line.find(" />") != -1): pass
        elif line[:9] == "<textbox ":
            result = process_textbox_debug(file, size, font)
            #if characters contained in textbox are valid add it to text
            if result[1]: text += result[0]
            elif result[2][:10] != "</textbox>": move_to_end_of_textbox(file)
        line = file.readline()
    return text

def process_textbox(file, size, font):
    i = 0
    num_passed = 0.0
    num_failed = 0.0
    text = ""
    line = file.readline()
    while line[:10] != "</textbox>":
        #if the first letter is a different size 
        if ((i == 0) and (line[:6] == "<text ")) or \
            ((line[:6] == "<text ") and equal_text(line, size, font)):
            text += get_characters(line)
            i += 1
            num_passed += 1.0
        elif (line[:6] == "<text>"):
            index = line.find("</text>")
            if index == -1: text += line[6:] #+ "\n"
            else: text += line[6:index]
            num_passed += 1.0
        elif (line[:9] =="<textline") or (line[:10] == "</textline") or \
            (line[:7] == "</text>") : pass
        else: #return "", False, line
            num_failed += 1.0
            if num_failed > num_passed: return "", False, line
        
        line = file.readline()
    percent_passed = num_passed / (num_passed + num_failed)
    return text, (percent_passed >= 0.95) and (i > 1), line #text, True, line

def process_textbox_debug(file, size, font):
    i = 0
    num_passed = 0.0
    num_failed = 0.0
    text = ""
    line = file.readline()
    while line[:10] != "</textbox>":
        #if the first letter is a different size 
        if ((i == 0) and (line[:6] == "<text ")) or \
            ((line[:6] == "<text ") and equal_text(line, size, font)):
            text += get_characters_debug(line)
            i += 1
            num_passed += 1.0
        elif (line[:6] == "<text>"):
            index = line.find("</text>")
            if index == -1: text += line[6:] #+ "\n"
            else: text += line[6:index]
            num_passed += 1.0
        elif (line[:9] =="<textline") or (line[:10] == "</textline") or \
            (line[:7] == "</text>") : pass
        else: #return "", False, line
            num_failed += 1.0
            if num_failed > num_passed: return "", False, line
        
        line = file.readline()
    percent_passed = num_passed / (num_passed + num_failed)
    return text, (percent_passed >= 0.95) and (i > 1), line #text, True, line

def equal_text(line, size, font):
    fontMatch = False
    pieces = line.split("\"")
    for i in range(0, len(pieces)):
        if (pieces[i].find(" font=") != -1) and (pieces[i+1].find(font) != -1):
            fontMatch = True
        elif (pieces[i] == " size="):
            if float(pieces[i+1]) == size: return True and fontMatch
            #need to play around with aprropriate epsilon
            elif (float(pieces[i+1]) < (size + 0.25)) and \
            (float(pieces[i+1]) > (size - 0.25)): return True and fontMatch
            else: return False

def get_characters(line):
    characters = ""
    inValue = False
    isFirst = True
    for i in range(0, len(line)):
        if line[i] == ">": inValue = True
        elif isFirst and (line[i] == "<"): isFirst = False
        elif (not isFirst) and (line[i] == "<"): break
        elif inValue: characters += line[i]
    return characters

def get_characters_debug(line):
    characters = ""
    inValue = False
    isFirst = True
    for i in range(0, len(line)):
        if line[i] == ">": inValue = True
        elif isFirst and (line[i] == "<"): isFirst = False
        elif (not isFirst) and (line[i] == "<"): break
        elif inValue: characters += line[i]
    
    gotBbox = False
    pieces = line.split("\"")
    for i in range(0, len(pieces)):
        if (pieces[i] == " bbox="):
            characters += "**" + pieces[i+1] + "**"
            gotBbox = True
        elif gotBbox: break
    return characters

def move_to_end_of_textbox(file):
    line = file.readline()
    while (line != "") and (line[:10] != "</textbox>"): line = file.readline()
<filename>XMLReduction.py
def xml_reduce(file, size, font):
    text = ""
    line = file.readline()
    #while the end of the file has not been reached
    while line != "":
        #if the line starts with "<textbox"
        if (line[:9] == "<textbox ") and (line.find(" />") != -1): pass
        elif line[:9] == "<textbox ":
            result = process_textbox(file, size, font)
            #if characters contained in textbox are valid add it to text
            if result[1]: text += result[0]
            elif result[2][:10] != "</textbox>": move_to_end_of_textbox(file)
        line = file.readline()
    return text

def xml_reduce_debug(file, size, font):
    text = ""
    line = file.readline()
    #while the end of the file has not been reached
    while line != "":
        #if the line starts with "<textbox"
        if (line[:9] == "<textbox ") and (line.find(" />") != -1): pass
        elif line[:9] == "<textbox ":
            result = process_textbox_debug(file, size, font)
            #if characters contained in textbox are valid add it to text
            if result[1]: text += result[0]
            elif result[2][:10] != "</textbox>": move_to_end_of_textbox(file)
        line = file.readline()
    return text

def process_textbox(file, size, font):
    i = 0
    num_passed = 0.0
    num_failed = 0.0
    text = ""
    line = file.readline()
    while line[:10] != "</textbox>":
        #if the first letter is a different size 
        if ((i == 0) and (line[:6] == "<text ")) or \
            ((line[:6] == "<text ") and equal_text(line, size, font)):
            text += get_characters(line)
            i += 1
            num_passed += 1.0
        elif (line[:6] == "<text>"):
            index = line.find("</text>")
            if index == -1: text += line[6:] #+ "\n"
            else: text += line[6:index]
            num_passed += 1.0
        elif (line[:9] =="<textline") or (line[:10] == "</textline") or \
            (line[:7] == "</text>") : pass
        else: #return "", False, line
            num_failed += 1.0
            if num_failed > num_passed: return "", False, line
        
        line = file.readline()
    percent_passed = num_passed / (num_passed + num_failed)
    return text, (percent_passed >= 0.95) and (i > 1), line #text, True, line

def process_textbox_debug(file, size, font):
    i = 0
    num_passed = 0.0
    num_failed = 0.0
    text = ""
    line = file.readline()
    while line[:10] != "</textbox>":
        #if the first letter is a different size 
        if ((i == 0) and (line[:6] == "<text ")) or \
            ((line[:6] == "<text ") and equal_text(line, size, font)):
            text += get_characters_debug(line)
            i += 1
            num_passed += 1.0
        elif (line[:6] == "<text>"):
            index = line.find("</text>")
            if index == -1: text += line[6:] #+ "\n"
            else: text += line[6:index]
            num_passed += 1.0
        elif (line[:9] =="<textline") or (line[:10] == "</textline") or \
            (line[:7] == "</text>") : pass
        else: #return "", False, line
            num_failed += 1.0
            if num_failed > num_passed: return "", False, line
        
        line = file.readline()
    percent_passed = num_passed / (num_passed + num_failed)
    return text, (percent_passed >= 0.95) and (i > 1), line #text, True, line

def equal_text(line, size, font):
    fontMatch = False
    pieces = line.split("\"")
    for i in range(0, len(pieces)):
        if (pieces[i].find(" font=") != -1) and (pieces[i+1].find(font) != -1):
            fontMatch = True
        elif (pieces[i] == " size="):
            if float(pieces[i+1]) == size: return True and fontMatch
            #need to play around with aprropriate epsilon
            elif (float(pieces[i+1]) < (size + 0.25)) and \
            (float(pieces[i+1]) > (size - 0.25)): return True and fontMatch
            else: return False

def get_characters(line):
    characters = ""
    inValue = False
    isFirst = True
    for i in range(0, len(line)):
        if line[i] == ">": inValue = True
        elif isFirst and (line[i] == "<"): isFirst = False
        elif (not isFirst) and (line[i] == "<"): break
        elif inValue: characters += line[i]
    return characters

def get_characters_debug(line):
    characters = ""
    inValue = False
    isFirst = True
    for i in range(0, len(line)):
        if line[i] == ">": inValue = True
        elif isFirst and (line[i] == "<"): isFirst = False
        elif (not isFirst) and (line[i] == "<"): break
        elif inValue: characters += line[i]
    
    gotBbox = False
    pieces = line.split("\"")
    for i in range(0, len(pieces)):
        if (pieces[i] == " bbox="):
            characters += "**" + pieces[i+1] + "**"
            gotBbox = True
        elif gotBbox: break
    return characters

def move_to_end_of_textbox(file):
    line = file.readline()
    while (line != "") and (line[:10] != "</textbox>"): line = file.readline()
#while the end of the file has not been reached #if the line starts with "<textbox" #if characters contained in textbox are valid add it to text #while the end of the file has not been reached #if the line starts with "<textbox" #if characters contained in textbox are valid add it to text #if the first letter is a different size #+ "\n" #return "", False, line #text, True, line #if the first letter is a different size #+ "\n" #return "", False, line #text, True, line #need to play around with aprropriate epsilon