我想转换为:
输入:
"#serviceRequest and @charges:getRoamingCharges or @plans:dataplans"
其中“#”-意图 “@”-实体 “:”-值
输出:
{"and":[
{"some" : [ {"var":"intents"}, {"==":[{"var":"intent"}]},
"serviceRequest"]},
{"or":[
{"and":[{"some" : [ {"var":"entities"}, {"==":[{"var":"entity"},
"charges"]} ]},
{"some" : [ {"var":"entities"}, {"==":[{"var":"value"},
"getRoamingCharges"]} ]}]
},{"and":[
{"some" : [ {"var":"entities"}, {"==":[{"var":"entity"}, "plans"]}
]},
{"some" : [ {"var":"entities"}, {"==":[{"var":"value"}, "data
plans"]} ]}
]}
]}
]}
我尝试过:
import pyparsing
identifier = pyparsing.QuotedString('"')
operator = (
pyparsing.Literal("==") |
pyparsing.Literal("≠") |
pyparsing.Literal("≥") |
pyparsing.Literal("≤") |
pyparsing.Literal("<") |
pyparsing.Literal(">")
)
value = pyparsing.QuotedString('"')
match_format = identifier + operator + value
#print(match_format.parseString('"foobar"=="123"'))
def list_to_dict(pos, tokens):
dic = {}
lis =[]
print(tokens)
abc= {tokens[1]: {tokens[2], tokens[0]}}
print(abc)
lis.append(abc)
dic['bfeh']=lis
return tokens
match_format = (identifier + operator +
value).setParseAction(list_to_dict)
print(match_format.parseString('"intent"=="serviceRequest"'))
提供:
{'==': {'intent', 'serviceRequest'}}
请帮助我使用解析(Python)或任何你想要的替代方法?你知道吗
您可以创建一个更简单的标记器来链接解析器:
现在,可以创建一个简单的解析器:
现在,组合组件:
输出:
毫无疑问,对于这个问题有较短的解决方案,但是,标记器和解析器的目标都是使您能够更容易地在将来扩展这个解决方案,以处理不能由更“黑客”的解决方案处理的输入。你知道吗
相关问题 更多 >
编程相关推荐