erinhengel / Textatistic

Calculate readability scores
http://www.erinhengel.com/software/textatistic/
Apache License 2.0
40 stars 9 forks source link

BUG: ZeroDivisionError while creating Textatistic object #4

Open sohang3112 opened 1 year ago

sohang3112 commented 1 year ago

The code Textatistic('Sunfeast Dark Fantasy Choco Fills, 600g, Original Filled Cookies with Choco Crème\n') raises ZeroDivisionError. This is because, in flesch_score function, vars['sent_count'] is 0 at the return statement.

def flesch_score(text=None, abbr=None, hyphen=None, vars={}):
    """Calculate Flesch Reading Ease score."""
    if text:
        if not abbr:
            abbr = Abbreviations()
        if not hyphen:
            hyphen = Hyphenator('en_US')
        text = punct_clean(text, abbr)
        vars['sent_count'] = sent_count(text, abbr, True)
        text = word_array(text, abbr, True)
        vars['word_count'] = word_count(text, abbr, True)
        vars['sybl_count'] = sybl_counts(text, abbr, hyphen, True)['sybl_count']
    return 206.835 - 1.015 * (vars['word_count'] / vars['sent_count']) - 84.6 * (vars['sybl_count'] / vars['word_count'])
MarcinKamil84 commented 1 year ago

I have the same issue and I've mitigated it by creating a new feature which adds '.' to the original text. Not the most elegant solution, I know.

sohang3112 commented 1 year ago

@MarcinKamil84 One issue with adding '.' could be that if a text already has '.' at the end of sentence, then extra '.' could (wrongly) increase sent_count.

MarcinKamil84 commented 1 year ago

@sohang3112 Totally agreed. It's just a not-so-elegant workaround.

sohang3112 commented 1 month ago

@erinhengel I would like to work on this issue - would you be interested in a Pull Request to fix this bug?