Package: picture-text
PictureTextPictureText converts a list of short documents to an interactive tree map with minimal code. It defaults to SBERT for text representation, leverages Hierarchical Agglomerative Clustering (HAC) for grouping and tree maps to visualize text interactively.Given a corpus of short documents (think news headlines) it can group them into hierarchical groups, that semantically belong together. It also allows the reader to explore each group in more detail by going deeper into a hierarchy and dynamically pulling out of it when needed.The approach is intended for grouping large sets of non-domain specific short texts. For instance: news headlines, natural language questions and social media posts would be good candidates.Getting startedInstallationcondacreate--nameptpython=3.6condainstall-nptnb_conda_kernelscondaactivateptpipinstallpicture_textA simple exampleConsider the default values and their resulttxt=['The cat sits outside','A man is playing guitar','I love pasta','The new movie is awesome','The cat plays in the garden','A woman watches TV','The new movie is so great','Do you like pizza?','Burgers are my favorite','I like chips','I will have french fries with my burger']frompicture_text.picture_textimportPictureText# initializing just sets the text corpuspt=PictureText(txt)# Calling the method does the heavy lifting:# 1. HAC# 2. text embeddingpt()# This step puts it all together:# 1. converts HAC into a treemap format# 2. determines a summary for each cluster and# 3. draws & return a treemappt.make_picture()DemoCheckout the Colab notebook for interactive examplesOutline of approachPerform any required preprocessing to get to a list of document stringsEmbed / Encode all documents with the method of choice, by default I useSBERTUse HAC to get a “linkage” table of hierarchical assignments of each point to the rest of the data. Here I usefastcluster, ward linkage by default.Convert to layers for treemap. Iteratively create “layers” by selecting a set number of splits to each layerSummarize. Generate a summary for each layer. In the default setting, I use the point closest to the average of the cluster. Using the average of the cluster to represent its centroid is used in a number of few-shot, unsupervised settingsDraw treemap. Useplotly'streemapfor interactive visualizationCustomizationConsider the default values and their resultfrompicture_text.picture_textimportPictureTextpt=PictureText(txt)pt(hac_method='ward',hac_metric='euclidean')pt.make_picture(layer_depth=6,layer_min_size=0.1,layer_max_extension=1,treemap_average_score=None,treemap_maxdepth=3,)Selecting Layer SettingsChanginglayer_depthparameter sets the number of layers produced by the split.pt.make_picture(layer_depth=1)Changinglayer_min_sizeparameters determines what is the minimal acceptable size of a new cluster for each layer. By defaultlayer_min_sizeis 0.1 (or 10%) meaning if a layer has a cluster smaller than 10% we will try to find another cluster to add to the layer hoping that the next one will be bigger. We will do so up to increasing the relative number of additional clusters up to 1 (or 100%,layer_max_extension= 1). Increasing both of these significantly basically means that we get a lot more clusters a lot earlier.pt.make_picture(layer_depth=1,layer_min_size=0.9,layer_max_extension=3,)Selecting Clustering SettingsThe defaults are the followingpt=PictureText(txt)pt(hac_method='ward',hac_metric='euclidean')However, those get fed directly into fastcluster, hence all choices from the fastcluster documentation are available here too.BYO-NLPThe key features to this sort of approach are the embeddings as well as the method of multi-doc summarization. You can use your NLP tools of choice there.Text embeddingsThe default set of text embeddings is viaSBERT'sdistilbert-base-nli-stsb-mean-tokens.frompicture_text.picture_textimportsbert_encoderpt=PictureText(txt)pt(encoder=sbert_encoder)However, any mapping of a list of text to encoding can be used instead.defsilly_encoder(text_list):return[[1]]*len(text_list)pt(encoder=silly_encoder)pt.make_picture()SummarizerThe default sumary method is to take the cluster member closest to the cluster averag. However, any mapping of a list of texts and embeddings into a text summary can be used instead.defsilly_summarizer(txt,txt_embeddings):return"All the same to me",0pt.make_picture(summarizer=silly_summarizer,)