Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import pandas as pd
  2. import yaml
  3. import re
  4. # Read the data from the CSV file
  5. df = pd.read_csv(r"D:\livecode\chatbot\AI Support Questionaire - Sheet1 (2).csv", encoding='latin')
  6. def remove_special_characters(df):
  7. for column in df.columns:
  8. df[column] = df[column].apply(lambda x: re.sub(r'[^a-zA-Z0-9->]', ' ', str(x)))
  9. return df
  10. df_cleaned = remove_special_characters(df)
  11. uniqueid = df['UniqueId'].tolist()
  12. # Answers = df['Answers'].tolist()
  13. question_cols = df.filter(regex='Question').columns
  14. answer_cols = df.filter(regex='Answers').columns
  15. #appending intents
  16. import ruamel.yaml as yaml
  17. # Load the domain YAML file
  18. with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'r') as f:
  19. domain = yaml.safe_load(f)
  20. for i in uniqueid:
  21. domain['intents'].append(i)
  22. with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'w') as f:
  23. yaml.dump(domain, f, default_flow_style=False, allow_unicode=True)
  24. question_cols = df.filter(regex='Question').columns
  25. answer_cols = df.filter(regex='Answer').columns
  26. # for i in question_cols:
  27. # df[i] = df[i].replace('\n', '$$', regex=True)
  28. # # df[i] = df[i].str.strip()
  29. # for i in answer_cols:
  30. # df[i] = df[i].replace('\n', '$$', regex=True)
  31. # # df[i] = df[i].str.strip()
  32. from ruamel.yaml import YAML
  33. # Create a YAML object that preserves the formatting of the original YAML file
  34. yaml = YAML()
  35. yaml.preserve_quotes = True
  36. yaml.indent(mapping=2, sequence=4, offset=2)
  37. # Read in the existing domain file
  38. with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'r', encoding='utf-8') as file:
  39. domain = yaml.load(file)
  40. # Generate the new YAML code for each row of data
  41. new_content = {}
  42. for index, row in df.iterrows():
  43. intent_name = row['UniqueId']
  44. examples = [row[col] for col in answer_cols ]
  45. separator = "_"
  46. for counter, example in enumerate(examples, start=1):
  47. #example=example.replace(':','')
  48. response_name = f"utter_{intent_name}{separator}{counter}"
  49. response_text =f"- text: {example}"
  50. if 'responses' not in new_content:
  51. new_content['responses'] = {}
  52. new_content['responses'][response_name] = yaml.load(response_text)
  53. # Update the `responses` section of the domain dictionary with the new content
  54. domain['responses'].update(new_content['responses'])
  55. # Write the updated domain file back to disk, preserving the formatting of the original file
  56. with open(r'D:\\livecode\\chatbot\\traildata\\domain.yml', 'w',encoding='utf-8') as file:
  57. yaml.dump(domain, file)
  58. #appending multipal action for each rule
  59. with open(r"D:\\livecode\\chatbot\\traildata\\rules.yml", 'a') as f:
  60. for index, row in df.iterrows():
  61. intent_name = row['UniqueId']
  62. examples = [row[col] for col in answer_cols]
  63. separator = "_"
  64. steps = {"intent": intent_name}
  65. gg=[]
  66. for counter, example in enumerate(examples, start=1):
  67. unique_id = f"{intent_name}{separator}{counter}"
  68. gg.append({"action": f"utter_{unique_id}"})
  69. output_str = " \n ".join([f"- action: {question['action'][:-1]}{i}" for i, question in enumerate(gg, start=1)])
  70. f.write(f"""
  71. - rule: Ticket{intent_name}
  72. steps:
  73. - intent: {steps['intent']}
  74. {output_str}
  75. - action: action_service
  76. """)
  77. with open(r'D:\\livecode\\chatbot\\traildata\\nlu.yml', "a",encoding='utf-8') as file:
  78. for index, row in df.iterrows():
  79. intent_name = row['UniqueId']
  80. examples = [row[col] for col in question_cols]
  81. file.write(f"""
  82. - intent: {intent_name}
  83. examples: |
  84. """)
  85. for example in examples:
  86. file.write(f" - \"{example}\"\n")
  87. file.write("\n")
  88. # filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\nlu.yml"
  89. # string_to_remove = ' - "nan"'
  90. # with open(filename, "r") as f:
  91. # text = f.read()
  92. # modified_text = text.replace(string_to_remove, "")
  93. # with open(filename, "w") as f:
  94. # f.write(modified_text)
  95. # filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\domain.yml"
  96. # string_to_remove = ' - "nan"'
  97. # with open(filename, "r") as f:
  98. # text = f.read()
  99. # modified_text = text.replace(string_to_remove, "")
  100. # with open(filename, "w") as f:
  101. # f.write(modified_text)
  102. # filename = r"C:\Users\Bizgaze\Desktop\fileupdation\trail_update\data\rules.yml"
  103. # string_to_remove = ' - "nan"'
  104. # with open(filename, "r") as f:
  105. # text = f.read()
  106. # modified_text = text.replace(string_to_remove, "")
  107. # with open(filename, "w") as f:
  108. # f.write(modified_text)