Python / PyParsing:遇到setResultsName问题
作者:互联网
我认为我在调用setResultsName()时犯了一个错误:
from pyparsing import *
DEPT_CODE = Regex(r'[A-Z]{2,}').setResultsName("Dept Code")
COURSE_NUMBER = Regex(r'[0-9]{4}').setResultsName("Course Number")
COURSE_NUMBER.setParseAction(lambda s, l, toks : int(toks[0]))
course = DEPT_CODE + COURSE_NUMBER
course.setResultsName("course")
statement = course
从IDLE:
>>> myparser import *
>>> statement.parseString("CS 2110")
(['CS', 2110], {'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})
我希望输出:
>>> myparser import *
>>> statement.parseString("CS 2110")
(['CS', 2110], {'Course': ['CS', 2110], 'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})
setResultsName()仅适用于终端吗?
解决方法:
如果您将课程的定义更改为
course = (DEPT_CODE + COURSE_NUMBER).setResultsName("Course")
您得到以下行为:
x=statement.parseString("CS 2110")
print(repr(x))
# (['CS', 2110], {'Course': [((['CS', 2110], {'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]}), 0)], 'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})
print(x['Dept Code'])
# CS
print(x['Course Number'])
# 2110
print(x['Course'])
# ['CS', 2110]
这不完全是您想要的代表,但是就足够了吗?
注意,from the docs:
[setResultsName] returns a copy of
the original ParserElement object;
this is so that the client can define
a basic element, such as an integer,
and reference it in multiple places
with different names.
因此,course.setResultsName(“ Course”)不起作用,因为它不会影响课程.您将不得不说course = course.setResultsName(“ Course”).那是做我上面的事情的另一种方式.
标签:pyparsing,nlp,python 来源: https://codeday.me/bug/20191024/1917524.html