【发布时间】:2021-08-04 14:41:33
【问题描述】:
我正在使用一个 XML 文件来存储我们创建的聊天机器人的所有“版本”。目前我们有 18 个版本,我只关心最新的一个。我正在尝试找到一种方法来提取此“v18”的所有botDialogGroup 元素及其关联的label 元素。 'botDialogGroup' 和 'label' 之间存在一对多的关系。
这是一个 XML 的 sn-p,其中 botDialogGroup 称为“Transfer”,label 称为“Transfer with a question”。并不是说这只是一个版本的 Bot,一共有 18 个。
链接到示例 XML 文件。 https://pastebin.com/aaDfBPUm
还要注意,fullName 是 botVersions 的子代。而botDialogGroup 和label 是botVersions 的孙子,他们的父母是botDialogs。
<Bot>
<botVersions>
<fullName>v18</fullName>
<botDialogs>
<botDialogGroup>Transfer</botDialogGroup>
<botSteps>
<botVariableOperation>
<askCollectIfSet>false</askCollectIfSet>
<botMessages>
<message>Would you like to chat with an agent?</message>
</botMessages>
<botQuickReplyOptions>
<literalValue>Yes</literalValue>
</botQuickReplyOptions>
<botQuickReplyOptions>
<literalValue>No</literalValue>
</botQuickReplyOptions>
<botVariableOperands>
<disableAutoFill>true</disableAutoFill>
<sourceName>YesOrNoChoices</sourceName>
<sourceType>MlSlotClass</sourceType>
<targetName>Transfer_To_Agent</targetName>
<targetType>ConversationVariable</targetType>
</botVariableOperands>
<optionalCollect>false</optionalCollect>
<quickReplyType>Static</quickReplyType>
<quickReplyWidgetType>Buttons</quickReplyWidgetType>
<retryMessages>
<message>I'm sorry, I didn't understand that. You have to select an option to proceed.</message>
</retryMessages>
<type>Collect</type>
</botVariableOperation>
<type>VariableOperation</type>
</botSteps>
<botSteps>
<botStepConditions>
<leftOperandName>Transfer_To_Agent</leftOperandName>
<leftOperandType>ConversationVariable</leftOperandType>
<operatorType>Equals</operatorType>
<rightOperandValue>No</rightOperandValue>
</botStepConditions>
<botSteps>
<botVariableOperation>
<botVariableOperands>
<targetName>Transfer_To_Agent</targetName>
<targetType>ConversationVariable</targetType>
</botVariableOperands>
<type>Unset</type>
</botVariableOperation>
<type>VariableOperation</type>
</botSteps>
<botSteps>
<botNavigation>
<botNavigationLinks>
<targetBotDialog>Main_Menu</targetBotDialog>
</botNavigationLinks>
<type>Redirect</type>
</botNavigation>
<type>Navigation</type>
</botSteps>
<type>Group</type>
</botSteps>
<botSteps>
<botStepConditions>
<leftOperandName>Transfer_To_Agent</leftOperandName>
<leftOperandType>ConversationVariable</leftOperandType>
<operatorType>Equals</operatorType>
<rightOperandValue>Yes</rightOperandValue>
</botStepConditions>
<botStepConditions>
<leftOperandName>Online_Product</leftOperandName>
<leftOperandType>ConversationVariable</leftOperandType>
<operatorType>NotEquals</operatorType>
<rightOperandValue>OTP</rightOperandValue>
</botStepConditions>
<botStepConditions>
<leftOperandName>Online_Product</leftOperandName>
<leftOperandType>ConversationVariable</leftOperandType>
<operatorType>NotEquals</operatorType>
<rightOperandValue>TCF</rightOperandValue>
</botStepConditions>
<botSteps>
<botVariableOperation>
<botVariableOperands>
<targetName>Transfer_To_Agent</targetName>
<targetType>ConversationVariable</targetType>
</botVariableOperands>
<type>Unset</type>
</botVariableOperation>
<type>VariableOperation</type>
</botSteps>
<botSteps>
<botNavigation>
<botNavigationLinks>
<targetBotDialog>Find_Business_Hours</targetBotDialog>
</botNavigationLinks>
<type>Call</type>
</botNavigation>
<type>Navigation</type>
</botSteps>
<type>Group</type>
</botSteps>
<botSteps>
<botNavigation>
<botNavigationLinks>
<targetBotDialog>Direct_Transfer</targetBotDialog>
</botNavigationLinks>
<type>Redirect</type>
</botNavigation>
<type>Navigation</type>
</botSteps>
<developerName>Transfer_To_Agent</developerName>
<label>Transfer with a question</label>
<mlIntent>Transfer_To_Agent</mlIntent>
<mlIntentTrainingEnabled>true</mlIntentTrainingEnabled>
<showInFooterMenu>false</showInFooterMenu>
</botDialogs>
</botVersions>
</Bot>
当前脚本
我遇到的问题是它将搜索整个树,所有 18 个版本,以查找 botDialogGroup 和 label 元素,因为我使用的是 findall()。而我只希望它搜索最近的fullName 的botVersions,在这种情况下是“v18”。
手动输入“v18”不是问题,因为我总是知道要查找的版本。而且它很有用,因为不同的机器人有不同的版本。
import xml.etree.ElementTree as ET
import pandas as pd
cols = ["BotVersion", "DialogGroup", "Dialog"]
rows = []
tree = ET.parse('ChattyBot.xml')
root = tree.getroot()
for fullName in root.findall(".//fullName[.='v18']"):
for botDialogGroup in root.findall(".//botDialogGroup"):
for label in root.findall(".//label"):
print(fullName.text, botDialogGroup.text, label.text)
rows.append({"BotVersion": fullName.text,
"DialogGroup": botDialogGroup.text,
"Dialog": label.text})
df = pd.DataFrame(rows, columns=cols)
df.to_csv("botcsvfile.csv")
使用 pandas 将所需的最终结果保存到 csv 文件。
| BotVersion | DialogGroup | Dialog |
|---|---|---|
| v18 | Transfer | Transfer with a question |
【问题讨论】:
-
如果最新版本是 v19 怎么办?需要修改代码吗?
-
@balderman 是的,我会下载一个新的 XML 文件,将版本更改为“v19”,然后运行脚本。这是意料之中的,因为有多个机器人都有不同数量的版本。
-
xml 可以包含多少个类似
<fullName>vXY</fullName>的条目?当前代码有什么问题? -
每个版本有一个条目。我认为可以创建的版本数量没有限制。
-
当前代码的问题如上所述,我有 18 个版本,它将在所有 18 个版本中搜索
botDialogGroup和label元素。我只希望它搜索这些元素的最新版本,在本例中为版本 18。
标签: python xml elementtree