好的,我终于得到了捕获所有测试用例的语法,但我有一个重复(案例 3)和误报(案例 6,“PATTERN 5”)。这是我的测试用例和我想要的输出。
我对 python 还是很陌生(虽然能够教我的孩子!可怕!)所以我确信有明显的方法可以解决这个问题,我什至不确定这是一个 pyparsing 问题。这是我的输出现在的样子:
['01/01/01','S01-12345','20/111-22-1001',['GLEASON', ['5', '+', '4'], '=', '9']]
['02/02/02','S02-1234','20/111-22-1002',['GLEASON', 'SCORE', ':', ['3', '+', '3'], '=', '6']]
['03/02/03','S03-1234','31/111-22-1003',['GLEASON', 'GRADE', ['4', '+', '3'], '=', '7']]
['03/02/03','S03-1234','31/111-22-1003',['GLEASON', 'SCORE', ':', '7', '=', ['4', '+', '3']]]
['04/17/04','S04-123','30/111-22-1004',['GLEASON', 'SCORE', ':', ['3', '+', '4', '-', '7']]]
['05/28/05','S05-1234','20/111-22-1005',['GLEASON', 'SCORE', '7', '[', ['3', '+', '4'], ']']]
['06/18/06','S06-10686','20/111-22-1006',['GLEASON', ['4', '+', '3']]]
['06/18/06','S06-10686','20/111-22-1006',['GLEASON', 'PATTERN', '5']]
['07/22/07','S07-2749','20/111-22-1007',['GLEASON', 'SCORE', '6', '(', ['3', '+', '3'], ')']]
这是语法
num = Word(nums)
arith_expr = operatorPrecedence(num,
[
(oneOf('-'), 1, opAssoc.RIGHT),
(oneOf('* /'), 2, opAssoc.LEFT),
(oneOf('+ -'), 2, opAssoc.LEFT),
])
accessionDate = Combine(num + "/" + num + "/" + num)("accDate")
accessionNumber = Combine("S" + num + "-" + num)("accNum")
patMedicalRecordNum = Combine(num + "/" + num + "-" + num + "-" + num)("patientNum")
score = (Optional(oneOf('( [')) +
arith_expr('lhs') +
Optional(oneOf(') ]')) +
Optional(oneOf('= -')) +
Optional(oneOf('( [')) +
Optional(arith_expr('rhs')) +
Optional(oneOf(') ]')))
gleason = Group("GLEASON" + Optional("SCORE") + Optional("GRADE") + Optional("PATTERN") + Optional(":") + score)
patientData = Group(accessionDate + accessionNumber + patMedicalRecordNum)
partMatch = patientData("patientData") | gleason("gleason")
和输出函数。
lastPatientData = None
for match in partMatch.searchString(TEXT):
if match.patientData:
lastPatientData = match
elif match.gleason:
if lastPatientData is None:
print "bad!"
continue
# getParts()
FOUT.write( "['{0.accDate}','{0.accNum}','{0.patientNum}',{1}]\n".format(lastPatientData.patientData, match.gleason))
如您所见,输出并不像看起来那么好,我只是在写入文件并伪造一些语法。我一直在为如何获取 pyparsing 中间结果而苦苦挣扎,以便我可以使用它们。我应该把它写出来并运行第二个找到重复项的脚本吗?
更新,基于 Paul McGuire 的回答。这个函数的输出让我每个条目只剩下一行,但现在我丢失了分数(每个 Gleason 分数,在智力上,都有形式primary + secondary = total
。这是一个数据库,所以 pri,sec,tot 是分开的posgresql 列,或者,对于解析器的输出,逗号分隔的值)
accumPatientData = None
for match in partMatch.searchString(TEXT):
if match.patientData:
if accumPatientData is not None:
#this is a new patient data, print out the accumulated
#Gleason scores for the previous one
writeOut(accumPatientData)
accumPatientData = (match.patientData, [])
elif match.gleason:
accumPatientData[1].append(match.gleason)
if accumPatientData is not None:
writeOut(accumPatientData)
所以现在输出看起来像这样
01/01/01,S01-12345,20/111-22-1001,9
02/02/02,S02-1234,20/111-22-1002,6
03/02/03,S03-1234,31/111-22-1003,7,4+3
04/17/04,S04-123,30/111-22-1004,
05/28/05,S05-1234,20/111-22-1005,3+4
06/18/06,S06-10686,20/111-22-1006,,
07/22/07,S07-2749,20/111-22-1007,3+3
我想回到那里并抓住一些丢失的元素,重新排列它们,找到丢失的元素,然后将它们全部放回去。像这样的伪代码:
def diceGleason(glrhs,gllhs)
if glrhs.len() == 0:
pri = gllhs[0]
sec = gllhs[2]
tot = pri + sec
return [pri, sec, tot]
elif glrhs.len() == 1:
pri = gllhs[0]
sec = gllhs[2]
tot = glrhs
return [pri, sec, tot]
else:
pri = glrhs[0]
sec = glrhs[2]
tot = gllhs
return [pri, sec, tot]
更新 2:好的,保罗很棒,但我很笨。完全按照他所说的进行了尝试,我尝试了几种获取 pri、sec 和 tot 的方法,但我失败了。我不断收到这样的错误:
Traceback (most recent call last):
File "Stage1.py", line 81, in <module>
writeOut(accumPatientData)
File "Stage1.py", line 47, in writeOut
FOUT.write( "{0.accDate},{0.accNum},{0.patientNum},{1.pri},{1.sec},{1.tot}\n".format( pd, gleaso
nList))
AttributeError: 'list' object has no attribute 'pri'
这些 AttributeErrors 是我不断得到的。显然我不明白两者之间发生了什么(保罗,我有这本书,我发誓它在我面前打开,我不明白)。这是我的脚本。有什么东西在错误的地方吗?我是否称结果错误?