我试图从其解析树中提取Chomsky Normal Form(CNF) - 一个句子的语法产生:
(ROOT
(S
(NP (DT the) (NNS kids))
(VP (VBD opened)
(NP (DT the) (NN box))
(PP (IN on)
(NP (DT the) (NN floor))))))
Run Code Online (Sandbox Code Playgroud)
我将整个树放入一个名为S的字符串中,然后:
tree = Tree.fromstring(S)
tree.chomsky_normal_form()
for p in tree.productions():
print p
Run Code Online (Sandbox Code Playgroud)
输出是
(1) NN -> 'box'
(2) PP -> IN NP
(3) DT -> 'the'
(4) ROOT -> S
(5) NP -> DT NN
(6) VBD -> 'opened'
(7) VP|<NP-PP> -> NP PP
(8) VP -> VBD VP|<NP-PP>
(9) NP -> DT …Run Code Online (Sandbox Code Playgroud) 我试图运行PLY的一个简单例子的第一部分,但我遇到了一个奇怪的错误.当我运行以下代码时,它给我一个关于lex.lex()的错误任何人都知道问题是什么?
import ply.lex as lex
tokens = [ 'NAME','NUMBER','PLUS','MINUS','TIMES', 'DIVIDE', 'EQUALS' ]
t_ignore = '\t'
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_EQUALS = r'='
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
lex.lex() # Build the lexer
Run Code Online (Sandbox Code Playgroud)
这是错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-e527bd224769> in <module>()
14 return t
15
---> 16 ply.lex.lex() # Build the lexer
c:\python27\lib\site-packages\ply\lex.pyc in lex(module, object, debug, optimize, lextab, reflags, nowarn, outputdir, debuglog, errorlog) …Run Code Online (Sandbox Code Playgroud)