112 lines
4.7 KiB
Python
Executable File
112 lines
4.7 KiB
Python
Executable File
import os
|
|
import json
|
|
import orjson
|
|
from pathlib import Path
|
|
import numpy as np
|
|
|
|
|
|
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)
|
|
)
|
|
|
|
|
|
def make_data(all_data, system_prompt, assistant_prompt=None):
|
|
api_batch = []
|
|
for item in all_data:
|
|
|
|
# user_prompt = f"Input: `{item.get('sanad')}`"
|
|
api_batch.append(
|
|
{
|
|
"id": str(item.get("id")),
|
|
"user_prompt": str(item.get("user_prompt")),
|
|
"type":item["type"],
|
|
"system_prompt": system_prompt,
|
|
"assistant_prompt": assistant_prompt,
|
|
}
|
|
)
|
|
return api_batch
|
|
|
|
|
|
SYSTEM_PROMPT = """Act like a professional legal translator and expert in jurisprudence and formal logic. You specialize in translating complex English legal and logical texts into precise, formal Persian (Farsi), using terminology standard in اصول فقه، حقوق، و منطق.
|
|
|
|
Your goal is to produce an accurate, terminology-consistent, and context-aware translation from English to Persian within the domain of legal theory and logical propositions.
|
|
|
|
Task: Translate the provided English text into formal Persian suitable for academic legal writing and اصول فقه discourse.
|
|
|
|
Requirements:
|
|
1) Use strictly legal and logical terminology. Avoid literary, colloquial, journalistic, or general-language equivalents.
|
|
2) Preserve conceptual distinctions central to legal theory and اصول فقه.
|
|
3) Ensure terminological consistency throughout the entire translation (no variation in key terms).
|
|
4) When a technical term appears, prefer its established jurisprudential equivalent over any common-language meaning.
|
|
5) Maintain the logical structure of arguments, conditions, exceptions, and implications exactly as in the source.
|
|
6) If a term could have multiple meanings, select only the meaning appropriate to legal-logical analysis.
|
|
|
|
Mandatory Terminology Examples (apply exactly as follows when they appear in the text):
|
|
- "Doctrinal_conflict" = تعارض
|
|
- "Practical_conflict" = تزاحم
|
|
- "Absolute prior, qualified subsequent" = مطلق مقدم، مقید موخر
|
|
- "Qualified prior, absolute subsequent" = مقید مقدم، مطلق موخر
|
|
- "Repetition of ruling" = تکرار حکم
|
|
- "Established conflict" = تعارض مستقر
|
|
- "Without conflict" = بدون تعارض
|
|
|
|
Context:
|
|
///
|
|
The text belongs to the field of legal theory, jurisprudence (especially اصول فقه), and formal logical propositions. The translation must remain within this conceptual framework and must not adopt meanings from non-legal domains.
|
|
///
|
|
|
|
Constraints:
|
|
- Format: Provide only the final Persian translation.
|
|
- Style: Formal, academic, and precise.
|
|
- Scope: Do not add explanations, commentary, or footnotes.
|
|
- Reasoning: Carefully preserve logical relations (عموم و خصوص، اطلاق و تقیید، تقدم و تأخر، تعارض و تزاحم).
|
|
- Self-check: Before finalizing, verify consistency of terminology and alignment with legal-logical usage.
|
|
|
|
Take a deep breath and work on this problem step-by-step.
|
|
"""
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
############################################## Make Data #####################################################
|
|
in_data = load_orjson(
|
|
"/home1/ava3/init_mahdi/project/rule_extractor/im_based/data/sequential/final_validity/output_6152.json"
|
|
)
|
|
|
|
|
|
f_data = make_data(
|
|
system_prompt=SYSTEM_PROMPT,
|
|
all_data = [
|
|
{
|
|
"id": item["id"]+'--'+str(i),
|
|
"user_prompt": item["conflict_detection"]["explanation_of_conflict"],
|
|
"type": "conflict_detection",
|
|
} for i, item in enumerate(in_data)
|
|
]
|
|
)
|
|
print(f'f_data {len(f_data)}')
|
|
f_data += make_data( system_prompt=SYSTEM_PROMPT,
|
|
all_data = [
|
|
{
|
|
"id": item["id"]+'-'+str(i),
|
|
"user_prompt": item["conflict_type_detection"]["explanation_of_type"],
|
|
"type": "conflict_type_detection",
|
|
} for i, item in enumerate(in_data)
|
|
]
|
|
)
|
|
print(f'f_data {len(f_data)}')
|
|
|
|
save_orjson(data=f_data, path="/home1/ava3/init_mahdi/project/aiDataParser/task/translate_en_per/input_data_6152.json")
|
|
except:
|
|
import traceback
|
|
traceback.print_exc()
|
|
# python3 -m aiDataParser.task.translate_en_per.create_data
|