text_plain_output_1.png
import os

ALPHABET_SIZE = 256

def badCharHeuristic(string, size):
    badChar = [-1] * ALPHABET_SIZE
    for i in range(size):
        badChar[ord(string[i])] = i
    return badChar

def BMMatch(text, pattern):
    text = text.lower()
    pattern = pattern.lower()
    counter = 0
    m = len(pattern)
    n = len(text)
    badChar = badCharHeuristic(pattern, m)
    s = 0
    while s <= n - m:
        j = m - 1
        while j >= 0 and pattern[j] == text[s + j]:
            counter += 1
            j -= 1
        if j < 0:
            return s
        else:
            counter += 1
            s += max(1, j - badChar[ord(text[s + j])])
    return counter

import os
os.listdir('/kaggle/input')
with open('../input/norskplaceholdertekst/Ibsen-PeerGynt2.txt', 'r') as file:
    text = file.read()

patterns = ['prins', 'slutt', 'konge', 'lille', 'hjælp', 'kavri', 'serri', 'jenta', 'jente', 'stemm', 'elven', 'ørken', 'banan', 'bringe', 'vejen', 'vegen', 'veien', 'danse']
comparisons = []
for i in patterns:
    counter = BMMatch(text, i)
    comparisons.append(counter)
comp_per_char = []
for i in comparisons:
    comp_per_char.append(i / len(text))

def average(list):
    return sum(list) / len(list)

average(comp_per_char)