Git Product home page Git Product logo

Comments (5)

ChenYang-Huang avatar ChenYang-Huang commented on June 12, 2024 5

The fix is proposed in many pull requests but it looks like the maintainer hasn't been active.

To fix it, go to $installation_path_of_pattern/text/__init__.py
Change line 608-609
yield line
raise StopIteration

to
image
This has made conjugate() working for me in Python 3.7.4

from pattern.

eterna2 avatar eterna2 commented on June 12, 2024 4

instead of changing the source code, u can do monkey patching. i.e.


import os.path

import pattern.text

from pattern.helpers import decode_string
from codecs import BOM_UTF8

BOM_UTF8 = BOM_UTF8.decode("utf-8")
decode_utf8 = decode_string


def _read(path, encoding="utf-8", comment=";;;"):
    """Returns an iterator over the lines in the file at the given path,
    strippping comments and decoding each line to Unicode.
    """
    if path:
        if isinstance(path, str) and os.path.exists(path):
            # From file path.
            f = open(path, "r", encoding="utf-8")
        elif isinstance(path, str):
            # From string.
            f = path.splitlines()
        else:
            # From file or buffer.
            f = path
        for i, line in enumerate(f):
            line = line.strip(BOM_UTF8) if i == 0 and isinstance(line, str) else line
            line = line.strip()
            line = decode_utf8(line, encoding)
            if not line or (comment and line.startswith(comment)):
                continue
            yield line


pattern.text._read = _read

from pattern.

juanpenia avatar juanpenia commented on June 12, 2024 1

Hola, esto esta arreglado pero no en un release oficial, sino un fix de la comunidad. Es por una modificacion que se da a partir de 3.7+ que tiene que ver con algo de los generadores y que se yo que.

NicolasBizzozzero@2867928

La solución esta ahi, pero yo tambien hice un fork y mergie una branch con arreglos de la comunidad.

from pattern.

aghasemi avatar aghasemi commented on June 12, 2024

Hi. Any idea how we can apply that fix on the library that hs been installed via pip?

from pattern.

nershman avatar nershman commented on June 12, 2024

Note that in my experience another, lazier fix, is that in your code you can run parse() 2-3 times and then the StopIteration error will stop occuring.

>>> from pattern.en import parse
>>> parse('This is a test.')
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/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 "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/pattern/text/en/__init__.py", line 169, in parse
    return parser.parse(s, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 1172, in parse
    s[i] = self.find_tags(s[i], **kwargs)
  File "/usr/local/lib/python3.8/site-packages/pattern/text/en/__init__.py", line 114, in find_tags
    return _Parser.find_tags(self, tokens, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 1113, in find_tags
    lexicon = kwargs.get("lexicon", self.lexicon or {}),
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 376, in __len__
    return self._lazy("__len__")
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 368, in _lazy
    self.load()
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 625, in load
    dict.update(self, (x.split(" ")[:2] for x in _read(self._path) if len(x.split(" ")) > 1))
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 625, in <genexpr>
    dict.update(self, (x.split(" ")[:2] for x in _read(self._path) if len(x.split(" ")) > 1))
RuntimeError: generator raised StopIteration
>>> parse('This is a test.')
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/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 "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/pattern/text/en/__init__.py", line 169, in parse
    return parser.parse(s, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 1172, in parse
    s[i] = self.find_tags(s[i], **kwargs)
  File "/usr/local/lib/python3.8/site-packages/pattern/text/en/__init__.py", line 114, in find_tags
    return _Parser.find_tags(self, tokens, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 1112, in find_tags
    return find_tags(tokens,
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 1540, in find_tags
    tagged = entities.apply(tagged)
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 976, in apply
    if w in self:
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 382, in __contains__
    return self._lazy("__contains__", *args)
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 368, in _lazy
    self.load()
  File "/usr/local/lib/python3.8/site-packages/pattern/text/__init__.py", line 959, in load
    for x in _read(self.path):
RuntimeError: generator raised StopIteration
>>> parse('This is a test.')
'This/DT/O/O is/VBZ/B-VP/O a/DT/B-NP/O test/NN/I-NP/O ././O/O'

from pattern.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.