βΒ Β 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
Import:
- 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)