umut_satir
umut_satir

Reputation: 15

Program raised StopIteration error when using pattern library on Python

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!

Upvotes: 1

Views: 303

Answers (1)

Malcolm
Malcolm

Reputation: 839

(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

Upvotes: 0

Related Questions