Files
AiDataParser/task/mn_sanad/summerize_make_data.py
T
2026-11-28 16:36:19 +00:00

123 lines
4.4 KiB
Python
Executable File

import os
import json
from collections import defaultdict
import orjson
from pathlib import Path
def load_orjson(path: str | Path):
path = Path(path)
with path.open("rb") as f: # باید باینری باز بشه برای orjson
return orjson.loads(f.read())
def save_orjson(path, data):
with open(path, "wb") as f:
f.write(
orjson.dumps(data, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS)
)
S_PROPMT = """شما یک متخصص ویرایش و خلاصه‌سازی متن فارسی هستید. وظیفه شما ایجاد یک خلاصه‌ی دقیق، روان و مختصر است به گونه‌ای که **لحن و سبک نویسنده اصلی حفظ شود**. دستورالعمل‌ها:
1. متن را با دقت بخوانید و تمام نکات کلیدی، مثال‌ها، استدلال‌ها و جزئیات مهم را شناسایی کنید.
2. خلاصه باید تمام اطلاعات ضروری را منتقل کند بدون اینکه نکات مهم حذف شوند.
3. جملات را ساده کنید، اما **سبک و صدا و لحن نویسنده اصلی را حفظ کنید**.
4. از نظر لحن، به سبک متن وفادار باشید؛ ممکن است غیررسمی، شاعرانه یا تحلیلی باشد، بسته به متن.
5. بررسی نهایی: مطمئن شوید همه بخش‌های مهم متن در خلاصه هستند و متن روان و قابل فهم است.
فرمت خروجی: متن ساده و روان با حفظ لحن نویسنده."""
old1 = """You are an expert Persian language editor and summarization specialist. Your task is to create a concise, accurate, and coherent summary of the Persian text provided. Follow these instructions carefully:
Step 1: Read and Understand
Carefully read the entire text.
Identify all key points, arguments, examples, and facts.
Note any technical terms, nuanced ideas, or critical details.
Step 2: Structure the Summary
Maintain the original meaning; simplify sentences without altering facts.
Step 3: Condense the Text
Retain all essential information; do not omit critical points.
Step 4: Style and Tone
Use a clear, professional, and neutral tone.
Avoid personal opinions, interpretations, or additional commentary.
Ensure terminology and details from the original text are preserved.
Step 5: Final Check
Verify that all sections of the original text are represented.
Ensure the summary is coherent, concise, and readable.
Output Format: plain fluent text."""
def make_data3(all_data, system_prompt):
api_batch = []
for i, (k, v) in enumerate(all_data.items()):
# i_token = count_tokens(system_prompt=system_prompt, user_prompt=item["content"])
api_batch.append(
{
"id": k,
"system_prompt": system_prompt,
"user_prompt": f"Text to Summarize: {v}",
}
)
# print(f"i {i}")
# if i == 3:
# break
print(f"len {len(api_batch)}")
return api_batch
#####################################################################################
input_data = load_orjson(
"/home2/semanticsearch_rules_planparser/data/ai_sections_mn_sanad.json"
)
print(f"input_data {len(input_data)}")
groups = defaultdict(list)
for item in input_data:
# base_id = item.get("base_id")
# if not base_id:
# if "_" in item["id"]:
_id = item["id"]
base_id = _id.split("_")[0]
child_order = _id.split("_")[-1]
groups[base_id].append(
{
"content": item["content"],
"child_order": child_order,
# "start_index": item["start_index"],
}
)
print(f"groups {len(groups)}")
f_data = {}
for k, v in groups.items():
# مرتب‌سازی بر اساس child_order
sorted_items = sorted(v, key=lambda x: int(x["child_order"]))
# گرفتن فقط content ها
contents = [item["content"] for item in sorted_items]
f_data[k] = "\n\n".join(contents)
print(f"input_data {len(f_data)}")
o_data = make_data3(f_data, system_prompt=S_PROPMT)
print(f"o_data {len(o_data)}")
save_orjson(
data=o_data,
path="/home1/ava3/project/aiDataParser/task/mn_sanad/summerize/input/mn_sanad.json",
)
#####################################################################################
# python3 -m aiDataParser.task.mn_sanad.summerize_make_data