nltk 解析字符串中高级表达式的语法错误

wlsrxk51  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(45)
nltk/nltk/inference/tableau.py
第681行到第694行:
|  | def test_higher_order_tableau_prover(): |
|  | tableau_test("believe(j, -lie(b))", ["believe(j, -lie(b) & -cheat(b))"]) |
|  | tableau_test("believe(j, lie(b) & cheat(b))", ["believe(j, lie(b))"]) |
|  | tableau_test( |
|  | "believe(j, lie(b))", ["lie(b)"] |
|  | ) # 我们如何捕获约翰相信所有真实的事情? |
|  | tableau_test( |
|  | "believe(j, know(b, cheat(b)))", |
|  | ["believe(j, know(b, lie(b)) & know(b, steals(b) & cheat(b)))"], |
|  | ) |
|  | tableau_test("P(Q(y), R(y) & R(z))", ["P(Q(x) & Q(y), R(y) & R(z))"]) |
|  | |
|  | tableau_test("believe(j, cheat(b) & lie(b))", ["believe(j, lie(b) & cheat(b))"]) |
|  | tableau_test("believe(j, -cheat(b) & -lie(b))", ["believe(j, -lie(b) & -cheat(b))"]) |

from nltk.inference.tableau import *
testHigherOrderTableauProver()


给出以下错误:

UnexpectedTokenException Traceback (most recent call last)
~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/sem/logic.py in parse(self, data, signature)
155 try:
--> 156 result = self.process_next_expression(None)
157 if self.inRange(0):

~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/sem/logic.py in process_next_expression(self, context)
291
--> 292 accum = self.handle(tok, context)
293

~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/sem/logic.py in handle(self, tok, context)
304 if self.isvariable(tok):
--> 305 return self.handle_variable(tok, context)
306

~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/sem/logic.py in handle_variable(self, tok, context)
360 )
--> 361 self.assertNextToken(Tokens.CLOSE)
362 return accum

~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/sem/logic.py in assertNextToken(self, expected)
572 if tok != expected:
--> 573 raise UnexpectedTokenException(self._currentIndex, tok, expected)
574

UnexpectedTokenException: Unexpected token: '&'. Expected token ')'.

During handling of the above exception, another exception occurred:

LogicalExpressionException Traceback (most recent call last)
in
----> 1 testHigherOrderTableauProver()

~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/inference/tableau.py in testHigherOrderTableauProver()
681
682 def testHigherOrderTableauProver():
--> 683 tableau_test('believe(j, -lie(b))', ['believe(j, -lie(b) & -cheat(b))'])
684 tableau_test('believe(j, lie(b) & cheat(b))', ['believe(j, lie(b))'])
685 tableau_test(

~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/inference/tableau.py in tableau_test(c, ps, verbose)
698 def tableau_test(c, ps=None, verbose=False):
699 pc = Expression.fromstring(c)
--> 700 pps = [Expression.fromstring(p) for p in ps] if ps else []
701 if not ps:
702 ps = []

~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/inference/tableau.py in (.0)
698 def tableau_test(c, ps=None, verbose=False):
699 pc = Expression.fromstring(c)
--> 700 pps = [Expression.fromstring(p) for p in ps] if ps else []
701 if not ps:
702 ps = []

~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/sem/logic.py in fromstring(cls, s, type_check, signature)
961 return cls._type_checking_logic_parser.parse(s, signature)
962 else:
--> 963 return cls._logic_parser.parse(s, signature)
964
965 def call(self, other, *additional):

~/.conda/envs/data_science/lib/python3.6/site-packages/nltk/sem/logic.py in parse(self, data, signature)
159 except LogicalExpressionException as e:
160 msg = '%s\n%s\n%s^' % (e, data, ' ' * mapping[e.index - 1])
--> 161 raise LogicalExpressionException(None, msg)
162
163 if self.type_check:

LogicalExpressionException: Unexpected token: '&'. Expected token ')'.
believe(j, -lie(b) & -cheat(b))


似乎 `Expression.fromstring` 方法在缺乏高阶 predicate 的命题参数周围缺少括号时卡住了。以下似乎可以工作:

from nltk.inference.tableau import *
tableau_test("believe(j, -lie(b))", ["believe(j, (-lie(b) & -cheat(b)))"])
tableau_test("believe(j, (lie(b) & cheat(b)))", ["believe(j, lie(b))"])
tableau_test(
"believe(j, lie(b))", ["lie(b)"]
) # how do we capture that John believes all things that are true
tableau_test(
"believe(j, know(b, cheat(b)))",
["believe(j, (know(b, lie(b)) & know(b, (steals(b) & cheat(b)))))"],
)
tableau_test("P(Q(y), (R(y) & R(z)))", ["P((Q(x) & Q(y)), (R(y) & R(z)))"])

tableau_test("believe(j, (cheat(b) & lie(b)))", ["believe(j, (lie(b) & cheat(b)))"])
tableau_test("believe(j, (-cheat(b) & -lie(b)))", ["believe(j, (-lie(b) & -cheat(b)))"])


如果需要,愿意进行进一步的测试或提交PR。谢谢!
[编辑:删除了意外的"not";修复了拼写错误"to"]
zd287kbt

zd287kbt1#

看起来这个问题在ticks中并不存在:tableau_test('believe(j, -lie(b))', ['believe(j, -lie(b) & -cheat(b))'])。所以很可能需要在上面提到的地方进行更改。在89eb7ae658c34124d72c1dc4a3a9182a12723cee中将双引号更改为单引号。@alvations,有什么想法吗?

相关问题