全部
我找到了一段代码来解析简单的 ARFF 文件,我想将其更改为适合稀疏 ARFF,其数据如下所示:
@data
{0 12,4 37,8 First,20 'Some Thing'}
{0 12,13 First,28 'Some Thing'}
这是代码:
def ParseFromSimpleARFF(data):
arffFormat = Forward()
E = CaselessLiteral("E")
comment = '%' + restOfLine
relationToken = Keyword('@RELATION', caseless=True)
dataToken = Keyword('@DATA', caseless=True)
attribToken = Keyword('@ATTRIBUTE', caseless=True)
ident = Word( alphas, alphanums + '_-' ).setName('identifier')
relation = Suppress(relationToken) \
+ ident.setResultsName('relation')
classDomain = Suppress('{') \
+ Group(delimitedList(ident.setResultsName('domain'))).setResultsName('domains') + Suppress('}')
attribute = Group(Suppress(attribToken)
+ Word(alphas).setResultsName('attrname')+(Word(alphas)|classDomain).setResultsName('type')).setResultsName('attribute')
arithSign = Word("+-",exact=1)
realNum = Combine( Optional(arithSign)
+ (Word( nums ) + "." + Optional( Word(nums) )|( "." + Word(nums) ))
+ Optional( E + Optional(arithSign) + Word(nums) ))
**#dataList = Group(delimitedList(realNum|ident)).setResultsName('record')
dataList = Suppress('{') + Group( delimitedList(realNum|ident)).setResultsName('record') + Suppress('}')**
arffFormat << ( relation
+ OneOrMore(attribute).setResultsName('attributes')
+ dataToken
+ OneOrMore(dataList).setResultsName('records')).setResultsName('arffdata')
simpleARFF = arffFormat
simpleARFF.ignore(comment)
tokens = simpleARFF.parseString(data)
return tokens
但它不起作用
我想我必须告诉程序识别空白,但我不知道如何
非常感谢