allenpark commited on
Commit
e9da7c7
·
verified ·
1 Parent(s): b6534ef

replace json string cleaning function

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -67,15 +67,33 @@ HEADER = """
67
  """
68
 
69
  def clean_json_string(json_str):
70
- # Replace single quotes with double quotes, but not apostrophes within words
71
- json_str = re.sub(r"(?<!\\)'([^']*)'", r'"\1"', json_str)
72
- # Add quotes around PASS or FAIL if they're not already quoted
73
- json_str = re.sub(r'"SCORE":\s*(PASS|FAIL)', r'"SCORE": "\1"', json_str)
74
 
75
- # Escape double quotes
76
- json_str = re.sub(r'(?<=: ")([^"]*)"([^"]*)"([^"]*)"', lambda m: m.group(1) + m.group(2).replace('"', '\\"') + m.group(3), json_str)
77
 
78
- return json_str
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  def model_call(question, document, answer):
81
  if question == "" or document == "" or answer == "":
 
67
  """
68
 
69
  def clean_json_string(json_str):
70
+ # # Replace single quotes with double quotes, but not apostrophes within words
71
+ # json_str = re.sub(r"(?<!\\)'([^']*)'", r'"\1"', json_str)
72
+ # # Add quotes around PASS or FAIL if they're not already quoted
73
+ # json_str = re.sub(r'"SCORE":\s*(PASS|FAIL)', r'"SCORE": "\1"', json_str)
74
 
75
+ # # Escape double quotes
76
+ # json_str = re.sub(r'(?<=: ")([^"]*)"([^"]*)"([^"]*)"', lambda m: m.group(1) + m.group(2).replace('"', '\\"') + m.group(3), json_str)
77
 
78
+ # return json_str
79
+
80
+ json_str = json_str.replace("'", '"')
81
+ json_str = re.sub(r'"SCORE":\s*(FAIL|PASS)', r'"SCORE": "\1"', json_str)
82
+
83
+ def escape_quotes(match):
84
+ return match.group(0).replace('"', '\\"')
85
+
86
+ pattern = r'"([^"\\]*(?:\\.[^"\\]*)*)"'
87
+ json_str = re.sub(pattern, lambda m: '"' + escape_quotes(m) + '"', json_str)
88
+
89
+ try:
90
+ parsed_json = json.loads(json_str)
91
+ return parsed_json
92
+ except json.JSONDecodeError as e:
93
+ print(f"Error parsing JSON: {e}")
94
+ print("Cleaned JSON string:")
95
+ print(json_str)
96
+ return None
97
 
98
  def model_call(question, document, answer):
99
  if question == "" or document == "" or answer == "":