Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

multipal_col.py 4.5KB

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