1

I want to use suggest function from pattern library in Python but I encountered an error.

I expected to see the output but it raised StopIteration error.

My code:

from pattern.en import sentiment, suggest

print(sentiment("What a mess day!"))
print(suggest("Aerplane"))

Terminal output:

(-0.21875, 0.175)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pattern/text/__init__.py", line 609, in _read
    raise StopIteration
StopIteration
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/umutsatir/Desktop/nda/sentiment.py", line 4, in <module>
    print(suggest("Aerplane"))
          ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pattern/text/en/__init__.py", line 207, in suggest
    return spelling.suggest(w)
           ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pattern/text/__init__.py", line 2677, in suggest
    if len(self) == 0:
       ^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pattern/text/__init__.py", line 376, in __len__
    return self._lazy("__len__")
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pattern/text/__init__.py", line 368, in _lazy
    self.load()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pattern/text/__init__.py", line 2621, in load
    for x in _read(self._path):
RuntimeError: generator raised StopIteration

I am newbie at StackOverFlow. If any data is missing, please let me know. Thanks!

umut_satir
  • 13
  • 4
  • I have just installed the python pattern package on my Linux box and I do not get the error. Checking my line numbers, it would appear that there are differences between your files and mine. What version of the package are you using? You can get this by running "import pattern" and "print(pattern.__version__)". – Malcolm Aug 09 '23 at 16:03
  • @Malcolm I have 3.6 right now – umut_satir Aug 09 '23 at 19:01
  • Hmm. That is what I have. – Malcolm Aug 10 '23 at 00:49
  • The error is that some how in its reading of self._path iterator (aka the generator) is failing rather than ending cleanly. I am not sure what would cause that. I am left to wonder if you have a corrupted file, or if there is some odd difference between Linux and OSX that causes the failure on your system, but not on mine. Perhaps reinstalling might fix it. – Malcolm Aug 10 '23 at 01:10
  • I tried reinstalling it but it does not work. – umut_satir Aug 10 '23 at 15:49
  • I am still puzzled by the difference in our line numbers, but I see another difference between us is that I am running python 3.10 while you are running python 3.11. There are some older posts about problems between python 3.6 and 3.7 (e.g. https://stackoverflow.com/questions/51700960/runtimeerror-generator-raised-stopiteration-every-time-i-try-to-run-app). – Malcolm Aug 10 '23 at 17:28

1 Answers1

0

(I am posting this as an answer to allow me to nicely format the code suggestions.)

Try editing your /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pattern/text/__init__.py file. Find the definition of the _read method. For me this is line 588. Then wrap the

for i, line in enumerate(f):
    ...

block with a try/except like

try:
    for i, line in enumerate(f):
        ...
except StopIteration:
    return
Malcolm
  • 461
  • 2
  • 10
  • I deleted raise StopIteration line under the block that you give me and I wrapped the code with try except commands than it worked. Thanks! – umut_satir Aug 18 '23 at 15:58