##### Import all the necessary Libraries

from yellowbrick.text import TSNEVisualizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from yellowbrick.text import UMAPVisualizer
from yellowbrick.datasets import load_hobbies

"""
Explanation: Comparing Corpus Visualizers on Yellowbrick
End of explanation
"""


corpus = load_hobbies()

"""
Explanation: UMAP vs T-SNE
Uniform Manifold Approximation and Projection (UMAP) is a dimension reduction technique that can be used for visualisation similarly to t-SNE, but also for general non-linear dimension reduction. The algorithm is founded on three assumptions about the data

The data is uniformly distributed on a Riemannian manifold;
The Riemannian metric is locally constant (or can be approximated as such);
The manifold is locally connected.

From these assumptions it is possible to model the manifold with a fuzzy topological structure. The embedding is found by searching for a low dimensional projection of the data that has the closest possible equivalent fuzzy topological structure.
End of explanation
"""


def visualize(dim_reduction,encoding,corpus,labels = True,alpha=0.7,metric=None):
    if 'tfidf' in encoding.lower():
        encode  = TfidfVectorizer()
    if 'count' in encoding.lower():
        encode = CountVectorizer()
    docs   = encode.fit_transform(corpus.data)
    if labels is True:
        labels = corpus.target
    else:
        labels = None
    if 'umap' in dim_reduction.lower():
        if metric is None:
            viz   = UMAPVisualizer()
        else:
            viz = UMAPVisualizer(metric=metric)
    if 't-sne' in dim_reduction.lower():
        viz = TSNEVisualizer(alpha = alpha)
    viz.fit(docs,labels)
    viz.show()

"""
Explanation: Writing a Function to quickly Visualize Corpus
Which can then be used for rapid comparison
End of explanation
"""


visualize('t-sne','tfidf',corpus)

visualize('t-sne','count',corpus,alpha = 0.5)

visualize('t-sne','tfidf',corpus,labels =False)

visualize('umap','tfidf',corpus)

visualize('umap','tfidf',corpus,labels = False)

visualize('umap','count',corpus,metric= 'cosine')

"""
Explanation: Quickly Comparing Plots by Controlling

The Dimensionality Reduction technique used 
The Encoding Technique used 
The dataset to be visualized 
Whether to differentiate Labels or not 
Set the alpha parameter
Set the metric for UMAP
End of explanation
"""


