translate-conflict
This commit is contained in:
@@ -0,0 +1,284 @@
|
||||
from dotenv import load_dotenv
|
||||
import os, sys
|
||||
from app.common.elastic_helper import ElasticHelper
|
||||
from app.common.data_helper import DataHelper
|
||||
from pydantic import BaseModel
|
||||
from typing import Literal, List, Dict, Any, Optional
|
||||
|
||||
|
||||
class SU(BaseModel):
|
||||
has_subject_unity: str
|
||||
required_assumptions: Optional[str] = None
|
||||
matching_rules: List[List[str]]
|
||||
reasoning: str
|
||||
|
||||
|
||||
class CD(BaseModel):
|
||||
has_confict: bool
|
||||
explanation_of_conflict: str
|
||||
|
||||
|
||||
class CDT(BaseModel):
|
||||
conflict_type: Literal["Doctrinal_conflict", "Practical_conflict"]
|
||||
explanation_of_type: str
|
||||
|
||||
|
||||
class E(BaseModel):
|
||||
is_subject_unity_assessment_correct: bool
|
||||
is_conflict_detection_correct: bool
|
||||
is_conflict_type_detection_correct: bool
|
||||
is_relation_type_detection_correct: bool
|
||||
valid_relation_type: Literal[
|
||||
"مطلق مقدم، مقید موخر",
|
||||
"مقید مقدم، مطلق موخر",
|
||||
"تکرار حکم",
|
||||
"تعارض مستقر",
|
||||
"بدون تعارض",
|
||||
] = None
|
||||
comments: Optional[str] = None
|
||||
|
||||
|
||||
class RI(BaseModel):
|
||||
# only filled if SubjectRelation is yes or yes_under_assumptions
|
||||
reasoning: str = None
|
||||
relation_type: Literal[
|
||||
"مطلق مقدم، مقید موخر",
|
||||
"مقید مقدم، مطلق موخر",
|
||||
"تکرار حکم",
|
||||
"تعارض مستقر",
|
||||
"بدون تعارض",
|
||||
]
|
||||
|
||||
|
||||
class FV(BaseModel):
|
||||
section1_validity: Optional[Literal["valid", "invalid", "undetermined"]] = None
|
||||
section2_validity: Optional[Literal["valid", "invalid", "undetermined"]] = None
|
||||
evaluator_comments: Optional[str] = None
|
||||
|
||||
|
||||
class InputDataFormat(BaseModel):
|
||||
id: str # context_id
|
||||
context_id: str
|
||||
subject_unity: SU
|
||||
conflict_detection: CD
|
||||
conflict_type_detection: CDT
|
||||
relation_identification: RI
|
||||
evaluation: E
|
||||
final_validity: FV
|
||||
|
||||
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
load_dotenv(dotenv_path=r"D:\init_mahdi\dataParsers\config.env")
|
||||
|
||||
host = os.getenv("HOST_160")
|
||||
password = os.getenv("HOST_160_PASS")
|
||||
path_schema = r"D:\init_mahdi\dataParsers\schema"
|
||||
elastic = ElasticHelper(
|
||||
es_url=host, es_pass=password, es_user="elastic", path_mappings=path_schema
|
||||
)
|
||||
|
||||
|
||||
def update_mj_qa_rules_with_rule_relations():
|
||||
update_filed = {
|
||||
"rule_relations": {
|
||||
"properties": {
|
||||
"rule_id": {"type": "keyword"},
|
||||
"section_mom_id": {"type": "keyword"},
|
||||
"score": {"type": "float"},
|
||||
"main_type": {"type": "keyword"},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def update_mj_qa_rules_with_4plan_data(
|
||||
index_schema_path=r"D:\init_mahdi\dataParsers\schema\mj_qa_rule.json",
|
||||
version_key="pair_gvc_e1_jv3_all",
|
||||
_source={
|
||||
"context_id", # context_id
|
||||
"group_section_ids",
|
||||
"qanon_id",
|
||||
"qanon_title",
|
||||
"qanon_etebar",
|
||||
"state_etebar",
|
||||
"ts_date",
|
||||
"ts_ref",
|
||||
"tcode_mains",
|
||||
"tcode_subs",
|
||||
},
|
||||
get_index_name="mj_qa_rule",
|
||||
out_put_index_name="mj_qa_conflict",
|
||||
):
|
||||
def get_evaluation_data(data: E):
|
||||
result = []
|
||||
if data.is_subject_unity_assessment_correct:
|
||||
result.append("تایید وجود وحدت موضوعی")
|
||||
else:
|
||||
result.append("رد وجود وحدت موضوعی")
|
||||
if data.is_conflict_detection_correct:
|
||||
result.append("تایید تشخیص مخالفت")
|
||||
else:
|
||||
result.append("رد تشخیص مخالفت")
|
||||
|
||||
if data.is_conflict_type_detection_correct:
|
||||
result.append("تایید تشخیص نوع مخالفت")
|
||||
else:
|
||||
result.append("رد تشخیص نوع مخالفت")
|
||||
|
||||
if data.is_relation_type_detection_correct:
|
||||
result.append("تایید تشخیص نوع تعارض")
|
||||
else:
|
||||
result.append("رد تشخیص نوع تعارض")
|
||||
return result
|
||||
|
||||
def get_vlide_text(text):
|
||||
if text == "valid":
|
||||
return "معتبر"
|
||||
elif text == "invalid":
|
||||
return "نامعتبر"
|
||||
elif text == "undetermined":
|
||||
return "نامشخص"
|
||||
map_sec_id_full_path = {}
|
||||
for i in DataHelper.load_orjson(r"D:\init_mahdi\dataParsers\mj_qa_section.json"):
|
||||
try:
|
||||
f_path = i["_source"]["other_info"]["full_path"]
|
||||
map_sec_id_full_path[i["_id"]]= f_path
|
||||
except:
|
||||
print(f'full_path {i["_id"]}')
|
||||
continue
|
||||
# rerank_data = DataHelper.load_orjson(r"D:\init_mahdi\dataParsers\rerank_data.json")
|
||||
# Dict[rule_id] : List[Dict["id":rule_id,"score":float[score]], ...]
|
||||
|
||||
p_1 = DataHelper.load_orjson(r"D:\init_mahdi\dataParsers\merged_all_pair.json")
|
||||
map_pair_id = {i["id"]: i for i in p_1}
|
||||
|
||||
finall_4plan_data = [
|
||||
InputDataFormat.model_validate(i)
|
||||
for i in DataHelper.load_orjson(
|
||||
r"D:\init_mahdi\dataParsers\4plan_finall__data.json"
|
||||
)
|
||||
]
|
||||
|
||||
bulk_data = []
|
||||
failed_data = []
|
||||
total_count = 0
|
||||
for item in finall_4plan_data:
|
||||
_pair_data = map_pair_id[item.context_id]
|
||||
_id = _pair_data["id"]
|
||||
previous_context_id = _pair_data["previous_context_id"]
|
||||
next_context_id = _pair_data["next_context_id"]
|
||||
_previous_info = elastic.search(
|
||||
index=get_index_name,
|
||||
# source=_source,
|
||||
query={"term": {"context_id": previous_context_id}},
|
||||
)
|
||||
_previous_info = _previous_info["hits"]["hits"]
|
||||
if len(_previous_info) > 0:
|
||||
previous_info = _previous_info[0]["_source"]
|
||||
__id = _previous_info[0]["_id"]
|
||||
previous_info = {
|
||||
k: previous_info[k] for k in _source if k in previous_info
|
||||
}
|
||||
context_id = previous_info['context_id']
|
||||
previous_info['section_mom_id'] = context_id
|
||||
try:
|
||||
previous_info["full_path"] = map_sec_id_full_path[
|
||||
context_id
|
||||
]
|
||||
except:
|
||||
context_id = __id.split('-')[0]
|
||||
previous_info["full_path"] = map_sec_id_full_path[
|
||||
context_id
|
||||
]
|
||||
print(f'mom_id {__id}')
|
||||
previous_info["rule_ids"] = _pair_data["previous_rules_ids"]
|
||||
else:
|
||||
failed_data.append(previous_context_id)
|
||||
continue
|
||||
|
||||
_next_info = elastic.search(
|
||||
index=get_index_name,
|
||||
query={"term": {"context_id": next_context_id}},
|
||||
)
|
||||
_next_info = _next_info["hits"]["hits"]
|
||||
if len(_next_info) > 0:
|
||||
__id = _next_info[0]["_id"]
|
||||
next_info = _next_info[0]["_source"]
|
||||
next_info = {k: next_info[k] for k in _source if k in next_info}
|
||||
|
||||
context_id = next_info['context_id']
|
||||
next_info['section_mom_id'] = context_id
|
||||
try:
|
||||
next_info["full_path"] = map_sec_id_full_path[
|
||||
context_id
|
||||
]
|
||||
except:
|
||||
context_id = __id.split('-')[0]
|
||||
next_info["full_path"] = map_sec_id_full_path[
|
||||
context_id
|
||||
]
|
||||
print(f'mom_id {__id}')
|
||||
|
||||
next_info["rule_ids"] = _pair_data["next_rules_ids"]
|
||||
else:
|
||||
failed_data.append(previous_context_id)
|
||||
continue
|
||||
|
||||
es_data = {
|
||||
"id": _id,
|
||||
"version_key": version_key,
|
||||
"previous_info": previous_info,
|
||||
"next_info": next_info,
|
||||
"subject_unity": {
|
||||
"main_type": item.subject_unity.has_subject_unity,
|
||||
"description": item.subject_unity.required_assumptions,
|
||||
"reason": item.subject_unity.reasoning,
|
||||
},
|
||||
"conflict_detection": {
|
||||
"has_conflict": item.conflict_detection.has_confict,
|
||||
"description": item.conflict_detection.explanation_of_conflict,
|
||||
},
|
||||
"conflict_type_detection": {
|
||||
"main_type": item.conflict_type_detection.conflict_type,
|
||||
"description": item.conflict_type_detection.explanation_of_type,
|
||||
},
|
||||
"conflict_relation_identification": {
|
||||
"main_type": item.relation_identification.relation_type,
|
||||
"description": item.relation_identification.reasoning,
|
||||
},
|
||||
"conflict_evaluation": {
|
||||
"validates": get_evaluation_data(item.evaluation),
|
||||
"main_type": item.evaluation.valid_relation_type,
|
||||
"description": item.evaluation.comments,
|
||||
},
|
||||
"conflict_final": {
|
||||
"valid_state1": get_vlide_text(item.final_validity.section1_validity),
|
||||
"valid_state2": get_vlide_text(item.final_validity.section2_validity),
|
||||
"description": item.final_validity.evaluator_comments,
|
||||
},
|
||||
}
|
||||
|
||||
bulk_data.append({"_id": _id, "_source": es_data})
|
||||
if len(bulk_data) > 200:
|
||||
total_count += len(bulk_data)
|
||||
print("=" * 10, "to_elastic , total=" + str(total_count))
|
||||
elastic.importBulkList(bulk_data, out_put_index_name)
|
||||
bulk_data = []
|
||||
# break
|
||||
|
||||
if len(bulk_data) > 0:
|
||||
total_count += len(bulk_data)
|
||||
print("=" * 10, "to_elastic , total=" + str(total_count))
|
||||
elastic.importBulkList(bulk_data, out_put_index_name)
|
||||
bulk_data = []
|
||||
print(f"failed_data {len(failed_data)}")
|
||||
DataHelper.save_orjson(
|
||||
data=failed_data,
|
||||
path=r"D:\init_mahdi\dataParsers\failed_data_mj_qa_conflict.json",
|
||||
)
|
||||
|
||||
|
||||
update_mj_qa_rules_with_4plan_data()
|
||||
|
||||
# python -m dataParsers.app.update_mj_qa_rules
|
||||
Reference in New Issue
Block a user