polyglot icon indicating copy to clipboard operation
polyglot copied to clipboard

ZeroDivisionError: float division by zero

Open web64 opened this issue 6 years ago • 5 comments

Great library by the way!

I frequently get this error:

File "/usr/local/lib/python3.5/dist-packages/polyglot/text.py", line 96, in polarity return sum(scores) / float(len(scores)) ZeroDivisionError: float division by zero

Locally I have replaced the line in text.py with:

if float(len(scores)) != 0: return sum(scores) / float(len(scores)) else: return 0

And that seems to work.

web64 avatar Aug 06 '17 17:08 web64

I've got some error on version polyglot==16.7.4/python3.4

w32 avatar Dec 21 '17 11:12 w32

same in '16.07.04'/python 3.5

chubakur avatar Apr 27 '18 14:04 chubakur

Same in python 3.6

MiliasV avatar Jul 13 '18 14:07 MiliasV

I've solved this problem by:

from polyglot.text import cached_property, Text
class TextOverride(Text):
    @cached_property
    def polarity(self):
        """Return the polarity score as a float within the range [-1.0, 1.0]
        """
        scores = [w.polarity for w in self.words if w.polarity != 0]
        if len(scores) == 0:
            return 0.0
        return sum(scores) / float(len(scores))

elben10 avatar Apr 10 '20 09:04 elben10