# Frequency Analysis By Unicode Block

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

# Unicode block functions
from get_unicode_blocks import *

# OpenAlex API wrapper
from habanero import Crossref

# Function to get a dictionary of each type of Unicode "Block"
def get_block_data(title):
    blockDict = {}
    # For each character in the title string...
    for char in title:
        # Get the Unicode block of that character
        block = get_block_for_codepoint(ord(char))
        # If block present, increment it
        if block in blockDict:
            blockDict[block] = blockDict[block] + 1
        else:   # Else add it to dictionary
            blockDict[block] = 1

    # Return the created dictionary
    return blockDict

# Initialise Client and create Query for 100 random titles in 2023
crossRefClient = Crossref(mailto = "jc550@students.waikato.ac.nz")
crossRefQuery = crossRefClient.works(cursor = "*", cursor_max = 100, filter = {'from_pub_date': '2023', 'type' : ['journal-article', 'proceedings-article', 'book-chapter']})

titles = []

# For each page (20 entries) in the query...
for page in crossRefQuery:
    # For each item in the page...
    for item in page["message"]["items"]:
        # Append the title string to the title list
        titles.append(item["title"][0])

# List containing block data for every title
titleBlockData = []

# For each title get their block data
for title in titles:
    titleBlockData.append(get_block_data(title))

# Dictionary to store frequency of number of blocks in titles
dictionaryFrequency = {}

# For each title data in the list...
for data in titleBlockData:
    # Add to the dictionary
    if str(len(data)) in dictionaryFrequency:
        dictionaryFrequency[str(len(data))] = dictionaryFrequency[str(len(data))] + 1
    else:
        dictionaryFrequency[str(len(data))] = 1
    
print(dictionaryFrequency)

xKeys = list(dictionaryFrequency.keys())
yBars = list(dictionaryFrequency.values())

#Do the plotting
fig, ax = plt.subplots()
ax.set_title("Graph of number of Unicode blocks in each title in random 100 2023 titles")
ax.bar(xKeys,yBars,width=1, edgecolor="white", linewidth=0.7, align="edge")
plt.show()

