Merge branch 'master' of https://git2.tavasi.ir/develop/dataParsers
This commit is contained in:
+12
@@ -39,3 +39,15 @@ app/monir/.env
|
|||||||
041106_لیست تمام موضوعات.json
|
041106_لیست تمام موضوعات.json
|
||||||
041106_لیست تمام موضوعات.xlsx
|
041106_لیست تمام موضوعات.xlsx
|
||||||
merge_labels.json
|
merge_labels.json
|
||||||
|
4plan_finall__data.json
|
||||||
|
.gitignore
|
||||||
|
failed_data_mj_qa_conflict.json
|
||||||
|
finall_data_translate.json
|
||||||
|
finall_data_translate111.json
|
||||||
|
merged_all_pair.json
|
||||||
|
mj_qa_context.json
|
||||||
|
mj_qa_rule.json
|
||||||
|
mj_qa_section.json
|
||||||
|
rerank_data.json
|
||||||
|
test1.json
|
||||||
|
schema/mj_qa_conflict.json
|
||||||
|
|||||||
@@ -83,45 +83,46 @@ def extract_json_context_id_rules_labels(
|
|||||||
index_name="mj_qa_rule",
|
index_name="mj_qa_rule",
|
||||||
):
|
):
|
||||||
input_data = DataHelper.load_orjson(json_path)
|
input_data = DataHelper.load_orjson(json_path)
|
||||||
print(
|
print(f"input_data {len(input_data)}")
|
||||||
f'input_data {len(input_data)}'
|
|
||||||
)
|
|
||||||
bulk_data = []
|
bulk_data = []
|
||||||
total_count = 0
|
total_count = 0
|
||||||
failed = 0
|
failed = 0
|
||||||
|
pass_data = 0
|
||||||
|
|
||||||
for _id1, (context_id, rule_label_data) in enumerate(input_data.items(), start=1):
|
for _id1, (context_id, rule_label_data) in enumerate(input_data.items(), start=1):
|
||||||
print(f"context_id {context_id}")
|
# print(f"context_id {context_id}")
|
||||||
for _id2, item in enumerate(rule_label_data, start=1):
|
for _id2, item in enumerate(rule_label_data, start=1):
|
||||||
rule_id = context_id + "-" + str(_id2)
|
rule_id = context_id + "-" + str(_id2)
|
||||||
try:
|
try:
|
||||||
old_data = elastic.get_document(index_name, rule_id)
|
old_data = elastic.get_document(index_name, rule_id)
|
||||||
old_data = old_data["_source"]
|
old_data = old_data["_source"]
|
||||||
item_obj = OutputLabels.model_validate(item)
|
_old_detail = old_data.get("label_object", None)
|
||||||
new_item = convert_OutputLabels_to_es_schema(item_obj)
|
if not _old_detail:
|
||||||
old_data["label_object"] = new_item
|
item_obj = OutputLabels.model_validate(item)
|
||||||
|
new_item = convert_OutputLabels_to_es_schema(item_obj)
|
||||||
|
old_data["label_object"] = new_item
|
||||||
|
else:
|
||||||
|
pass_data += 1
|
||||||
|
if pass_data >= 1000:
|
||||||
|
print(f"pass_data {pass_data}")
|
||||||
|
pass_data = 0
|
||||||
|
continue
|
||||||
except:
|
except:
|
||||||
failed += 1
|
failed += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
bulk_data.append(
|
bulk_data.append({"_id": old_data["id"], "_source": old_data})
|
||||||
{"_id": old_data["id"], "_source": old_data}
|
# qs648706-1
|
||||||
)
|
|
||||||
# qs648706-1
|
|
||||||
|
|
||||||
if len(bulk_data) >= 100:
|
if len(bulk_data) >= 100:
|
||||||
total_count += len(bulk_data)
|
total_count += len(bulk_data)
|
||||||
print(
|
print("=" * 10, "to_elastic , total=" + str(total_count))
|
||||||
"=" * 10, "to_elastic , total=" + str(total_count)
|
|
||||||
)
|
|
||||||
elastic.updateBulkList(bulk_data, index_name)
|
elastic.updateBulkList(bulk_data, index_name)
|
||||||
bulk_data = []
|
bulk_data = []
|
||||||
|
|
||||||
if len(bulk_data) >= 0:
|
if len(bulk_data) >= 0:
|
||||||
total_count += len(bulk_data)
|
total_count += len(bulk_data)
|
||||||
print(
|
print("=" * 10, "to_elastic , total=" + str(total_count))
|
||||||
"=" * 10, "to_elastic , total=" + str(total_count)
|
|
||||||
)
|
|
||||||
elastic.updateBulkList(bulk_data, index_name)
|
elastic.updateBulkList(bulk_data, index_name)
|
||||||
bulk_data = []
|
bulk_data = []
|
||||||
# qs648706-1
|
# qs648706-1
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# import mariadb
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from app.common.elastic_helper import ElasticHelper
|
||||||
|
|
||||||
|
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")
|
||||||
|
elastic = ElasticHelper(es_url=host, es_pass=password, es_user="elastic")
|
||||||
|
|
||||||
|
|
||||||
|
elastic.exportToJsonForAI(
|
||||||
|
path_back=r"D:\init_mahdi\dataParsers",
|
||||||
|
index_name="mj_qa_section", out_name="mj_qa_section.json"
|
||||||
|
)
|
||||||
|
|
||||||
|
# python -m app.save_qa_rules_from160
|
||||||
@@ -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
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
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
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
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_translate(
|
||||||
|
out_put_index_name="mj_qa_conflict",
|
||||||
|
):
|
||||||
|
in_data = DataHelper.load_orjson(
|
||||||
|
r"D:\init_mahdi\dataParsers\finall_data_translate.json"
|
||||||
|
)
|
||||||
|
f_data = defaultdict(dict)
|
||||||
|
for i in in_data:
|
||||||
|
f_data[i["id"]].update({i["type"]: i["result"]})
|
||||||
|
|
||||||
|
bulk_data = []
|
||||||
|
failed_data = []
|
||||||
|
total_count = 0
|
||||||
|
for id, dict_item in f_data.items():
|
||||||
|
db_data = elastic.get_document(out_put_index_name, id)
|
||||||
|
|
||||||
|
_id = db_data["_id"]
|
||||||
|
_source = db_data["_source"]
|
||||||
|
conflict_detection = dict_item.get("conflict_detection", None)
|
||||||
|
if conflict_detection:
|
||||||
|
_source['conflict_detection']["description"] = conflict_detection
|
||||||
|
conflict_type_detection = dict_item.get("conflict_type_detection", None)
|
||||||
|
if conflict_type_detection:
|
||||||
|
_source['conflict_type_detection']["description"] = conflict_type_detection
|
||||||
|
|
||||||
|
|
||||||
|
bulk_data.append({"_id": _id, "_source": _source})
|
||||||
|
# print(
|
||||||
|
# f'bulk_data {bulk_data}'
|
||||||
|
# )
|
||||||
|
# break
|
||||||
|
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_translate()
|
||||||
|
|
||||||
|
# python -m dataParsers.app.update_mj_qa_rules_persian_text
|
||||||
Reference in New Issue
Block a user