#-*-coding:utf-8 -*- requirements = ["pyserial"] def check_requirement(package): try: exec("import {0}".format(package)) except ModuleNotFoundError: if package == 'yaml': package = 'pyyaml' #inquiry = input("This script requires {0}. Do you want to install {0}? [y/n]".format(package)) #while (inquiry != "y") and (inquiry != "n"): # inquiry = input("This script requires {0}. Do you want to install {0}? [y/n]".format(package)) import os print("Execute commands: pip install {0}".format(package)) os.system("pip install {0}".format(package)) ''' if inquiry == "y": import os print("Execute commands: pip install {0}".format(package)) os.system("pip install {0} -y".format(package)) else: print("{0} is missing, so the program exists!".format(package)) exit(-1) ''' for requirement in requirements: check_requirement(requirement) import yaml import os, sys, re sys.path.append(os.path.dirname(os.path.dirname(__file__))) def stories_generator(name): # 在下面的代码行中使用断点来调试脚本。 print(f'Hi, {name}') # 按 Ctrl+F8 切换断点。 #curPath = os.path.dirname(os.path.realpath(__file__) # 获取文件当前路径 dataDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) # root目录 storiesFilePath = os.path.join(dataDir, "data" + os.sep + "nlu.yml") # 获取yaml文件地址 f = open(storiesFilePath,'r',encoding='utf-8') stories = yaml.load(f,Loader=yaml.FullLoader) # 读取yaml文件 #print(stories) #steps = [" - intent: greet\n user: | -\n 你好\n"] f = open('./nlu_stories.yml', 'w', encoding='utf-8') f.write("{}\n".format('version: "2.0"')) f.write("{}\n".format('stories:')) f.write("{}\n".format('- story: Story from Conversation')) f.write("{}\n".format(' steps:')) intents = stories['nlu'] for intent in intents: #print(intent) #mainStr = '' #main字符串 #subSynonymStr = '' #sub同义词 #mainSynonymStr = '' #main同义词 if 'intent' in intent: if intent['intent'] == 'inform_protocol': exampleArr = intent['examples'].split('- ') for example in exampleArr: maintag = 0 subtag = 0 if example != '': #print('example:' + example) f.write("{}\n".format(" - intent: inform_protocol")) f.write("{}\n".format(" user: |-")) f.write("{}".format(" " + example)) mainStr = r"(main)" if mainStr in example: list1 = re.findall(mainStr, example) mainStartS = [each.start() for each in re.finditer(mainStr, example)] #mainStartS = re.finditer(mainStr, example) mainEnds = [start + len(mainStr) - 1 for start in mainStartS] for index in range(len(mainStartS)): i = mainStartS[index] - 2 #print(example[i]) if example[i] != ']': break while i >= 0: if example[i] == '[': mainStr = example[i + 1:mainStartS[index] - 2] break i -= 1 # 构架intent中的main if maintag == 0: f.write("{}\n".format(" - slot_was_set:")) f.write("{}\n".format(" - main:")) maintag = 1 f.write("{}\n".format(" - " + mainStr)) f.flush() # 构建同义词 if ']{"entity": "main", "value":' in example: mainSynStr = '{"entity": "main", "value":' mainSynStartS = [each.start() for each in re.finditer(mainSynStr, example)] mainSynEnds = [start + len(mainSynStr) - 1 for start in mainSynStartS] for index in range(len(mainSynStartS)) : i = mainSynStartS[index] - 2 #取main字符串 while i >= 0: if example[i] == '[': mainSynStr = example[i + 1:mainSynStartS[index] - 1] #print('mainSynStr: ' + mainSynStr) break i -= 1 #取同义词字符串 j = mainSynEnds[index] + 1 exampleSubStr = example[j:] mainSynonymStr = exampleSubStr.split('\"')[1] # 构建intent中的同义词 if maintag == 0: f.write("{}\n".format(" - slot_was_set:")) f.write("{}\n".format(" - main:")) maintag = 1 f.write("{}\n".format(" - " + mainSynStr)) f.write("{}\n".format(" - " + mainSynonymStr)) f.flush() # 构建sub subStr = '(sub)' if subStr in example: subStarts = [each.start() for each in re.finditer(subStr, example)] for substart in subStarts: i = substart - 2 #i = subStarts[0] - 2 while i >= 0 : if example[i] == '[': subStr = example[i + 1:substart - 2] break i -= 1 # 构建intent中的sub if subtag == 0: f.write("{}\n".format(" - slot_was_set:")) f.write("{}\n".format(" - sub:")) subtag = 1 f.write("{}\n".format(" - " + subStr)) #构建intent中的sub同义词 if ']{"entity": "sub", "value":' in example: subSynStr = '{"entity": "sub", "value":' subSynStartS = [each.start() for each in re.finditer(subSynStr, example)] subSynEnds = [start + len(subSynStr) - 1 for start in subSynStartS] for index in range(len(subSynStartS)): i = subSynStartS[index] - 2 #取main字符串 while i >= 0: if example[i] == '[': subSynStr = example[i + 1:subSynStartS[index] - 1] break i -= 1 # 取同义词字符串 j = subSynEnds[index] + 1 exampleSynSubStr = example[j:] subSynonymStr = exampleSynSubStr.split('\"')[1] # 构建intent中的sub同义词 if subtag == 0: f.write("{}\n".format(" - slot_was_set:")) f.write("{}\n".format(" - sub:")) subtag = 1 f.write("{}\n".format(" - " + subSynStr)) f.write("{}\n".format(" - " + subSynonymStr )) # slot_was_set加入到intent中 #f.write("{}\n".format(" - action: utter_protocol_submit")) f.write("{}\n".format(" - action: action_check_protocol")) f.write("{}\n".format(" - action: action_initial_protocol")) f.close() print('Generate Stories finish!') # 按间距中的绿色按钮以运行脚本。 if __name__ == '__main__': stories_generator('NLU')
rasa NLU.yml生成批量验证stories
最新推荐文章于 2024-01-01 23:29:38 发布