Files
init_dataParser/app/majles/majles_conflict_ranks_v3.py
T
2026-07-15 19:29:16 +03:30

462 lines
14 KiB
Python

import numbers
import os
from dotenv import load_dotenv
from elasticsearch import Elasticsearch, helpers
import json
import re
import zipfile
import pandas as pd
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
from app.common.elastic_helper import ElasticHelper
from app.common.general_functions import *
from app.common.text_normalizer import TextNormalizer
from app.common.data_helper import DataHelper
text_normal = TextNormalizer()
def clear_array_object_by(array, version_key):
array = [c for c in array if c["version_key"] != version_key]
return array
def compute_votes(list_main, conflict_type):
vote = 0
count = 0
for su in list_main :
if not "ref_key" in su :
continue
ref_key = su["ref_key"]
if "has_conflict" in su :
main_type = su["has_conflict"]
main_type = "true" if main_type is True else "false"
else :
main_type = su["main_type"]
if not ref_key in dict_version_weight[conflict_type] :
continue
version_weight = dict_version_weight[conflict_type][ref_key]
if main_type in dict_main_type_weight :
weight = dict_main_type_weight[main_type]
vote += weight * version_weight
count += 1
return vote, count
def merge_unrepeat_by_ref_key(list1, list2) :
for item in list2 :
if not "main_type" in item or not "ref_key" in item :
continue
ref_key = item["ref_key"]
find = [i for i in list1 if i["ref_key"] == ref_key]
if not find :
list1.append(item)
return list1
## -----------------------------------------------------------------------
def compute_avg_all(elastic, index_name="mj_qa_conflict", body={}):
try:
list_ = elastic.es_iterate_all_documents(index_name, body)
except Exception as e:
save_error(0, e)
data = list(list_)
total = len(data)
percent_prev = 0
count = 0
bulk_data = []
total_count = 0
total_vote_su = 0
total_count_su = 0
total_vote_cd = 0
total_count_cd = 0
total_vote_rt = 0
total_count_rt = 0
for mentry in data:
count += 1
entry = mentry["source"]
_id = entry.get("id", "")
subject_unitys = entry.get("subject_unitys", [])
conflict_detections = entry.get("conflict_detections", [])
# conflict_type_detections = entry.get("conflict_type_detections", [])
conflict_relation_identifications = entry.get("conflict_relation_identifications", [])
conflict_evaluations = entry.get("conflict_evaluations", [])
percent = int((count / total) * 100)
if percent != percent_prev:
print("compute_avg_all -> %.2f %%" % percent)
percent_prev = percent
vote, count = compute_votes(subject_unitys, "subject_unitys")
total_vote_su += vote
total_count_su += count
vote, count = compute_votes(conflict_detections, "conflict_detections")
total_vote_cd += vote
total_count_cd += count
relation_identifications = conflict_relation_identifications
relation_identifications = merge_unrepeat_by_ref_key(relation_identifications, conflict_evaluations)
vote, count = compute_votes(relation_identifications, "relation_identifications")
total_vote_rt += vote
total_count_rt += count
C_avg_su = total_vote_su/ total_count_su if total_count_su > 0 else 0
C_avg_cd = total_vote_cd/ total_count_cd if total_count_cd > 0 else 0
C_avg_rt = total_vote_rt/ total_count_rt if total_count_rt > 0 else 0
print( "-" * 50 )
print( "-" * 5, "subject_unity total_vote, total_count, avge : ", total_vote_su, total_count_su, C_avg_su )
print( "-" * 10 )
print( "-" * 5, "conflict_detections total_vote, total_count, avge : ", total_vote_cd, total_count_cd, C_avg_cd )
print( "-" * 10 )
print( "-" * 5, "conflict_relation_identifications total_vote, total_count, avge : ", total_vote_rt, total_count_rt, C_avg_rt )
print( "-" * 50 )
return C_avg_su, C_avg_cd, C_avg_rt
def re_compute_conflict_ranks(elastic, index_name, C_avg_su, C_avg_cd, C_avg_rt, m_count= 3, body= {}):
try:
list_ = elastic.es_iterate_all_documents(index_name, body)
except Exception as e:
save_error(0, e)
data = list(list_)
total = len(data)
print( "-" * 50)
print( " total count --> ", total)
print( "-" * 50)
percent_prev = 0
count = 0
bulk_data = []
total_count = 0
for mentry in data:
count += 1
entry = mentry["source"]
_id = entry.get("id", "")
subject_unitys = entry.get("subject_unitys", [])
conflict_detections = entry.get("conflict_detections", [])
# conflict_type_detections = entry.get("conflict_type_detections", [])
conflict_relation_identifications = entry.get("conflict_relation_identifications", [])
conflict_evaluations = entry.get("conflict_evaluations", [])
conflict_ranks = entry.get("conflict_ranks", [])
percent = int((count / total) * 100)
if percent != percent_prev:
print("re_compute_conflict_ranks -> %.2f %%" % percent, total)
percent_prev = percent
vote_su, count_su = compute_votes(subject_unitys, "subject_unitys")
avg_su = vote_su / count_su if count_su > 0 else 0
if count_su == 0 :
bayes_su = 0
else:
bayes_su = (count_su * avg_su + m_count * C_avg_su) / (count_su + m_count)
vote_cd, count_cd = compute_votes(conflict_detections, "conflict_detections")
avg_cd = vote_cd / count_cd if count_cd > 0 else 0
if count_cd == 0 :
bayes_cd = 0
else:
bayes_cd = (count_cd * avg_cd + m_count * C_avg_cd) / (count_cd + m_count)
relation_identifications = conflict_relation_identifications
relation_identifications = merge_unrepeat_by_ref_key(relation_identifications, conflict_evaluations)
vote_rt, count_rt = compute_votes(relation_identifications, "relation_identifications")
avg_rt = vote_rt / count_rt if count_rt > 0 else 0
if count_rt == 0 :
bayes_rt = 0
else:
bayes_rt = (count_rt * avg_rt + m_count * C_avg_rt) / (count_rt + m_count)
conflict_rank = {
"subject_unity_count": count_su,
"subject_unity_avg": round(avg_su * 100, 3),
"subject_unity_bayes": round(bayes_su * 100, 3),
"conflict_detection_count": count_cd,
"conflict_detection_avg": round(avg_cd * 100, 3),
"conflict_detection_bayes": round(bayes_cd * 100, 3),
"relation_identification_count": count_rt,
"relation_identification_avg": round(avg_rt * 100, 3),
"relation_identification_bayes": round(bayes_rt * 100, 3)
}
data = {
"conflict_rank": conflict_rank
}
bulk_data.append({"_id": _id, "_source": data})
if len(bulk_data) > 100:
total_count += len(bulk_data)
# print("=" * 10, "to_elastic , total=" + str(total_count))
elastic.updateBulkList(bulk_data, index_name)
bulk_data = []
if len(bulk_data) > 0:
total_count += len(bulk_data)
# print("=" * 10, "to_elastic , total=" + str(total_count))
elastic.updateBulkList(bulk_data, index_name)
bulk_data = []
dict_main_type_weight={
"yes" : 1,
"no" : -1,
"yes_under_assumptions" : 0.8,
"true" : 1,
"false" : -1,
"تعارض مستقر": 1,
"مطلق مقدم، مقید موخر": 1,
"مقید مقدم، مطلق موخر": 1,
"تکرار حکم": 1,
"بدون تعارض": -1,
# "تایید وجود وحدت موضوعی": 0.5,
# "تایید تشخیص نوع مخالفت": 0.5,
# "تایید تشخیص نوع تعارض": 0.5,
# "تایید تشخیص مخالفت": 0.5,
# "رد تشخیص مخالفت": -0.5,
# "رد تشخیص نوع تعارض": -0.5,
# "رد تشخیص نوع مخالفت": -0.5,
# "رد وجود وحدت موضوعی": -0.5,
}
dict_version_weight = {
"subject_unitys" : {
"oss120": 0.8,
"gemma4": 1,
"qwen3-6": 1,
"deepseek": 1,
"qwopus4_6": 1,
"qwen3_5_122B": 1,
"qwen3_80B": 1,
"gemma4_103": 1,
"deepseek_103": 1,
"oss120_103": 1,
"qwen3_5_103": 1,
"qwen3_6_103": 1,
"qwen3_103": 1,
"qwopus4_6_103": 1,
"yourmind_103": 1,
},
"conflict_detections" : {
"oss120": 0.8,
"gemma4": 1,
"qwen3-6": 1,
"deepseek": 1,
"qwopus4_6": 1,
"qwen3_5_122B": 1,
"qwen3_80B": 1,
"openyourmind": 1.2,
"gemma4_103": 1,
"deepseek_103": 1,
"oss120_103": 1,
"qwen3_5_103": 1,
"qwen3_6_103": 1,
"qwen3_103": 1,
"qwopus4_6_103": 1,
"yourmind_103": 1,
},
"relation_identifications" : {
"oss120": 0.8,
"gemma4": 1,
"qwen3-6": 1,
"deepseek": 1,
"qwopus4_6": 1,
"qwen3_5_122B": 1,
"qwen3_80B": 1,
"openyourmind": 1,
"gemma4_103": 1,
"deepseek_103": 1,
"oss120_103": 1,
"qwen3_5_103": 1,
"qwen3_6_103": 1,
"qwen3_103": 1,
"qwopus4_6_103": 1,
"yourmind_103": 1,
}
}
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
load_dotenv(dotenv_path="config.env")
host = os.getenv("HOST_160")
password = os.getenv("HOST_160_PASS")
# host = os.getenv("HOST_112")
# password = os.getenv("HOST_112_PASS")
# host = os.getenv("HOST_H102")
# password = os.getenv("HOST_H102_PASS")
# print( "---------------->>>>>>>>>>>>>>>", host )
# host=os.getenv('HOST_113')
# password=os.getenv('HOST_113_PASS')
print(".... majles_codes_update_steps ......", host)
path_base_map = "G:/Project2/tavasi_160/src/repo/_other/"
elastic = ElasticHelper(host, password, "elastic", path_base_map)
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
version_keys_v3 = [
"su-gemma4_cd-ri-urmind_e-qwen3.6_285k_group_v3"
]
## -----------------------------------------------------------------------
body_v3={
"query":{
"terms": {
"version_key": version_keys_v3
}
}
}
body_103={
"query": {
"nested": {
"path": "subject_unitys",
"query": {
"term": {
"subject_unitys.ref_key": {
"value": "gemma4_103"
}
}
}
}
}
}
body_no_rank={
"query": {
"bool": {
"must":[
{
"terms": {
"version_key": version_keys_v3
}
}
],
"must_not": [
{
"exists": {
"field": "conflict_rank.subject_unity_count"
}
}
]
}
}
}
# C_avg_su, C_avg_cd, C_avg_rt = compute_avg_all(elastic, "mj_qa_conflict", body_v3)
# #--------------------------------------------------
# #----- subject_unity total_vote, total_count, avge : 283938.199999187 314203 0.9036775587731085
# #----------
# #----- conflict_detections total_vote, total_count, avge : -337630.80000177945 314203 -1.0745626235324917
# #----------
# #----- conflict_relation_identifications total_vote, total_count, avge : 41412.600000000915 114953 0.3602568006054728
# #--------------------------------------------------
# #--------------------------------------------------
# #----- subject_unity total_vote, total_count, avge : 350754.3999994566 663381 0.528737482682586
# #----------
# #----- conflict_detections total_vote, total_count, avge : -891082.9999999751 994299 -0.8961921916847699
# #----------
# #----- conflict_relation_identifications total_vote, total_count, avge : 83677.40000002725 158600 0.5276002522069814
# #--------------------------------------------------
C_avg_su = 0.528737482682586
# C_avg_cd = -1.0745626235324917
C_avg_cd = 0
C_avg_rt = 0.5276002522069814
### -----103 بسته -----------
# C_avg_su = 0.1315904139433552
# C_avg_cd = 0.09096573208722765
# C_avg_rt = 0.6596421471172959
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_103)
### ---------------------------
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_no_rank)
re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_v3)
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
# .\.env\Scripts\activate
# python -m app.majles.majles_conflict_ranks_v3