Spaces:
Running
Running
Create tests/follow_up_response.py
Browse files- tests/follow_up_response.py +90 -0
tests/follow_up_response.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
def parse_text(input_text):
|
4 |
+
# Define patterns for response and clarification
|
5 |
+
response_pattern = re.compile(r'<response>(.*?)<\/response>', re.DOTALL)
|
6 |
+
clarification_pattern = re.compile(r'<clarification>(.*?)<\/clarification>', re.DOTALL)
|
7 |
+
|
8 |
+
# Find all matches for response and clarification
|
9 |
+
response_matches = response_pattern.finditer(input_text)
|
10 |
+
clarification_matches = clarification_pattern.finditer(input_text)
|
11 |
+
|
12 |
+
# Initialize variables to keep track of the position
|
13 |
+
last_end = 0
|
14 |
+
combined_response = ""
|
15 |
+
parsed_clarifications = []
|
16 |
+
|
17 |
+
# Combine responses and capture everything in between
|
18 |
+
for response_match in response_matches:
|
19 |
+
# Capture text before the current response tag
|
20 |
+
combined_response += input_text[last_end:response_match.start()].strip() + "\n"
|
21 |
+
# Add the response content
|
22 |
+
combined_response += response_match.group(1).strip() + "\n"
|
23 |
+
# Update the last end position
|
24 |
+
last_end = response_match.end()
|
25 |
+
|
26 |
+
# Check for clarifications and parse them
|
27 |
+
for clarification_match in clarification_matches:
|
28 |
+
# Capture text before the current clarification tag
|
29 |
+
combined_response += input_text[last_end:clarification_match.start()].strip() + "\n"
|
30 |
+
# Process the clarification block
|
31 |
+
clarification_text = clarification_match.group(1).strip()
|
32 |
+
if clarification_text:
|
33 |
+
# Split by "text:" to separate each question block
|
34 |
+
question_blocks = clarification_text.split("- text:")
|
35 |
+
|
36 |
+
# Loop through each block and extract the question and its options
|
37 |
+
for block in question_blocks[1:]:
|
38 |
+
# Extract the question using regex (up to the "options:" part)
|
39 |
+
question_match = re.search(r'^(.*?)\s*options:', block, re.DOTALL)
|
40 |
+
if question_match:
|
41 |
+
question = question_match.group(1).strip()
|
42 |
+
|
43 |
+
# Extract the options using regex
|
44 |
+
options_match = re.search(r'options:\s*(.*?)$', block, re.DOTALL)
|
45 |
+
if options_match:
|
46 |
+
options = [option.strip() for option in options_match.group(1).split('-') if option.strip()]
|
47 |
+
|
48 |
+
# Add the parsed question and options to the list
|
49 |
+
parsed_clarifications.append({'question': question, 'options': options})
|
50 |
+
# Update the last end position
|
51 |
+
last_end = clarification_match.end()
|
52 |
+
|
53 |
+
# Capture any remaining text after the last tag
|
54 |
+
combined_response += input_text[last_end:].strip()
|
55 |
+
|
56 |
+
return combined_response.strip(), parsed_clarifications
|
57 |
+
|
58 |
+
# Example usage
|
59 |
+
input_text = """
|
60 |
+
Some introductory text that should be included in the response.
|
61 |
+
|
62 |
+
<response>response to previous question is provided here</response>
|
63 |
+
|
64 |
+
Some more text that should also be included in the response.
|
65 |
+
|
66 |
+
<clarification>
|
67 |
+
questions:
|
68 |
+
- text: What topic should the article cover?
|
69 |
+
options:
|
70 |
+
- Technology
|
71 |
+
- Health and Wellness
|
72 |
+
- Travel
|
73 |
+
- Other
|
74 |
+
- text: What is the target audience for the article?
|
75 |
+
options:
|
76 |
+
- General public
|
77 |
+
- Professionals in a specific field
|
78 |
+
- Students
|
79 |
+
- Other
|
80 |
+
</clarification>
|
81 |
+
|
82 |
+
Final notes that should be part of the response.
|
83 |
+
"""
|
84 |
+
|
85 |
+
parsed_data = parse_text(input_text)
|
86 |
+
print(f"Response: {parsed_data['response']}")
|
87 |
+
print("Clarifications:")
|
88 |
+
for item in parsed_data['clarifications']:
|
89 |
+
print(f" Question: {item['question']}")
|
90 |
+
print(" Options:", ", ".join(item['options']))
|