143 lines
5.4 KiB
Python
Executable File
143 lines
5.4 KiB
Python
Executable File
import os
|
|
import re
|
|
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"`word`: `{item.get('word')}`,`description`: `{item.get('description')}` "
|
|
api_batch.append(
|
|
{
|
|
"id": str(item.get("id")),
|
|
"user_prompt": str(user_prompt),
|
|
"system_prompt": system_prompt,
|
|
"assistant_prompt": assistant_prompt,
|
|
}
|
|
)
|
|
return api_batch
|
|
|
|
SYS_PROMPT ='''You are an expert linguist and jurist specializing in Persian and Arabic legal terminology.
|
|
You have deep knowledge of Persian morphology, Arabic roots, and the usage of compound legal terms in Iranian, Islamic, and international law.
|
|
|
|
Your task: Given a legal Persian or Arabic-origin word or compound, identify its **legal base form (root)** and generate accurate **Persian legal synonyms** at two levels.
|
|
|
|
---
|
|
|
|
### 1️⃣ Surface-level synonyms
|
|
Words or short legal phrases that convey the same meaning in **formal legal Persian** without changing the grammatical form, tense, or plurality of the input.
|
|
|
|
### 2️⃣ Legal root-level analysis
|
|
Find the **legal compound root**, not the etymological or morphological root.
|
|
This “root” refers to the **core legal term or phrase** that appears as an independent legal concept in laws, contracts, or formal documents.
|
|
|
|
---
|
|
|
|
### Rules:
|
|
- `"root"` must represent the **legal concept root**, not linguistic or morphological roots.
|
|
✅ Example:
|
|
- `"word": "حقالزحمههایمان"` → `"root": "حقالزحمه"`
|
|
- `"word": "موافقتنامههایشان"` → `"root": "موافقتنامه"`
|
|
- `"word": "مشترکالمنافعی"` → `"root": "مشترکالمنافع"`
|
|
❌ Incorrect roots: `"حق"`, `"موافقت"`, `"مشترک"`
|
|
- Do NOT use Arabic triliteral roots like "و-ف-ق" or parentheses.
|
|
- Preserve **morphological integrity**: do not simplify or rewrite the input (e.g. `"استیضاحهایتان"` ≠ `"استیضاح شما"`).
|
|
- Use **only legal or semi-legal synonyms** (those found in legal texts, contracts, or jurisprudence).
|
|
- If no synonym exists, return empty lists.
|
|
- Output must always be a **valid JSON** object with the following keys and structure:
|
|
|
|
---
|
|
|
|
### Output Format:
|
|
```json
|
|
{
|
|
"word": "<original word>",
|
|
"root": "<legal compound root or base term>",
|
|
"root_meaning": "<brief explanation of the legal meaning of the root>",
|
|
"surface_synonyms": ["...", "..."],
|
|
"root_synonyms": ["...", "..."]
|
|
}
|
|
|
|
'''
|
|
|
|
ASSIST_PROMPT = '''=== Example 1 ===
|
|
input:
|
|
{
|
|
"word": "حقالحساب",
|
|
"description": "واژهای مرکب از حق (به معنی عدالت یا استحقاق) و حساب (به معنی محاسبه یا تسویه). در متون حقوقی به پاداش یا دستمزد قانونی اشاره دارد."
|
|
}
|
|
output:
|
|
{
|
|
"word": "حقالحساب",
|
|
"root": "حق",
|
|
"root_meaning": "درستی، استحقاق، چیزی که مطابق قانون باشد",
|
|
"surface_synonyms": ["حقالزحمه", "طلب قانونی"],
|
|
"root_synonyms": ["عدالت", "استحقاق", "مطالبه مشروع"]
|
|
}
|
|
=== Example 2 ===
|
|
input:
|
|
{
|
|
"word": "تفسخ",
|
|
"description": "اصطلاح عربی در حقوق مدنی، به معنای برهم زدن قرارداد با رضایت دو طرف یا به حکم قانون."
|
|
}
|
|
output:
|
|
{
|
|
"word": "تفسخ",
|
|
"root": "فسخ",
|
|
"root_meaning": "از بین بردن یا بیاثر کردن عقد یا قرارداد",
|
|
"surface_synonyms": ["انحلال قرارداد", "ابطال معامله"],
|
|
"root_synonyms": ["فسخ", "ابطال", "لغو"]
|
|
}
|
|
'''
|
|
|
|
# ---------- مثال استفاده ----------
|
|
if __name__ == "__main__":
|
|
# data = load_orjson("/home1/ava3/project/aidataparser_test/motaradef_dataset/motaradef.json")
|
|
# finall_data = [{'id':i['id'], 'word':i['word'], "description":i['ai_code']['ai_code']['ner_description']}
|
|
# for i in data]
|
|
# procces_list = make_data(
|
|
# all_data=finall_data, assistant_prompt=ASSIST_PROMPT, system_prompt=SYS_PROMPT
|
|
# )
|
|
# # print(
|
|
# # f'updated_data {updated_data}'
|
|
# # )
|
|
# # print(type(updated_data))
|
|
# save_orjson(
|
|
# data=procces_list,
|
|
# path="/home1/ava3/project/aidataparser_test/motaradef_dataset/prompt_data.json")
|
|
# ------------------------------------- normalize json data format
|
|
data = load_orjson("/home1/ava3/project/aidataparser_test/motaradef_dataset/output/final_data_motaradef_legal.json")
|
|
finall_list = []
|
|
for item in data:
|
|
form_ = {}
|
|
form_['word'] = item['word']
|
|
form_['synonyms'] = item['surface_synonyms']
|
|
form_['score'] = 1.0
|
|
finall_list.append(form_)
|
|
form_ = {}
|
|
form_['word'] = item['root']
|
|
form_['synonyms'] = item['surface_synonyms']
|
|
form_['score'] = 1.0
|
|
finall_list.append(form_)
|
|
|
|
save_orjson(
|
|
data=finall_list,
|
|
path='./motaradef_finall_arch.json'
|
|
)
|