552 lines
16 KiB
Python
552 lines
16 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" : 1,
|
|
|
|
"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,
|
|
"oss120-v2": 1,
|
|
"gemma4-v3": 1.5,
|
|
"Magistral-Small-p3": 1,
|
|
"DeepSeek-R1-32B-p3": 1,
|
|
"Qwen3-80B-p3": 1.2,
|
|
"Qwen3.6-35B-p3": 1.2,
|
|
"gemma-4-p3": 1.5,
|
|
"gpt-oss-120b-p3": 1,
|
|
"gpt-oss-20b-p3": 0.8,
|
|
"openyourmind": 1,
|
|
"qwen3-6": 1,
|
|
"gemma4-p2": 1,
|
|
"oss120-p2": 1,
|
|
"qwen3-6-p2": 1.2,
|
|
"yourmind-p3": 1
|
|
},
|
|
"conflict_detections" : {
|
|
"oss120": 0.8,
|
|
"oss120-v2": 1,
|
|
"gemma4-v3": 1.2,
|
|
"Magistral-Small-p3": 1.2,
|
|
"DeepSeek-R1-32B-p3": 1,
|
|
"Qwen3-80B-p3": 1.4,
|
|
"Qwen3.6-35B-p3": 1.4,
|
|
"gemma-4-p3": 1.2,
|
|
"gpt-oss-120b-p3": 1,
|
|
"gpt-oss-20b-p3": 0.8,
|
|
"openyourmind": 1.2,
|
|
"qwen3-6": 1,
|
|
"gemma4-p2": 1,
|
|
"oss120-p2": 1,
|
|
"qwen3-6-p2": 1,
|
|
"yourmind-p3": 1.2
|
|
},
|
|
"relation_identifications" : {
|
|
"oss120": 0.5,
|
|
"oss120-v2": 0.8,
|
|
"gemma4-v3": 1,
|
|
"Magistral-Small-p3": 1,
|
|
"DeepSeek-R1-32B-p3": 1,
|
|
"Qwen3-80B-p3": 1.2,
|
|
"Qwen3.6-35B-p3": 1.2,
|
|
"gemma-4-p3": 1,
|
|
"gpt-oss-120b-p3": 1,
|
|
"gpt-oss-20b-p3": 0.5,
|
|
"openyourmind": 1,
|
|
"qwen3-6": 1,
|
|
"gemma4-p2": 1,
|
|
"oss120-p2": 1,
|
|
"qwen3-6-p2": 1,
|
|
"yourmind-p3": 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_v2 = [
|
|
"pair_gvc_e1_jv3_130k_chunk_v2_all_qavanin_part1", # 130826
|
|
"oss120b-Qwen3.6.35B.eval_all_qavanin_prompt2", # 127078
|
|
"su-gemma4_cd-ri-urmind_e-qwen3.6_p1p2_eval_oss_valid_sections_only", # 28094
|
|
"oss120b_Qwen3.6.35B.eval_backup.gemma4.31B_all_qavanin_prompt2", # 5163
|
|
"pair_gvc_e1_jv3_6152_chunk_v2", # 3071
|
|
"pair_gvc_e1_jv3_2880_chunk_v2", # 2879
|
|
"pair_gvc_e1_jv3_2501_chunk_v2", # 2501
|
|
"pair_gvc_e1_jv3_all" # 1835
|
|
]
|
|
|
|
## -----------------------------------------------------------------------
|
|
|
|
body={
|
|
"query":{
|
|
"terms": {
|
|
"version_key": version_keys_v2
|
|
}
|
|
}
|
|
}
|
|
# C_avg_su, C_avg_cd, C_avg_rt = compute_avg_all(elastic, "mj_qa_conflict", body)
|
|
# #--------------------------------------------------
|
|
# #----- subject_unity total_vote, total_count, avge : 108125.20000029108 346281 0.3122469901620103
|
|
# #----- conflict_detections total_vote, total_count, avge : 36015.799999964365 232220 0.15509344587014195
|
|
# #----- conflict_relation_identifications total_vote, total_count, avge : 123440.40000029562 289375 0.4265758963293153
|
|
# #--------------------------------------------------
|
|
# #----- subject_unity total_vote, total_count, avge : 108125.20000028852 346281 0.31224699016200286
|
|
# #----- conflict_detections total_vote, total_count, avge : 36015.79999992643 232220 0.15509344586997859
|
|
# #----- conflict_relation_identifications total_vote, total_count, avge : 119328.4000002682 148263 0.8048427456632349
|
|
# #--------------------------------------------------
|
|
# #----- subject_unity total_vote, total_count, avge : 108125.20000027672 346281 0.3122469901619688
|
|
# #----- conflict_detections total_vote, total_count, avge : 36015.799999949406 232220 0.15509344587007753
|
|
# #----- conflict_relation_identifications total_vote, total_count, avge : 75817.70000002025 148263 0.5113730330562598
|
|
# #--------------------------------------------------
|
|
C_avg_su = 0.3122469901620103
|
|
C_avg_cd = 0.15509344587014195
|
|
C_avg_rt = 0.5113730330562598
|
|
C_avg_rt = 0.30
|
|
|
|
|
|
body={
|
|
"query": {
|
|
"bool": {
|
|
"must":[
|
|
{
|
|
"terms": {
|
|
"version_key": version_keys_v2
|
|
}
|
|
}
|
|
],
|
|
"must_not": [
|
|
{
|
|
"exists": {
|
|
"field": "conflict_rank.subject_unity_count"
|
|
}
|
|
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
body_100={
|
|
"query": {
|
|
"nested": {
|
|
"path": "subject_unitys",
|
|
"query": {
|
|
"term": {
|
|
"subject_unitys.ref_key": {
|
|
"value": "Qwen3-80B-p3"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
body_7005={
|
|
"query": {
|
|
"nested": {
|
|
"path": "conflict_evaluations",
|
|
"query": {
|
|
"term": {
|
|
"conflict_evaluations.ref_key": {
|
|
"value": "qwen3-6-v2"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
body_51000={
|
|
"query": {
|
|
"bool": {
|
|
"must":[
|
|
{
|
|
"nested": {
|
|
"path": "subject_unitys",
|
|
"query": {
|
|
"term": {
|
|
"subject_unitys.ref_key": {
|
|
"value": "gemma4-v3"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
],
|
|
"must_not": [
|
|
{
|
|
"exists": {
|
|
"field": "conflict_rank.subject_unity_count"
|
|
}
|
|
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
body_test1={
|
|
"query": {
|
|
"bool": {
|
|
"must": [
|
|
{
|
|
"terms": {
|
|
"version_key": version_keys_v2
|
|
}
|
|
},
|
|
{
|
|
"term": {
|
|
"previous_info.tcode_mains": {
|
|
"value": "بیمه"
|
|
}
|
|
}
|
|
}
|
|
],
|
|
"must_not": [
|
|
{
|
|
"nested": {
|
|
"path": "subject_unitys",
|
|
"query": {
|
|
"term": {
|
|
"subject_unitys.ref_key": {
|
|
"value": "Qwen3-80B-p3"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"nested": {
|
|
"path": "conflict_evaluations",
|
|
"query": {
|
|
"term": {
|
|
"conflict_evaluations.ref_key": {
|
|
"value": "qwen3-6-v2"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_100)
|
|
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_7005)
|
|
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_51000)
|
|
|
|
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body)
|
|
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_test1)
|
|
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, {})
|
|
|
|
## -----------------------------------------------------------------------
|
|
## -----------------------------------------------------------------------
|
|
## -----------------------------------------------------------------------
|
|
## -----------------------------------------------------------------------
|
|
## -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
## -----------------------------------------------------------------------
|
|
## -----------------------------------------------------------------------
|
|
# .\.env\Scripts\activate
|
|
# python -m app.majles.majles_conflict_ranks
|