import sys

def get_percentage(a, b, n):
    return round(a / b * 100, n)

def ngram(filename, n, print_all=False):
    if print_all:
        if print_all.lower() == "true":
            print_all = True
        else:
            print_all = False


    n = int(n)
    file = open(filename, "r")
    text = file.read()
    grams = []

    # Padding to prevent crashes when splitting
    for i in range(n - len(text) % n):
        text += " "

    # Split up into n sized bites
    for j in range(0, len(text)-n, n):
        grams.append(text[j:j+n])

    num_of_grams = len(grams)
    
    # Count
    count = {}
    for gram in grams:
        if gram in count.keys():
            count[gram] += 1
        else:
            count[gram] = 1
    
    # Order
    ordered_grams = []
    for gram in count.keys():
        ordered_grams.append([gram, count[gram]])
    
    ordered_grams = sorted(ordered_grams, key=lambda x: x[1], reverse=True)

    # Print either all or top 6
    if print_all:
        for gram in ordered_grams:
            print(f"[{gram[0]}] - {get_percentage(gram[1], num_of_grams, 2)}%")
            
    else:
        for i in range(6):
            gram = ordered_grams[i]
            print(f"[{gram[0]}] - {get_percentage(gram[1], num_of_grams, 2)}%")

    return ordered_grams

def main ():
    # Print top 6
    if len(sys.argv) == 3 : ngram(sys.argv[1], sys.argv[2])
    # Print all
    elif len(sys.argv) == 4 : ngram(sys.argv[1], sys.argv[2], print_all=sys.argv[3])

if __name__=="__main__": main()
import sys

def get_percentage(a, b, n):
    return round(a / b * 100, n)

def ngram(filename, n, print_all=False):
    if print_all:
        if print_all.lower() == "true":
            print_all = True
        else:
            print_all = False


    n = int(n)
    file = open(filename, "r")
    text = file.read()
    grams = []

    # Padding to prevent crashes when splitting
    for i in range(n - len(text) % n):
        text += " "

    # Split up into n sized bites
    for j in range(0, len(text)-n, n):
        grams.append(text[j:j+n])

    num_of_grams = len(grams)
    
    # Count
    count = {}
    for gram in grams:
        if gram in count.keys():
            count[gram] += 1
        else:
            count[gram] = 1
    
    # Order
    ordered_grams = []
    for gram in count.keys():
        ordered_grams.append([gram, count[gram]])
    
    ordered_grams = sorted(ordered_grams, key=lambda x: x[1], reverse=True)

    # Print either all or top 6
    if print_all:
        for gram in ordered_grams:
            print(f"[{gram[0]}] - {get_percentage(gram[1], num_of_grams, 2)}%")
            
    else:
        for i in range(6):
            gram = ordered_grams[i]
            print(f"[{gram[0]}] - {get_percentage(gram[1], num_of_grams, 2)}%")

    return ordered_grams

def main ():
    # Print top 6
    if len(sys.argv) == 3 : ngram(sys.argv[1], sys.argv[2])
    # Print all
    elif len(sys.argv) == 4 : ngram(sys.argv[1], sys.argv[2], print_all=sys.argv[3])

if __name__=="__main__": main()
# Padding to prevent crashes when splitting # Split up into n sized bites # Count # Order # Print either all or top 6 # Print top 6 # Print all