Suppose I have a sentence of words, where each word (or character) has an associated numeric value, or color. As an example, this could be coming from an RNN sentiment classifier, which would produce something like:
I'm looking for a lightweight way of visualizing words/characters in sentences in Jupyter with python. Is there an elegant way of doing this inline in a notebook? I've mostly seen this done with additional javascript, in a separate html file. Note that I'm fine with just a static visualization. I've seen you can change the color of the font of each letter, but I'd prefer to just manipulate the background color (fill?), while keeping the text black. I'm just not sure what this is referred to.
Some of the most common data visualization examples include charts, tables, graphs, timelines, and maps. Data visualization tools are ideal for identifying trends, deviations, and patterns in data sets easily and in a way that these patterns can also be understood by non-technical people.
Text visualization is mainly achieved through the use of graph, chart, word cloud, map, network, timeline, etc. It is these visualized results that make it possible for humans to read the most important aspects of a huge amount of information.
These stages are exploration, analysis, synthesis, and presentation.
Examples of visualizations include word clouds, graphs, charts, and maps.
Not sure if there is a preferred way to achieve this; here is a quick approach that uses a tiny html
template and IPython.display.display_html
:
from IPython.display import display_html
def to_html(text, r, g, b):
return "<var style='background-color:rgb({}, {}, {});'>{} </var>".format(
r, g, b, text
)
example = "A quick brown fox jumps over the lazy dog.".split()
res = ''.join(to_html(word, *np.random.randint(0,256,size=3)) for word in example)
display_html(res, raw=True)
Resulting in something like this:
<var style='background-color:rgb(144, 237, 221);'>A </var><var style='background-color:rgb(28, 208, 84);'>quick </var><var style='background-color:rgb(142, 241, 214);'>brown </var><var style='background-color:rgb(67, 199, 115);'>fox </var><var style='background-color:rgb(121, 120, 116);'>jumps </var><var style='background-color:rgb(251, 46, 48);'>over </var><var style='background-color:rgb(128, 147, 44);'>the </var><var style='background-color:rgb(48, 215, 5);'>lazy </var><var style='background-color:rgb(239, 90, 48);'>dog. </var>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With