ZeroDivisonError error when I divide a number by the length of a list

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!

What should be the value of len(list_words) when text is empty ?

try this, (initially st.text will return empty string)

len("".split())

if you expect the word_size = sum(len(word) for word in list_words) then I would suggest doing

word_size = sum(len(word) for word in list_words) / max(len(list_words), 1)

Otherwise just put a try except around it.

2 Likes

Thank you for answering @ash2shukla,

I have been doing some research and I realized that st.text_input . Streamlit always runs all the code in the .py file at once the first time. Since no input has been entered, len(list_words) is worth 0.

I tried the try except aproach without getting good results. But using max(len(list_words), 1) worked fine for me! Thank you!

Also i tried another solution that worked for me, putting everything in an if-loop where the execution does not enter if no input has been put, like this:

if 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)

Thank you so much @ash2shukla for answering!

1 Like