Skip to content

❗  Word importances

Submitted by Arnaud Miribel

Summary

Highlight words based on their importances. Inspired from captum library.

Functions

format_word_importances

Adds a background color to each word based on its importance (float from -1 to 1)

Parameters:

Name Type Description Default
words list

List of words

required
importances list

List of importances (scores from -1 to 1)

required

Returns:

Name Type Description
html str

HTML string with formatted word

Source code in src/streamlit_extras/word_importances/__init__.py
@extra
def format_word_importances(words: List[str], importances: List[float]) -> str:
    """Adds a background color to each word based on its importance (float from -1 to 1)

    Args:
        words (list): List of words
        importances (list): List of importances (scores from -1 to 1)

    Returns:
        html (str): HTML string with formatted word


    """
    if importances is None or len(importances) == 0:
        return "<td></td>"
    assert len(words) == len(importances), "Words and importances but be of same length"

    tags = ["<td>"]
    for word, importance in zip(words, importances[: len(words)]):
        color = _get_color(importance)
        unwrapped_tag = (
            '<mark style="background-color: {color}; opacity:1.0;             '
            '        line-height:1.75"><font color="black"> {word}            '
            "        </font></mark>".format(color=color, word=word)
        )
        tags.append(unwrapped_tag)
    tags.append("</td>")
    html = "".join(tags)

    return html

Import:

from streamlit_extras.word_importances import format_word_importances # (1)!
  1. You should add this to the top of your .py file 🛠

Examples

example

def example():
    text = (
        "Streamlit Extras is a library to help you discover, learn, share and"
        " use Streamlit bits of code!"
    )
    html = format_word_importances(
        words=text.split(),
        importances=(0.1, 0.2, 0, -1, 0.1, 0, 0, 0.2, 0.3, 0.8, 0.9, 0.6, 0.3, 0.1, 0, 0, 0),  # fmt: skip
    )
    st.write(html, unsafe_allow_html=True)
Output (beta)