# viz.py
#    By Johnny Cheng
#    Updated: 24 June 2022

import numpy as np
import pandas as pd
from importlib_resources import files
import re
import nltk
from nltk.corpus import stopwords
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from PIL import Image
import jieba
from collections import Counter


def load_word(ver='web.csv'):
    scfile = files('wordjc.data').joinpath(ver)
    return pd.read_csv(scfile)


def plot_cloud(wordcloud):
    plt.figure(figsize=(15, 10))
    plt.imshow(wordcloud) 
    plt.axis("off");


def show_wordcloud(text, image='heart.jpg', mask=None):
    if image:
        img_file = files('wordjc.images').joinpath(image)
        mask = np.array(Image.open(img_file))

    wordcloud = WordCloud(background_color='black', colormap='Set2', mask=mask) \
                    .generate(text)

    plot_cloud(wordcloud)


def chi_wordcloud(df, image='heart.jpg', mask=None):
    add_chi_vocab()
    stops = chi_stops()
    diction = get_diction(df, stops)

    if image:
        img_file = files('wordjc.images').joinpath(image)
        mask = np.array(Image.open(img_file))

    font = 'msyh.ttc'
    wordcloud = WordCloud(background_color='black', colormap='Set2', \
	                                mask=mask, font_path=font) \
                            .generate_from_frequencies(frequencies=diction)

    plot_cloud(wordcloud)
