|
@@ -7,6 +7,7 @@ import sys
|
|
|
import os
|
|
import os
|
|
|
import json
|
|
import json
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
|
+import re
|
|
|
|
|
|
|
|
# Lisa src kaust Pythoni teele
|
|
# Lisa src kaust Pythoni teele
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
|
|
@@ -158,6 +159,8 @@ def batch_mode(queries_file: str):
|
|
|
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
|
|
|
|
|
|
print(f" ✓ Salvestatud {filename}")
|
|
print(f" ✓ Salvestatud {filename}")
|
|
|
|
|
+ # Konverteerime json'i markdown failiks
|
|
|
|
|
+ convert_json_file_to_markdown(filename)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
print(f" ✗ Viga: {str(e)}")
|
|
print(f" ✗ Viga: {str(e)}")
|
|
@@ -175,6 +178,8 @@ def batch_mode(queries_file: str):
|
|
|
json.dump(results, f, ensure_ascii=False, indent=2)
|
|
json.dump(results, f, ensure_ascii=False, indent=2)
|
|
|
|
|
|
|
|
print(f"\n✅ Valmis! Tulemused salvestatud faili: {summary_file}")
|
|
print(f"\n✅ Valmis! Tulemused salvestatud faili: {summary_file}")
|
|
|
|
|
+ # Konverteerime json'i markdown failiks
|
|
|
|
|
+ convert_json_file_to_markdown(summary_file)
|
|
|
|
|
|
|
|
def single_query(query: str, search_type: str = "hybrid", context_type: str = "detailed"):
|
|
def single_query(query: str, search_type: str = "hybrid", context_type: str = "detailed"):
|
|
|
"""Üksik päring"""
|
|
"""Üksik päring"""
|
|
@@ -194,8 +199,91 @@ def single_query(query: str, search_type: str = "hybrid", context_type: str = "d
|
|
|
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
|
|
|
|
|
|
print(f"📄 Tulemus salvestatud faili: {filename}")
|
|
print(f"📄 Tulemus salvestatud faili: {filename}")
|
|
|
|
|
+ # Konverteerime json'i markdown failiks
|
|
|
|
|
+ convert_json_file_to_markdown(filename)
|
|
|
|
|
+
|
|
|
|
|
+def json_to_markdown(json_data):
|
|
|
|
|
+ """
|
|
|
|
|
+ Konverteerib transpordiartiklite analüüsi JSON-vormingu Markdown-vormingusse
|
|
|
|
|
+ """
|
|
|
|
|
+ markdown_content = "# Transpordiartiklite analüüsi kokkuvõte\n\n"
|
|
|
|
|
+
|
|
|
|
|
+ for i, query_result in enumerate(json_data):
|
|
|
|
|
+ # Pealkiri
|
|
|
|
|
+ query = query_result.get("query", f"Päring {i+1}")
|
|
|
|
|
+ markdown_content += f"## {i+1}. {query}\n\n"
|
|
|
|
|
+
|
|
|
|
|
+ # Metaandmed
|
|
|
|
|
+ timestamp = query_result.get("timestamp", "")
|
|
|
|
|
+ articles_found = query_result.get("articles_found", 0)
|
|
|
|
|
+ confidence = query_result.get("confidence", 0)
|
|
|
|
|
+
|
|
|
|
|
+ markdown_content += f"**Aeg:** {timestamp}\n"
|
|
|
|
|
+ markdown_content += f"**Leitud artikleid:** {articles_found}\n"
|
|
|
|
|
+ markdown_content += f"**Usaldusväärsus:** {confidence:.2f}\n\n"
|
|
|
|
|
+
|
|
|
|
|
+ # Põhivastus (answer)
|
|
|
|
|
+ answer = query_result.get("answer", "")
|
|
|
|
|
+ if answer:
|
|
|
|
|
+ # Eemaldame liigsed reavahetused ja vormindame
|
|
|
|
|
+ answer = re.sub(r'\n{3,}', '\n\n', answer.strip())
|
|
|
|
|
+ markdown_content += "### Analüüs\n\n"
|
|
|
|
|
+ markdown_content += f"{answer}\n\n"
|
|
|
|
|
+
|
|
|
|
|
+ # Allikad
|
|
|
|
|
+ sources = query_result.get("sources", [])
|
|
|
|
|
+ if sources:
|
|
|
|
|
+ markdown_content += "### Allikad\n\n"
|
|
|
|
|
+ for j, source in enumerate(sources):
|
|
|
|
|
+ title = source.get("title", "Pealkiri puudub")
|
|
|
|
|
+ authors = ", ".join(source.get("authors", ["Autor puudub"]))
|
|
|
|
|
+ year = source.get("year", "Aasta puudub")
|
|
|
|
|
+ summary = source.get("summary", source.get("full_summary", "Kokkuvõte puudub"))
|
|
|
|
|
+
|
|
|
|
|
+ # Võtame kokkuvõttest esimesed 2-3 lauset
|
|
|
|
|
+ summary_preview = " ".join(summary.split()[:50])
|
|
|
|
|
+ if len(summary.split()) > 50:
|
|
|
|
|
+ summary_preview += "..."
|
|
|
|
|
+
|
|
|
|
|
+ markdown_content += f"#### {j+1}. {title}\n"
|
|
|
|
|
+ markdown_content += f"**Autor(id):** {authors}\n"
|
|
|
|
|
+ markdown_content += f"**Aasta:** {year}\n"
|
|
|
|
|
+ markdown_content += f"**Kokkuvõte:** {summary_preview}\n\n"
|
|
|
|
|
+
|
|
|
|
|
+ markdown_content += "---\n\n"
|
|
|
|
|
+
|
|
|
|
|
+ return markdown_content
|
|
|
|
|
+
|
|
|
|
|
+def convert_json_file_to_markdown(json_filename):
|
|
|
|
|
+ # Loeme JSON-faili sisu (antud juhul on see juba muutujas)
|
|
|
|
|
+ # Tegelikus rakenduses loeksime failist:
|
|
|
|
|
+ with open(json_filename, 'r', encoding='utf-8') as f:
|
|
|
|
|
+ json_data = json.load(f)
|
|
|
|
|
+
|
|
|
|
|
+ # Teisenda Markdowniks
|
|
|
|
|
+ # Kui on dict ja sisaldab päringu välju, pane listi
|
|
|
|
|
+ if isinstance(json_data, dict):
|
|
|
|
|
+ markdown_content = json_to_markdown([json_data])
|
|
|
|
|
+ # Kui on list, kasuta otse
|
|
|
|
|
+ elif isinstance(json_data, list):
|
|
|
|
|
+ markdown_content = json_to_markdown(json_data)
|
|
|
|
|
+ # Muudel juhtudel proovi listina
|
|
|
|
|
+ else:
|
|
|
|
|
+ markdown_content = "# Viga\n\nAndmed pole sobivas vormingus.\n"
|
|
|
|
|
+
|
|
|
|
|
+ # Loo Markdown failinimi
|
|
|
|
|
+ base_name = os.path.splitext(json_filename)[0]
|
|
|
|
|
+ md_filename = f"{base_name}.md"
|
|
|
|
|
+
|
|
|
|
|
+ # Salvesta Markdown fail
|
|
|
|
|
+ with open(md_filename, 'w', encoding='utf-8') as f:
|
|
|
|
|
+ f.write(markdown_content)
|
|
|
|
|
+
|
|
|
|
|
+ print(f"✅ Markdown fail loodud: {md_filename}")
|
|
|
|
|
+ return md_filename
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
|
|
|
+ #convert_json_file_to_markdown('tmp/query_summary_20260111_215300.json')
|
|
|
import argparse
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Transpordiartiklite päringumootor")
|
|
parser = argparse.ArgumentParser(description="Transpordiartiklite päringumootor")
|