nltk / nltk_book

NLTK Book
http://www.nltk.org/book
403 stars 143 forks source link

Chapter 10: example (48) does not parse #222

Open dod38fr opened 5 years ago

dod38fr commented 5 years ago

Hi

The example 48 in chapter 10 is not correct:

>>> expr4 =  read_expr(r'\P.exists x.(dog(x) & P(x))(\z.chase(y, z))')        
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 156, in parse
    result = self.process_next_expression(None)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 292, in process_next_expression
    accum = self.handle(tok, context)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 311, in handle
    return self.handle_lambda(tok, context)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 399, in handle_lambda
    accum = self.process_next_expression(tok)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 292, in process_next_expression
    accum = self.handle(tok, context)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 314, in handle
    return self.handle_quant(tok, context)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 429, in handle_quant
    accum = self.process_next_expression(tok)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 299, in process_next_expression
    return self.attempt_adjuncts(accum, context)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 324, in attempt_adjuncts
    expression = self.attempt_ApplicationExpression(expression, context)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 524, in attempt_ApplicationExpression
    + "' is not a Lambda Expression, an "
nltk.sem.logic.LogicalExpressionException: The function '(dog(x) & P(x))' is not a Lambda Expression, an Application Expression, or a functional predicate, so it may not take arguments.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 963, in fromstring
    return cls._logic_parser.parse(s, signature)
  File "/usr/lib/python3/dist-packages/nltk/sem/logic.py", line 161, in parse
    raise LogicalExpressionException(None, msg)
nltk.sem.logic.LogicalExpressionException: The function '(dog(x) & P(x))' is not a Lambda Expression, an Application Expression, or a functional predicate, so it may not take arguments.
\P.exists x.(dog(x) & P(x))(\z.chase(y, z))
                          ^

After adding parentheses, the expression is parsed fine and yields the expected result after simplification:

>>> expr4 =  read_expr(r'(\P.exists x.(dog(x) & P(x)))(\z.chase(y, z))')
>>> print(expr4.simplify())
exists x.(dog(x) & chase(y,x))

Hope this helps