Hello everyone,
This is my first post so my apologies if I’m not using optional tags properly.
I’m trying to create a streamlit app showing extracted features from text and I got this error when trying to show the average word size, and other features when I have to divide a number by the length of a list:
ZeroDivisionError: division by zero
Traceback:
File "/home/pipe11/.local/lib/python3.6/site-packages/streamlit/script_runner.py", line 324, in _run_script
exec(code, module.__dict__)File "/home/pipe11/TFM_fake_news_detector/predictors/test_app.py", line 35, in <module>
n_words, avg_word_size, avg_word_sentences, unique_words, ttr, mltd = get_news_features(text)File "/home/pipe11/.local/lib/python3.6/site-packages/streamlit/caching.py", line 593, in wrapped_func
return get_or_create_cached_value()File "/home/pipe11/.local/lib/python3.6/site-packages/streamlit/caching.py", line 575, in get_or_create_cached_value
return_value = func(*args, **kwargs)File "/home/pipe11/TFM_fake_news_detector/predictors/test_app.py", line 22, in get_news_features
avg_word_size = sum(len(word) for word in list_tokens) / n_words
This is my code:
%%writefile test_app.py
import streamlit as st
import pandas as pd
import spacy
from nltk import FreqDist
from lexical_diversity import lex_div as ld
pd.options.display.max_columns = None
@st.cache(show_spinner = False)
def get_news_features(text):
nlp = spacy.load('es_core_news_md')
doc = nlp(text)
list_tokens = []
for sentence in doc.sents:
for token in sentence:
list_tokens.append(token.text)
fdist = FreqDist(list_tokens)
n_words = len(list_tokens)
avg_word_size = sum(len(word) for word in list_tokens) / n_words
avg_word_sentences = (float(n_words) / n_sents)
unique_words = (len(fdist.hapaxes()) / n_words) * 100
ttr = ld.ttr(list_tokens) * 100
mltd = ld.mtld(list_tokens)
return n_words, avg_word_size, avg_word_sentences, unique_words, ttr, mltd
# display title and description
st.title("Text features")
text = st.text_input("Insert text:")
n_words, avg_word_size, avg_word_sentences, unique_words, ttr, mltd = get_news_features(text)
st.write('Number of words', n_words)
st.write('Average word size', avg_word_size)
st.write('Average n words per sentence', avg_word_sentences)
st.write('% unique words', unique_words)
st.write('Type token ratio', ttr)
st.write('Mltd', mltd)
I was getting so frustrated that I started testing with this error and trying to replicate in a simple ways like this case:
%%writefile test_app.py
import streamlit as st
text = st.text_input("Insert text:")
list_words = text.split()
word_size = sum(len(word) for word in list_words) / len(list_words)
st.write('Number of words', len(list_words))
st.write('Average word size', word_size)
And got the same error:
ZeroDivisionError: division by zero
Traceback:
File "/home/pipe11/.local/lib/python3.6/site-packages/streamlit/script_runner.py", line 324, in _run_script
exec(code, module.__dict__)File "/home/pipe11/TFM_fake_news_detector/predictors/test_app.py", line 7, in <module>
word_size = sum(len(word) for word in list_words) / len(list_words)
Trying several tests I just realized that a list generated with .split() or with any kind of iteration, when I apply len() to get that number and I use it as divisor, it generates a ZeroDivisionError. It’s like that len(list) is empty when it is a divisor. Is it there any solution?
My python version is: 3.6.9 and streamlit 0.65.2.
Thank you in advance!