|
|
@@ -86,7 +86,100 @@ def clean_markdown_for_pdf(text):
|
|
|
|
|
|
return text.strip()
|
|
|
|
|
|
+def clean_json_markers(text):
|
|
|
+ """
|
|
|
+ Eemaldab JSON ümber olevad ```json ja ``` markerid
|
|
|
+ """
|
|
|
+ # Eemalda algusest
|
|
|
+ text = re.sub(r'^```json\s*', '', text, flags=re.MULTILINE)
|
|
|
+ # Eemalda lõpust
|
|
|
+ text = re.sub(r'\s*```$', '', text, flags=re.MULTILINE)
|
|
|
+ # Eemalda kõikjalt kui on
|
|
|
+ text = re.sub(r'\s*```(json)?\s*', '', text)
|
|
|
+ return text.strip()
|
|
|
+
|
|
|
+def clean_json_string(text):
|
|
|
+ """Puhasta JSON string"""
|
|
|
+ # Eemalda reavahetused ja liigsed tühikud
|
|
|
+ text = text.replace('\n', '').replace('\r', '')
|
|
|
+ # Asenda \\" tavaliste jutumärkidega
|
|
|
+ text = text.replace('\\"', '"')
|
|
|
+ # Eemalda esimesed ja viimased jutumärgid kui vaja
|
|
|
+ if text.startswith('"') and text.endswith('"'):
|
|
|
+ text = text[1:-1]
|
|
|
+ return text
|
|
|
+
|
|
|
+def extract_and_format_json(data):
|
|
|
+ """Eralda ja vorminda JSON andmed"""
|
|
|
+ formatted_parts = []
|
|
|
+
|
|
|
+ # Võti-väärtus paaride kaart
|
|
|
+ key_map = {
|
|
|
+ "theoretical_contribution": "Theoretical contribution",
|
|
|
+ "practical_applicability": "Practical applicability",
|
|
|
+ "problem_solving": "Problem solving",
|
|
|
+ "limitations": "Limitations",
|
|
|
+ "future_research": "Future research",
|
|
|
+ "methodology": "Methodology"
|
|
|
+ }
|
|
|
+
|
|
|
+ for key, title in key_map.items():
|
|
|
+ if key in data and data[key]:
|
|
|
+ formatted_parts.append(f"{title}")
|
|
|
+ formatted_parts.append(str(data[key]))
|
|
|
+ formatted_parts.append("") # tühi rida
|
|
|
+
|
|
|
+ return "\n".join(formatted_parts)
|
|
|
+
|
|
|
+def process_json_text(input_text):
|
|
|
+ """Pööra JSON tekst loetavaks vorminguks"""
|
|
|
+ try:
|
|
|
+ # Parse esimene kiht
|
|
|
+ parsed = json.loads(input_text)
|
|
|
+
|
|
|
+ # Otsi analüüsi andmeid
|
|
|
+ analysis_data = None
|
|
|
+
|
|
|
+ # Variant 1: "analysis" väljal on JSON string
|
|
|
+ if "analysis" in parsed:
|
|
|
+ try:
|
|
|
+ # Puhasta ja parse sisemine JSON
|
|
|
+ clean_analysis = clean_json_string(str(parsed["analysis"]))
|
|
|
+ analysis_data = json.loads(clean_analysis)
|
|
|
+ except:
|
|
|
+ # Kui ei saa JSON-iks, kasuta otse
|
|
|
+ analysis_data = parsed.get("analysis", {})
|
|
|
+
|
|
|
+ # Variant 2: andmed otse pealkirjade all
|
|
|
+ elif any(key in parsed for key in ["theoretical_contribution", "practical_applicability"]):
|
|
|
+ analysis_data = parsed
|
|
|
+
|
|
|
+ # Variant 3: teised võimalused
|
|
|
+ else:
|
|
|
+ # Proovi leida JSON kuskil mujal
|
|
|
+ for key, value in parsed.items():
|
|
|
+ if isinstance(value, str) and any(x in value.lower() for x in ["theoretical", "practical", "contribution"]):
|
|
|
+ try:
|
|
|
+ clean_val = clean_json_string(value)
|
|
|
+ analysis_data = json.loads(clean_val)
|
|
|
+ break
|
|
|
+ except:
|
|
|
+ continue
|
|
|
+
|
|
|
+ if analysis_data:
|
|
|
+ return extract_and_format_json(analysis_data)
|
|
|
+ else:
|
|
|
+ return "No analysis data found in JSON"
|
|
|
+
|
|
|
+ except json.JSONDecodeError as e:
|
|
|
+ return f"JSON parsing error: {str(e)}"
|
|
|
+ except Exception as e:
|
|
|
+ return f"Error: {str(e)}"
|
|
|
+
|
|
|
def parse_transport_context(context_data):
|
|
|
+ context_data = clean_json_markers(context_data)
|
|
|
+ context_data = process_json_text(context_data)
|
|
|
+ print(context_data)
|
|
|
"""Parsi transpordikonteksti JSON-ist loetavaks"""
|
|
|
if isinstance(context_data, str):
|
|
|
# Proovi parsida string JSON-iks
|