import collections

# this script was adapted from:
# https://towardsdatascience.com/very-simple-python-script-for-extracting-most-common-words-from-a-story-1e3570d0b9d0

# open and read file
file = open(input("\nFile you want to categorize is: \n"), encoding="utf8")
a = file.read()

# my stopwords are common words I don't want to count, like "a", "an", "the".
stopwords = set(line.strip() for line in open('stopwords.txt'))

# dictionary
wordcount = {}

# spliting words from punctuation so "book" and "book!" counts as the same word
for word in a.lower().split():
    word = word.replace(".","")
    word = word.replace(",","")
    word = word.replace(":","")
    word = word.replace("\"","")
    word = word.replace("!","")
    word = word.replace("â€œ","")
    word = word.replace("â€˜","")
    word = word.replace("*","")

# counting
    if word not in stopwords:
        if word not in wordcount:
            wordcount[word] = 1
        else:
            wordcount[word] += 1

# print x most common words
# n_print = int(input("How many most common words to print: "))
n_print = int(3)
print("\nMost common words are:")
word_counter = collections.Counter(wordcount)
for word, count in word_counter.most_common(n_print):
    print(word,"—", count)

# categories

# words that are inside the category Library Studies
library_studies = set(line.strip() for line in open('library_studies.txt'))

for word, count in word_counter.most_common(n_print):
    if word in library_studies:
        print("\nWe suggest the following categorization for this file:\nLibrary Studies\n")
        break
else:
    print("\nWe don't have any suggestion of categorization for this file.\n")

# Close the file
file.close()
