# Frequency Analysis

# Graphing Libraries
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

import json

# Open the data file
file = open("data.json", "r", encoding="utf-8")

jsonFile = json.load(file)
dataArray = jsonFile["dataArray"]

dictionaryFrequency = {}

for data in dataArray:
    for title in data["titles"]:
        for char in enumerate(title):
            encodedChar = char[1].encode('utf-8')
            #print("Original: " + str(char[1]) + " Encoded: " + str(encodedChar))
            if encodedChar not in dictionaryFrequency:
                dictionaryFrequency[encodedChar] = 1
            else:
                dictionaryFrequency[encodedChar] = dictionaryFrequency[encodedChar] + 1


#Print each key with number of times it appeared
keys = dictionaryFrequency.keys()
sortedKeys =list(sorted(keys, key=dictionaryFrequency.get, reverse=True)) 

strangeArticles = {"titles":[], "dois":[]}
numArticles = 0

strangeOutput = open("strangeArticles.txt", "w", encoding="utf-8")

# TRYING TO ITERATE THROUGH SORTED KEYS IN REVERSE
# improve this code ?
for i in range(-1, len(sortedKeys) * -1, -1):
    charToCheck = sortedKeys[i].decode('utf-8')
    if numArticles >= 50:
        break
    for data in dataArray:
        if numArticles >= 50:
            break
        for index in range(len(data["titles"])):
            title = data["titles"][index]
            if charToCheck in title:
                if title not in strangeArticles["titles"]:
                    numArticles = numArticles + 1
                    strangeArticles["titles"].append(title)
                    strangeArticles["dois"].append(data["dois"][index])
                    if numArticles >= 50:
                        print("Uncommon Characters: ", file=strangeOutput, end="")
                        for char in sortedKeys[i:-1:1]:
                            print("'" + str(char.decode('utf-8')) + "' ", file=strangeOutput, end="")
                            print(char)
                        print("", file=strangeOutput)
                        break


for index in range(50):
    currentTitle = strangeArticles["titles"][index]
    currentDOI = strangeArticles["dois"][index]
    print("Title: " + currentTitle + " | DOI: " + currentDOI, file=strangeOutput)

yBars = []
xKeys = []
for key in sortedKeys:
    print(str(key.decode('utf-8')) + ": " + str(dictionaryFrequency[key]))
    yBars.append(dictionaryFrequency[key])
    xKeys.append(key.decode('utf-8'))

print("xKeys: ")
print(xKeys)
print("ybars: ")
print(yBars)

#plt.style.use('_mpl-gallery')
#x = 0.5 + np.arange(len(keys))

#Do the plotting
fig, ax = plt.subplots()
ax.set_title("Character Frequency Analysis of First 5000 Journal Entries in 2023")
ax.bar(xKeys,yBars,width=1, edgecolor="white", linewidth=0.7, align="edge")
plt.show()

