From 0bc3f9a63347444926933e08024f05a4b571856f Mon Sep 17 00:00:00 2001 From: hsafaei Date: Mon, 15 Jun 2026 13:12:04 +0330 Subject: [PATCH] 1 --- 1_errors.txt | 30 + app/common/general_functions.py | 26 +- app/majles/majles_conflict_works.py | 1041 ++++++++++++++++++++++++++- app/majles/majles_plans_import.py | 71 +- app/monir/monir_teamwork.py | 519 +++++++++++++ 5 files changed, 1653 insertions(+), 34 deletions(-) create mode 100644 app/monir/monir_teamwork.py diff --git a/1_errors.txt b/1_errors.txt index 22fe6e9..e300531 100644 --- a/1_errors.txt +++ b/1_errors.txt @@ -7,4 +7,34 @@ err message : 'full_path' err date : 2026-02-17 13:19:08.603065 +************************************************** + + id : 0 + filename : G:\Project2\dataParsers\app\majles\majles_conflict_works.py + function : fill_meta_data + err line no : 104 + err line : list_ = elastic.es_iterate_all_documents(index_name_i, body) + err message : name 'index_name_i' is not defined + err date : 2026-05-11 13:10:26.559717 + +************************************************** + + id : 0 + filename : G:\Project2\dataParsers\app\monir\monir_teamwork.py + function : import_from_jahat_entities + err line no : 24 + err line : list_ = elastic.es_iterate_all_documents(index_name, body) + err message : name 'index_name' is not defined + err date : 2026-05-12 15:05:48.540392 + +************************************************** + + id : 0 + filename : G:\Project2\dataParsers\app\majles\majles_conflict_works.py + function : genrate_excel_data + err line no : 598 + err line : list_ = elastic.es_iterate_all_documents(out_put_index_name, body) + err message : name 'out_put_index_name' is not defined + err date : 2026-06-01 19:09:38.841180 + ************************************************** diff --git a/app/common/general_functions.py b/app/common/general_functions.py index 44222b5..ab6a580 100644 --- a/app/common/general_functions.py +++ b/app/common/general_functions.py @@ -1009,6 +1009,23 @@ def save_error(id=0, e=Exception): file.write(error + '\n' + 50*'*' + '\n') return error +def is_image_file(file_path): + if not file_path : + return False + + ext = os.path.splitext(file_path)[1].lower() + + image_extensions = { + '.jpg', '.jpeg', '.png', '.gif', '.bmp', + '.webp', '.tiff', '.svg', '.ico' + } + + is_image = ext in image_extensions + return is_image + # # example + # info = is_image_file('/path/to/file.png') + # print(info) + # تابع برای تعیین نوع عنوان قانونی def getTitleType(caption: str) -> str: # عادی‌سازی متن ورودی @@ -1018,17 +1035,20 @@ def getTitleType(caption: str) -> str: if "موافقتنامه" in caption or "موافقت نامه" in caption or "کنوانسیون" in caption or \ "قرارداد بین المللی" in caption or "مقررات بین المللی" in caption or "الحاق دولت" in caption or \ "الحاق ایران" in caption or "قرارداد بین المللی" in caption or \ - caption.startswith("قانون عضویت دولت") or caption.startswith("قانون عضویت ایران") or caption.startswith("قانون اجازه عضویت"): + caption.startswith("قانون عضویت دولت") or caption.startswith("قانون عضویت ایران") or \ + caption.startswith("قانون اجازه عضویت") or caption.startswith("قانون معاهده") or caption.startswith("قانون یادداشت تفاهم بین") : return "موافقتنامه" elif caption.startswith("الحاق ") or caption.startswith("قانون الحاق ") or caption.startswith("قانون الحاقیه "): return "الحاقی" elif re.match(r"^قانون اضافه.*قانون", caption): return "الحاقی" - elif caption.startswith("اصلاح ") or re.match(r"^قانون اصلاح(?:ی)? .*قانون", caption) or re.match(r"^لایحه قانونی اصلاح(?:ی)? .*لایحه", caption): + elif caption.startswith("اصلاح ") or re.match(r"^قانون اصلاح(?:ی)? .*(قانون|قوانین)", caption) or re.match(r"^لایحه قانونی اصلاح(?:ی)? .*لایحه", caption): return "اصلاحی" elif re.match(r"^قانون اصلاحیه.*قانون", caption): return "اصلاحی" + elif caption.startswith("اصلاحیه "): + return "اصلاحی" elif re.match(r"^قانون تغییر.*قانون", caption): return "اصلاحی" elif caption.startswith("نظر تفسیری ") or caption.startswith("استفساریه ") or caption.startswith("تفسیر ") or caption.startswith("قانون تفسیر ") or caption.startswith("قانون استفساریه "): @@ -1075,7 +1095,7 @@ def getTitleType(caption: str) -> str: else: return "اصلی" - +# ["الحاقی","اصلاحی","تفسیری","نامه","لغو","حذفی","تمدیدی","شمولی","نسخی","مربوط"] #//////////////////////////////////////////////////////////// #//////////////////////////////////////////////////////////// diff --git a/app/majles/majles_conflict_works.py b/app/majles/majles_conflict_works.py index 0e521f8..6537dcf 100644 --- a/app/majles/majles_conflict_works.py +++ b/app/majles/majles_conflict_works.py @@ -5,6 +5,8 @@ from elasticsearch import Elasticsearch, helpers import json import re import zipfile +import pandas as pd + import sys @@ -40,6 +42,7 @@ def get_evaluation_data(data): result.append("رد تشخیص نوع تعارض") return result + def get_vlid_text(text): if text == "valid": return "معتبر" @@ -47,17 +50,17 @@ def get_vlid_text(text): return "نامعتبر" elif text == "undetermined": return "نامشخص" - -def import_conflict_to_elastic( file_path, out_put_index_name): - + +def import_conflict_to_elastic(file_path, out_put_index_name): + data_all = DataHelper.load_orjson(file_path) bulk_data = [] failed_data = [] total_count = 0 - - for item in data_all : + + for item in data_all: _id = item["id"] previous_info = item["previous_info"] @@ -77,11 +80,11 @@ def import_conflict_to_elastic( file_path, out_put_index_name): "next_info": next_info, "subject_unity": subject_unity, "conflict_detection": conflict_detection, - "conflict_type_detection":conflict_type_detection, - "conflict_relation_identification":conflict_relation_identification, + "conflict_type_detection": conflict_type_detection, + "conflict_relation_identification": conflict_relation_identification, "conflict_evaluation": conflict_evaluation, "conflict_final": conflict_final, - } + } bulk_data.append({"_id": _id, "_source": es_data}) if len(bulk_data) > 200: @@ -95,19 +98,826 @@ def import_conflict_to_elastic( file_path, out_put_index_name): total_count += len(bulk_data) print("=" * 10, "to_elastic , total=" + str(total_count)) elastic.importBulkList(bulk_data, out_put_index_name) - bulk_data = [] + bulk_data = [] + + +def fill_meta_data(elastic, out_put_index_name, body={}): + try: + list_ = elastic.es_iterate_all_documents(out_put_index_name, body) + except Exception as e: + save_error(0, e) + + data = list(list_) + total = len(data) + count = 0 + bulk_data = [] + total_count = 0 + + for mentry in data: + count += 1 + entry = mentry["source"] + _id = entry.get("id", "") + + previous_info = entry.get("previous_info", {}) + qanon_title1 = previous_info.get("qanon_title", "") + qanon_id1 = previous_info.get("qanon_id", "") + + next_info = entry.get("next_info", {}) + qanon_title2 = next_info.get("qanon_title", "") + qanon_id2 = next_info.get("qanon_id", "") + + title_type1 = getTitleType(qanon_title1) + title_type2 = getTitleType(qanon_title2) + conflict_tags = [] + if qanon_id1 == qanon_id2: + conflict_tags.append("هم قانون") + + previous_info["title_type"] = title_type1 + next_info["title_type"] = title_type2 + data = { + "previous_info": previous_info, + "next_info": next_info, + "conflict_tags": conflict_tags, + } + 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, 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.updateBulkList(bulk_data, out_put_index_name) + bulk_data = [] + + +# اصلاح برای ساختار جدید nested +def restore_correct_data_to_elastic( + elastic, file_path, index_name, ref_key="oss120", file_error="" +): + error_list = [] + fileNo = 0 + with zipfile.ZipFile(file_path, "r") as zObject: + fileNo += 1 + + packNo = 0 + counter = 0 + for filename in zObject.namelist(): + packNo += 1 + + print("index:", index_name, "=>", filename) + data = [] + with zObject.open(filename) as file: + data = json.loads(file.read()) + + data_new = correct_nested_json(data, error_list, ref_key) + if index_name == "mj_mashruh": + data_new = normal_mashruh_data_json(data_new) + + elastic.importJsonDataToElastic(data_new, index_name, [], True) + counter += len(data) + # print('-'*10, 'len()', str(counter) + '/ ' + str(len(data)) + ' , ' +str(len(data_new)) ) + + elastic.es.indices.refresh(index=index_name) + print(elastic.es.cat.count(index=index_name, format="json")) + + file_log_error = file_error + if not file_error: + file_log_error = "G:/Project2/160_dotic/date_log_error.txt" + with open(file_log_error, "w", encoding="utf8") as f: + json.dump(error_list, f, ensure_ascii=False, indent=4) + + +# اصلاح برای ساختار جدید nested +def correct_nested_json(data, error_list, ref_key): + data_new = [] + for item in data: + id = item["_id"] + + # یکبار فقط نیاز بود + id = str(id).replace("mj_mashruh_", "mh") + item["_id"] = id + item["_source"]["id"] = id + + # "subject_unitys", + # "conflict_detections", + # "conflict_type_detections", + # "conflict_relation_identifications", + # "conflict_evaluations", + # "conflict_finals + + if "subject_unity" in item["_source"]: + cd = item["_source"]["subject_unity"] + cd["ref_key"] = ref_key + del item["_source"]["subject_unity"] + item["_source"]["subject_unitys"] = [cd] + + if "conflict_detection" in item["_source"]: + cd = item["_source"]["conflict_detection"] + cd["ref_key"] = ref_key + del item["_source"]["conflict_detection"] + item["_source"]["conflict_detections"] = [cd] + + if "conflict_type_detection" in item["_source"]: + cd = item["_source"]["conflict_type_detection"] + cd["ref_key"] = ref_key + del item["_source"]["conflict_type_detection"] + item["_source"]["conflict_type_detections"] = [cd] + + if "conflict_relation_identification" in item["_source"]: + cd = item["_source"]["conflict_relation_identification"] + cd["ref_key"] = ref_key + del item["_source"]["conflict_relation_identification"] + item["_source"]["conflict_relation_identifications"] = [cd] + + if "conflict_evaluation" in item["_source"]: + cd = item["_source"]["conflict_evaluation"] + cd["ref_key"] = ref_key + del item["_source"]["conflict_evaluation"] + item["_source"]["conflict_evaluations"] = [cd] + + if "conflict_final" in item["_source"]: + cd = item["_source"]["conflict_final"] + cd["ref_key"] = ref_key + del item["_source"]["conflict_final"] + item["_source"]["conflict_finals"] = [cd] + + data_new.append(item) + return data_new + + +## ----------------------------------------------------------------------- +## ----------------------------------------------------------------------- + + +def append_conflict_to_elastic( + elastic, + input_path, + out_put_index_name, + ref_key, + fields=[ + "subject_unity", + "conflict_detection", + "relation_identification", + "conflict_evaluation", + "llm_output", + ], + ref_key_advance = {}, + import_if_not_found = False +): + list_files = [] + if os.path.isdir(input_path): + list_files = [os.path.join(input_path, f) for f in os.listdir(input_path) if os.path.isfile(os.path.join(input_path, f))] + elif os.path.isfile(path): + list_files.append(input_path) + + for file in list_files : + + data_all = [] + try: + data_all = DataHelper.load_orjson(file) + except: + print("********** error read data file json " , file) + continue + + print("@"*5,"file start...", file) + list_notfound = [] + bulk_data = [] + bulk_data_import = [] + failed_data = [] + total_count = 0 + + for item in data_all: + + _id = item["id"] + + # if _id == "qs2489164_v2-9.qs2365626_v2-7": + # print("qs2489164_v2-9.qs2365626_v2-7") + + import_state = False + _source = {} + res = None + try: + res = elastic.get_document(out_put_index_name, _id) + except: + list_notfound.append(_id) + if not import_if_not_found : + continue + + import_state = True + version_key = item["version_key"] + + previous_info = item["previous_info"] + qanon_title1 = previous_info["qanon_title"] + qanon_id1 = previous_info["qanon_id"] + + next_info = item["next_info"] + qanon_title2 = next_info["qanon_title"] + qanon_id2 = next_info["qanon_id"] + + title_type1 = getTitleType(qanon_title1) + title_type2 = getTitleType(qanon_title2) + conflict_tags = [] + if qanon_id1 == qanon_id2: + conflict_tags.append("هم قانون") + + previous_info["title_type"] = title_type1 + next_info["title_type"] = title_type2 + + _source = { + "previous_info": previous_info, + "next_info": next_info, + "conflict_tags": conflict_tags, + "version_key":version_key, + "subject_unitys": [], + "conflict_detections": [], + "conflict_type_detections": [], + "conflict_relation_identifications": [], + "conflict_evaluations": [], + "conflict_finals": [], + } + res = { + "_id": id, + "_source": _source + } + + # print("id start...", _id) + + subject_unitys = res["_source"]["subject_unitys"] if "subject_unitys" in res["_source"] else [] + conflict_detections = res["_source"]["conflict_detections"] if "conflict_detections" in res["_source"] else [] + conflict_type_detections = res["_source"]["conflict_type_detections"] if "conflict_type_detections" in res["_source"] else [] + conflict_relation_identifications = res["_source"]["conflict_relation_identifications"] if "conflict_relation_identifications" in res["_source"] else [] + conflict_evaluations = res["_source"]["conflict_evaluations"] if "conflict_evaluations" in res["_source"] else [] + conflict_finals = res["_source"]["conflict_finals"] if "conflict_finals" in res["_source"] else [] + + subject_unity = {} + conflict_detection = {} + conflict_type_detection = {} + conflict_relation_identification = {} + conflict_evaluation = {} + conflict_final = {} + + data = {} + if ( + "subject_unity" in item + and item["subject_unity"] + and "subject_unity" in fields + ): + main_type = "" + description = "" + reason = "" + ref_key_adv = ref_key_advance["subject_unity"] if "subject_unity" in ref_key_advance else ref_key + + ss = item["subject_unity"] + if "main_type" in ss: + main_type = ss["main_type"] + elif "has_subject_unity" in ss: + main_type = ss["has_subject_unity"] + + if "required_assumptions" in ss: + description = ss["required_assumptions"] + elif "description" in ss: + description = ss["description"] + + if ( + "matching_rules" in ss + and len(ss["matching_rules"]) + and "reasoning" in ss["matching_rules"][0] + ): + reason = ss["matching_rules"][0]["reasoning"] + elif "reason" in ss: + reason = ss["reason"] + cd = { + "ref_key": ref_key_adv, + "main_type": main_type, + "description": description, + "reason": reason, + } + + subject_unitys = clear_array_object_by(subject_unitys, ref_key_adv) + + subject_unitys.append(cd) + data["subject_unitys"] = subject_unitys + + if ( + "conflict_detection" in item + and item["conflict_detection"] + and "conflict_detection" in fields + ): + description = "" + ref_key_adv = ref_key_advance["conflict_detection"] if "conflict_detection" in ref_key_advance else ref_key + + + ss = item["conflict_detection"] + if "has_conflict" in ss: + has_conflict = ss["has_conflict"] + + if "explanation_of_conflict" in ss: + description = ss["explanation_of_conflict"] + elif "description" in ss: + description = ss["description"] + + cd = { + "ref_key": ref_key_adv, + "has_conflict": has_conflict, + "description": description, + } + + conflict_detections = clear_array_object_by(conflict_detections, ref_key_adv) + + conflict_detections.append(cd) + data["conflict_detections"] = conflict_detections + + if ( + "conflict_type_detection" in item + and item["conflict_type_detection"] + and "conflict_type_detection" in fields + ): + main_type = "" + description = "" + ref_key_adv = ref_key_advance["conflict_type_detection"] if "conflict_type_detection" in ref_key_advance else ref_key + + + ss = item["conflict_type_detection"] + if "main_type" in ss: + main_type = ss["main_type"] + elif "conflict_type" in ss: + main_type = ss["conflict_type"] + + if "description" in ss: + description = ss["description"] + elif "explanation_of_type" in ss: + description = ss["explanation_of_type"] + cd = { + "ref_key": ref_key_adv, + "main_type": main_type, + "description": description, + } + + conflict_type_detections = clear_array_object_by(conflict_type_detections, ref_key_adv) + + conflict_type_detections.append(cd) + data["conflict_type_detections"] = conflict_type_detections + + if ( + "relation_identification" in item + and item["relation_identification"] + and "relation_identification" in fields + ): + main_type = "" + description = "" + ref_key_adv = ref_key_advance["relation_identification"] if "relation_identification" in ref_key_advance else ref_key + + + ss = item["relation_identification"] + if "main_type" in ss: + main_type = ss["main_type"] + elif "relation_type" in ss: + main_type = ss["relation_type"] + + if "description" in ss: + description = ss["description"] + elif "reasoning" in ss: + description = ss["reasoning"] + + cd = { + "ref_key": ref_key_adv, + "main_type": main_type, + "description": description, + } + + conflict_relation_identifications = clear_array_object_by(conflict_relation_identifications, ref_key_adv) + conflict_relation_identifications.append(cd) + data["conflict_relation_identifications"] = ( + conflict_relation_identifications + ) + + # #--- در خروجی تغییر اسم داده شد conflict_relation_type = conflict_relation_identification + if ( + "conflict_relation_type" in item + and item["conflict_relation_type"] + and "conflict_relation_type" in fields + ): + main_type = "" + description = "" + ref_key_adv = ref_key_advance["conflict_relation_type"] if "conflict_relation_type" in ref_key_advance else ref_key + + + ss = item["conflict_relation_type"] + if "main_type" in ss: + main_type = ss["main_type"] + elif "relation_type" in ss: + main_type = ss["relation_type"] + + if "description" in ss: + description = ss["description"] + elif "reasoning" in ss: + description = ss["reasoning"] + + cd = { + "ref_key": ref_key_adv, + "main_type": main_type, + "description": description, + } + + conflict_relation_identifications = clear_array_object_by(conflict_relation_identifications, ref_key_adv) + conflict_relation_identifications.append(cd) + data["conflict_relation_identifications"] = ( + conflict_relation_identifications + ) + + if ( + "conflict_evaluation" in item + and item["conflict_evaluation"] + and "conflict_evaluation" in fields + ): + ref_key_adv = ref_key_advance["conflict_evaluation"] if "conflict_evaluation" in ref_key_advance else ref_key + + item["conflict_evaluation"]["ref_key"] = ref_key_adv + + conflict_evaluations = clear_array_object_by(conflict_evaluations, ref_key_adv) + conflict_evaluations.append(item["conflict_evaluation"]) + data["conflict_evaluations"] = conflict_evaluations + elif "llm_output" in item and item["llm_output"] and "llm_output" in fields: + main_type = "" + description = "" + validates = [] + ref_key_adv = ref_key_advance["conflict_evaluation"] if "conflict_evaluation" in ref_key_advance else ref_key + + + ss = item["llm_output"] + if "main_type" in ss: + main_type = ss["main_type"] + elif "valid_relation_type" in ss: + main_type = ss["valid_relation_type"] + + if "description" in ss: + description = ss["description"] + elif "comments" in ss and len(ss["comments"]): + description = ss["comments"][0] + + if "validates" in ss: + validates = ss["validates"] + else: + validates = get_evaluation_data(ss) + + cd = { + "ref_key": ref_key_adv, + "validates": validates, + "main_type": main_type, + "description": description, + } + + conflict_evaluations = clear_array_object_by(conflict_evaluations, ref_key_adv) + conflict_evaluations.append(cd) + data["conflict_evaluations"] = conflict_evaluations + + if ( + "conflict_final" in item + and item["conflict_final"] + and "conflict_final" in fields + ): + ref_key_adv = ref_key_advance["conflict_final"] if "conflict_final" in ref_key_advance else ref_key + + item["conflict_final"]["ref_key"] = ref_key_adv + + conflict_finals = clear_array_object_by(conflict_finals, ref_key_adv) + conflict_finals.append(item["conflict_final"]) + data["conflict_finals"] = conflict_finals + + if import_state : + data["id"] = _id + data["previous_info"] = _source["previous_info"] + data["next_info"] = _source["next_info"] + data["conflict_tags"] = _source["conflict_tags"] + data["version_key"] = _source["version_key"] + bulk_data_import.append({"_id": _id, "_source": data}) + elif data: + bulk_data.append({"_id": _id, "_source": data}) + + if len(bulk_data_import) > 200: + total_count += len(bulk_data_import) + print("=!" * 10, " import to_elastic , total=" + str(total_count)) + elastic.importBulkList(bulk_data_import, out_put_index_name) + bulk_data_import = [] + + if len(bulk_data) > 200: + total_count += len(bulk_data) + print("=" * 10, "to_elastic , total=" + str(total_count)) + elastic.updateBulkList(bulk_data, out_put_index_name) + bulk_data = [] + # break + + if len(bulk_data_import) > 0: + total_count += len(bulk_data_import) + print("=!" * 10, " import to_elastic , total=" + str(total_count)) + elastic.importBulkList(bulk_data_import, out_put_index_name) + bulk_data_import = [] + + if len(bulk_data) > 0: + total_count += len(bulk_data) + print("=" * 10, "to_elastic , total=" + str(total_count)) + elastic.updateBulkList(bulk_data, out_put_index_name) + bulk_data = [] + + + +def clear_array_object_by(array, ref_key): + array = [c for c in array if c["ref_key"] != ref_key] + + # تغییر کلید و حذف اضافات قبلی + # array = [c for c in array if (c["ref_key"] != "oss120-p2-v2" and c["ref_key"] != "gemma4-p2-v2" )] + + return array + + +def get_evaluation_data(data): + 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 genrate_excel_data(elastic, file_ouput, index_name_o="mj_qa_conflict", key="subject_unitys", key_value="qwen3-6", base_url="http://192.168.23.160"): + + body = { + "query": { + "bool": { + "must": [ + { + "nested": { + "path": key, + "query": { + "term": {key+".ref_key": {"value": key_value}} + } + } + } + ] + } + }, + "sort":[ + {"previous_info.ts_date": "desc"} + ] + } + + print(body) + columns = [ + "ردیف", + "شناسه", + "لینک", + "وحدت موضوع خبره", + "توضیح خبره", + "امتیاز ماشینی", + "وحدت موضوع oss120", + "وحدت موضوع qwen3-6", + "وحدت موضوع gemma4-p2", + "وحدت موضوع oss120-p2", + "وحدت موضوع qwen3-6-p2", + "وحدت موضوع openyourmind", + "تخالف حکمی خبره", + "توضیح خبره", + "امتیاز ماشینی", + "تخالف حکمی oss120", + "تخالف حکمی qwen3-6", + "تخالف حکمی gemma4-p2", + "تخالف حکمی oss120-p2", + "تخالف حکمی qwen3-6-p2", + "تخالف حکمی openyourmind", + "نوع رابطه خبره", + "توضیح خبره", + "امتیاز ماشینی", + "نوع رابطه oss120", + "نوع رابطه qwen3-6", + "نوع رابطه gemma4-p2", + "نوع رابطه oss120-p2", + "نوع رابطه qwen3-6-p2", + "نوع رابطه openyourmind", + "ارزیابی آخر خبره", + "توضیح خبره", + "امتیاز ماشینی", + "ارزیابی آخر oss120", + "ارزیابی آخر qwen3-6", + "ارزیابی آخر gemma4-p2", + "ارزیابی آخر oss120-p2", + "ارزیابی آخر qwen3-6-p2", + "ارزیابی آخر openyourmind", + ] + # Create DataFrame with column names + + try: + list_ = elastic.es_iterate_all_documents(index_name_o, body) + except Exception as e: + print(str(e)) + return + + # data_out = [ + # ["John", 25, "New York"], + # ["Sarah", 30, "London"], + # ["Mike", 28, "Toronto"], + # ["Emma", 35, "Sydney"], + # ] + + data = list(list_) + total = len(data) + count = 0 + total_count = 0 + print(total) + data_out = [] + + for mentry in data: + count += 1 + entry = mentry["source"] + _id = entry.get("id", "") + print(_id) + + next_info = entry.get("next_info", {}) + previous_info = entry.get("previous_info", {}) + + 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_finals = entry.get("conflict_finals", {}) + + + link = f'=HYPERLINK("{base_url}/refine/conflict/{_id}", "لینک")' + + + subject_rate = 0 + subject_oss120 = "" + subject_qwen3_6 = "" + subject_gemma4_p2= "" + subject_oss120_p2= "" + subject_qwen3_6p2= "" + subject_openyourmind= "" + + for su in subject_unitys : + if su["ref_key"] == "oss120" : + subject_oss120 = su["main_type"] + # subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6" : + subject_qwen3_6 = su["main_type"] + subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "gemma4-p2" : + subject_gemma4_p2 = su["main_type"] + subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "oss120-p2" : + subject_oss120_p2 = su["main_type"] + subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6-p2" : + subject_qwen3_6p2 = su["main_type"] + subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "openyourmind" : + subject_openyourmind = su["main_type"] + subject_rate += getRate(su["main_type"]) + + conflict_rate = 0 + conflict_oss120 = "" + conflict_qwen3_6 = "" + conflict_gemma4_p2= "" + conflict_oss120_p2= "" + conflict_qwen3_6p2= "" + conflict_openyourmind= "" + + for su in conflict_detections : + if su["ref_key"] == "oss120" : + conflict_oss120 = str(su["has_conflict"]) + # conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "qwen3-6" : + conflict_qwen3_6 = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "gemma4-p2" : + conflict_gemma4_p2 = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "oss120-p2" : + conflict_oss120_p2 = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "qwen3-6-p2" : + conflict_qwen3_6p2 = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "openyourmind" : + conflict_openyourmind = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + + + relation_rate = 0 + relation_oss120 = "" + relation_qwen3_6 = "" + relation_gemma4_p2= "" + relation_oss120_p2= "" + relation_qwen3_6p2= "" + relation_openyourmind= "" + + for su in conflict_relation_identifications : + if su["ref_key"] == "oss120" : + relation_oss120 = su["main_type"] + # relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6" : + relation_qwen3_6 = su["main_type"] + relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "gemma4-p2" : + relation_gemma4_p2 = su["main_type"] + relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "oss120-p2" : + relation_oss120_p2 = su["main_type"] + relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6-p2" : + relation_qwen3_6p2 = su["main_type"] + relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "openyourmind" : + relation_openyourmind = su["main_type"] + relation_rate += getRate(su["main_type"]) + + + evalution_rate = 0 + evalution_oss120 = "" + evalution_qwen3_6 = "" + evalution_gemma4_p2= "" + evalution_oss120_p2= "" + evalution_qwen3_6p2= "" + evalution_openyourmind= "" + + for su in conflict_evaluations : + if su["ref_key"] == "qwen3-6" : + evalution_oss120 = su["main_type"] + # evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6" : + evalution_qwen3_6 = su["main_type"] + evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "gemma4-p2" : + evalution_gemma4_p2 = su["main_type"] + evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "oss120-p2" : + evalution_oss120_p2 = su["main_type"] + evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6-p2" : + evalution_qwen3_6p2 = su["main_type"] + evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "openyourmind" : + evalution_openyourmind = su["main_type"] + evalution_rate += getRate(su["main_type"]) + + + item = [ + str(count), _id, link , + "", "", subject_rate, subject_oss120, subject_qwen3_6, subject_gemma4_p2, subject_oss120_p2, subject_qwen3_6p2, subject_openyourmind, + "", "", conflict_rate, conflict_oss120, conflict_qwen3_6, conflict_gemma4_p2, conflict_oss120_p2, conflict_qwen3_6p2, conflict_openyourmind, + "", "", relation_rate, relation_oss120, relation_qwen3_6, relation_gemma4_p2, relation_oss120_p2, relation_qwen3_6p2, relation_openyourmind, + "", "", evalution_rate, evalution_oss120, evalution_qwen3_6, evalution_gemma4_p2, evalution_oss120_p2, evalution_qwen3_6p2, evalution_openyourmind, + ] + print(item) + + + data_out.append(item) + + df = pd.DataFrame(data_out, columns=columns) + # Save to Excel + df.to_excel(file_ouput, index=False) + +def getRate(main_type): + if main_type == "yes": + return 1 + elif main_type == "yes_under_assumptions": + return 0.7 + elif main_type == "no": + return 0 + elif main_type == True: + return 1 + else : + return 0 - ## ----------------------------------------------------------------------- load_dotenv(dotenv_path="config.env") -# host = os.getenv("HOST_160") -# password = os.getenv("HOST_160_PASS") +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_112") +# password = os.getenv("HOST_112_PASS") # host = os.getenv("HOST_H102") @@ -124,20 +934,203 @@ path_base_map = "G:/Project2/tavasi_160/src/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) - - +## ---------------------------------------------------- # import_conflict_to_elastic( -# file_path=r"G:\Project2\102_gpu\v3\output_130k_all_qavanin_persian_translate_full.json", +# path=r"G:\Project2\102_gpu\v3\133k_oss120_base\output_130k_all_qavanin_persian_translate_full.json", # out_put_index_name="mj_qa_conflict") - + +## ---------------------------------------------------- # elastic.backupIndexToZipfile("G:/Project2/111/server160", "mj_qa_conflict", "mj_qa_conflict") # elastic.createIndex( 'mj_qa_conflict', 'majles', 'mj_qa_conflict', path_base_map+ 'majles/'+'mj_qa_conflict.json' ) # elastic.restorFileToElastic2('G:/Project2/111/server160/mj_qa_conflict.zip', 'mj_qa_conflict', 'majles', False) -## ----------------------------------------------------------------------- - # .\.env\Scripts\activate - # python -m app.majles.majles_conflict_works.py - - - \ No newline at end of file + +## ---------------------------------------------------- +##---- پر کردن نوع قانون برای مقدم و موخر + conflict_tags +# fill_meta_data(elastic, "mj_qa_conflict", {}) + + +## ---------------------------------------------------- +##---- تغییر ساختار اجزاء کانفلیکت به nested +##---- نیاز به ذخیره آرایه ای و پرکردن ref_key +# elastic.backupIndexToZipfile("G:/Project2/111/server160", "mj_qa_conflict", "mj_qa_conflict") +# elastic.createIndex( 'mj_qa_conflict', 'majles', 'mj_qa_conflict', path_base_map+ 'majles/'+'mj_qa_conflict.json' ) +# restore_correct_data_to_elastic( elastic, 'G:/Project2/111/server160/mj_qa_conflict.zip', 'mj_qa_conflict', "oss120", 'G:/Project2/111/server160/mj_qa_conflict_nested_error.txt' ) + + +## ---------------------------------------------------- +##---- درج نتایج آزمایشی جدید مراحل مختلف کانفلیکت یا ... + +# # #------------------------------تست 5 تایی----------------------------------------------------------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\5_gemini_claude\claude-opus-4-6.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="claude-opus-4-6", +# fields=["llm_output"]) + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\5_gemini_claude\gemini-3.1-pro-preview.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="gemini3-1", +# fields=["llm_output"]) + +# # #--------------------------------------تست 100 تایی-------------------------------------------------- +# #-----100_qwen_3.6 -------------- + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\100_qwen_3.6\finall.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="qwen3-6", +# fields=["subject_unity", "conflict_detection", "conflict_type_detection", "conflict_relation_identification", "conflict_evaluation", "conflict_final"]) + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\100_qwen_3.6\no_conflict_detection.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="qwen3-6", +# fields=["subject_unity", "conflict_detection"]) + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\100_qwen_3.6\no_subject_unity.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="qwen3-6", +# fields=["subject_unity"]) + + +# # #-----100_qwen3_6-p2 -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\100_qwen_3.6-p2\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="qwen3-6-p2") + +# # #-----100_gemma4-p2 -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\100_gemma4-p2\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="gemma4-p2") +# # # #-----100_oss120-p2 -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\100_oss120-p2\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="oss120-p2") + +# # # #-----100_OpenYourMind -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\100_OpenYourMind\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="openyourmind") + + +# #------------------------------------تست 1 تایی با سرویس پولی ----------------------------------------------------------- +# # #-----1 sample_data_Claude 4.6 Sonnet -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\sample_data_Claude 4.6 Sonnet\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="claude4.6") + +# # #-----1 sample_data_DeepSeek V4 Pro -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\sample_data_DeepSeek V4 Pro\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="deepseekv4pro") + +# # # #-----1 sample_data_Gemini 3.1 Pro -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\sample_data_Gemini 3.1 Pro\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="gemini3.1pro") + +# # # #-----1 sample_data_GPT-o4-mini-high -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\sample_data_GPT-o4-mini-high\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="gpt-o4-mini-high") + +# # # #-----1 sample_data_Kimi 2.5 -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\sample_data_Kimi 2.5\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="kimi2.5") + +# # # #-----1 sample_data_Minimax M2 -------------- +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\sample_data_Minimax M2\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="minimax-m2") + + +# # #---------------------------------------------------------------------------------------------- +# #---ردی که قبلا وحدت موضوع در فاز1 و فاز 2 داشتند را با +# #---oss120 پرامپت جدید دوباره انجام دادیم +# #---با این تفاوت که مرحله آخر ارزیابی را با کوئین انجام شد +# # #-----all_qavanin_prompt2 -------------- + +# fields=[ +# "subject_unity", +# "conflict_detection", +# "relation_identification", +# "conflict_relation_type", +# "conflict_evaluation", +# "llm_output", +# ] +# ref_key_advance = { +# "subject_unity": "oss120-v2", +# "conflict_detection": "oss120-v2", +# "relation_identification": "oss120-v2", +# "conflict_relation_type": "oss120-v2", +# "conflict_evaluation": "qwen3-6-v2", +# "llm_output": "qwen3-6-v2", +# } + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\all_qavanin_prompt2", +# out_put_index_name="mj_qa_conflict", +# ref_key="oss120-v2", +# fields = fields, +# ref_key_advance=ref_key_advance, +# import_if_not_found= True, +# ) + + +# #--- ------------------------------------------------------------------------------------------- +# #--- ------------------------------------------------------------------------------------------- +# #--- از مجموع فاز 1 و فاز 2 نسخه 3، تعداد 60 هزارتا قانون اصلی بدون نسخ و بدون آیین نامه و اساسنامه انتخاب شد +# #--- با گما4 وحدت موضوع آنها بررسی شد، با یورمایند تخالفها بررسی شد و با کوئین ارزیانی آخر انجام شد +# #--- gemma_ym_ym_qwen_phase1_2_just_evalution_step_oss_120b -------------- +# #--- version_key = "su-gemma4_cd-ri-urmind_e-qwen3.6_p1p2_eval_oss_valid_sections_only" + +# fields=[ +# "subject_unity", +# "conflict_detection", +# "relation_identification", +# "conflict_relation_type", +# "conflict_evaluation" +# ] +# ref_key_advance = { +# "subject_unity": "gemma4-v3", +# "conflict_detection": "openyourmind-v3", +# "relation_identification": "openyourmind-v3", +# "conflict_relation_type": "openyourmind-v3", +# "conflict_evaluation": "qwen3-6-v3" +# } + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v3\gemma_ym_ym_qwen_phase1_2_just_evalution_step_oss_120b", +# out_put_index_name="mj_qa_conflict", +# ref_key="oss120-v3", +# fields = fields, +# ref_key_advance=ref_key_advance, +# import_if_not_found= True, +# ) + + +# # #---------------------------------------------------------------------------------------------- +# # #---------------------------------------------------------------------------------------------- + +# file_ouput = r"G:\Project2\102_gpu\v3\100_pair_2_tavasi_1.xlsx" +# # genrate_excel_data(elastic, file_ouput, "mj_qa_conflict", "subject_unitys", "qwen3-6", "http://192.168.23.160" ) +# genrate_excel_data(elastic, file_ouput, "mj_qa_conflict", "subject_unitys", "qwen3-6", "https://majles.tavasi.ir" ) + +## ----------------------------------------------------------------------- +# .\.env\Scripts\activate +# python -m app.majles.majles_conflict_works diff --git a/app/majles/majles_plans_import.py b/app/majles/majles_plans_import.py index 8d95617..923313a 100644 --- a/app/majles/majles_plans_import.py +++ b/app/majles/majles_plans_import.py @@ -31,6 +31,9 @@ def recersive_sections( """ title = section.get("title", "").strip() section_list = section.get("section_list", []) + if not section_list : + section_list = section.get("sections", []) + description = section.get("definition", "").strip() tags = section.get("keywords", []) @@ -66,6 +69,12 @@ def recersive_sections( # Process subsections (children) subsections = section.get("subsections", []) + if not subsections : + subsections = section.get("subnodes", []) + if not subsections : + subsections = section.get("sub_title", []) + + if subsections : for idx, subsec in enumerate(subsections, start=1): recersive_sections( @@ -280,17 +289,25 @@ def import_v2_section_list(elastic, all_codes, folder_path, index_name="mj_plan" data_tree = DataHelper.load_orjson(file_path) if data_tree["title"] : code_name = data_tree["title"] + code_name = code_name.replace("قانون جامع", "").strip() - if data_tree["subsections"] : + if "subsections" in data_tree : root_node = data_tree["subsections"] + if "sub_title" in data_tree : + root_node = data_tree["sub_title"] + + if "subnodes" in data_tree : + root_node = data_tree["subnodes"] + + except Exception as e: print(output) continue item_found = [item for item in all_codes if item.get("title") == code_name] if not item_found : - print("* " * 10, " error in code_name ", key, code_name) + print("* " * 10, " error in code_name ", code_name) continue @@ -326,6 +343,7 @@ def import_v2_section_list(elastic, all_codes, folder_path, index_name="mj_plan" return list_codes_imported + load_dotenv(dotenv_path='config.env') host=os.getenv('HOST_160') @@ -350,20 +368,59 @@ with open("./data/codes49.json", "r", encoding="utf8") as f: ## --------------------------------------------------------- -# .\.env\Scripts\activate -# python -m app.majles.majles_plans_import +# #--- برای گلان خبره استفاده شد +# D49_TgV1 + +# #--- D49_oss120 پلان ai - نسخه 1 # import_v1(elastic, all_codes, "D49_oss120") # list_codes_imported=[] -# file_path = r"G:\Project2\102_gpu\v3\plan_dict_def_keyword.json" +# file_path = r"G:\Project2\102_gpu\match_plan\v2_0\plan_dict_def_keyword.json" # import_v2_full_tree(elastic, all_codes, file_path, "mj_plan", list_codes_imported, "D49_oss120_gemma") -folder_path = r"G:\Project2\102_gpu\match_plan\v2" -list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_oss120_gemma_matched") +# folder_path = r"G:\Project2\102_gpu\match_plan\v3.0" +# list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_oss120_gemma_matched") + +# folder_path = r"G:\Project2\102_gpu\match_plan\v3.01" +# list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_oss120_gemma_matched_2") + +# folder_path = r"G:\Project2\102_gpu\match_plan\v3.02" +# list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_oss120_gemma_matched_3") + +# folder_path = r"G:\Project2\102_gpu\match_plan\v4" +# list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_oss120_matched_4") + +# folder_path = r"G:\Project2\102_gpu\match_plan\v4.1" +# list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_oss120_matched_4-1") + +# folder_path = r"G:\Project2\102_gpu\match_plan\v4.2" +# list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_oss120_matched_4-2") + +# folder_path = r"G:\Project2\102_gpu\match_plan\v4.3" +# list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_oss120_matched_4-3") + + +folder_path = r"G:\Project2\102_gpu\match_plan\v5" +list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_gemma4_5") + +folder_path = r"G:\Project2\102_gpu\match_plan\v5.1_gemma" +list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_gemma4_5_1") + +folder_path = r"G:\Project2\102_gpu\match_plan\v5.1_oss" +list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_oss_5_1") + +folder_path = r"G:\Project2\102_gpu\match_plan\v5.1_yourmind" +list_codes_imported = import_v2_section_list(elastic, all_codes, folder_path, "mj_plan", "D49_yourmind_5_1") + +## --------------------------------------------------------- +## --------------------------------------------------------- + +# .\.env\Scripts\activate +# python -m app.majles.majles_plans_import diff --git a/app/monir/monir_teamwork.py b/app/monir/monir_teamwork.py new file mode 100644 index 0000000..49a5cba --- /dev/null +++ b/app/monir/monir_teamwork.py @@ -0,0 +1,519 @@ +import mariadb +from dotenv import load_dotenv +import os +import datetime +import json +import uuid +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 + + + + +def import_from_jahat_entities(elastic, index_name_i, index_name_o): + body={} + try: + list_ = elastic.es_iterate_all_documents(index_name_i, body) + except Exception as e: + save_error(0, e) + + data = list(list_) + total = len(data) + count = 0 + bulk_data = [] + total_count = 0 + + for mentry in data: + count += 1 + entry = mentry["source"] + _id = entry.get("id", "") + type_id = entry.get("type_id", 0) + + + # type_id = 1 # مسائل اولیه + # type_id = 2 # مساله + # type_id = 3 # پاسخ + + # فعلا مساله ها را وارد می کنیم + if not type_id in [1,2] : + continue + + + title = entry.get("title", "") + + files = entry.get("files", []) + product = entry.get("product", "") + description = entry.get("description", "") + text = entry.get("text", "") + + method = entry.get("method", "") + target = entry.get("target", "") + summary = entry.get("summary", "") + links = entry.get("links", []) + + subject = entry.get("subject", []) + # { + # "order": 0, + # "id": 884, + # "title": "نظام سلامت" + # }, + # { + # "order": 0, + # "id": 888, + # "title": "کشاورزی" + # } + + crition = entry.get("crition", []) + # "properties":{ + # "id": { "type": "keyword" }, + # "entity_id": { "type": "keyword" }, + # "entity_title": { "type": "keyword" }, + # "description": { "type": "text" } + # } + + questions = entry.get("questions", []) + # "properties":{ + # "id": { "type": "keyword" }, + # "title": { "type": "text" }, + # } + + notes = entry.get("notes", []) + # "properties":{ + # "id": { "type": "keyword" }, + # "text": { "type": "text" }, + # "resource": { "type": "text" }, + # "description": { "type": "text" }, + # "link": { "type": "text" }, + # "file": { "type": "keyword" } + # } + + necessity = entry.get("necessity", []) + # "properties":{ + # "id": { "type": "keyword" }, + # "description": { "type": "text" }, + # "file": { "type": "keyword" }, + # "title": { + # "type": "text", + # "fields": { + # "keyword": { + # "type": "keyword", + # "ignore_above": 256 + # } + # } + # } + + + + # entities = entry.get("entities", []) + + #--- فیلدهای خالی یا بی ارزش + # html = entry.get("html", "") + # keywords = entry.get("keywords", []) + # tags = entry.get("tags", []) + # critionvalid = entry.get("critionvalid", []) + # validation = entry.get("validation", []) + # products = entry.get("products", []) + # state_id = entry.get("state_id", 0) + # Weight = entry.get("Weight", 0) + # score = entry.get("score", 0) + # date_edit = entry.get("date_edit", "") + + time_edit = entry.get("time_edit", "") + date_create = entry.get("date_create", "") + + user_edit = entry.get("user_edit", 0) + user_create = entry.get("user_create", 0) + user_log = entry.get("user_log", []) + + file_main = {} + files_new = [] + for file in files : + if "file" in file : + file["link"] = file["file"] + del file["file"] + + if not file_main and is_image_file(file["link"]) : + file_main = file + else : + files_new.append(file) + + relations = [] + if crition : + for item in crition : + if not item["entity_title"] : + continue + title = item["entity_title"].replace("* ", "").strip() + new = { + "id": item["entity_id"], + "title": title, + "relation_type": "معیار صحت" + } + if "description" in item : + new["description"] = item["description"] + relations.append(new) + + sections = [] + if notes : + child_order = 0 + for item in notes : + child_order = child_order + 1 + sections.append({ + "id" : _id + "_no" + str(child_order), + "section_type" : "نکات", + "child_order" : child_order, + "title" : item["title"], + "description" : item["description"] + }) + + if necessity : + child_order = 0 + for item in necessity : + child_order = child_order + 1 + sections.append({ + "id" : _id + "_nc" + str(child_order), + "section_type" : "ضرورت", + "child_order" : child_order, + "title" : item["title"], + "file" : item["file"], + "description" : item["description"] + }) + + if description : + sections.append({ + "id" : _id + "_de", + "section_type" : "بیان", + "description" : description + }) + if text : + sections.append({ + "id" : _id + "_tx", + "section_type" : "توضیح", + "description" : text + }) + if product : + sections.append({ + "id" : _id + "_pr", + "section_type" : "محصول", + "description" : product + }) + + if method : + sections.append({ + "id" : _id + "_mt", + "section_type" : "روش", + "description" : method + }) + if target : + sections.append({ + "id" : _id + "_ta", + "section_type" : "هدف", + "description" : target + }) + if summary : + sections.append({ + "id" : _id + "_sm", + "section_type" : "چکیده", + "description" : summary + }) + + + + # subject ?? + data = { + "id" : _id, + "main_type" : "مساله", + "other_type" : "جهادعلمی", + "title" : title, + "file_main" : file_main, + "files" : files_new, + "sections" : sections, + "relations" : relations, + "links" : links, + "user_logs" : user_log, + "user_create" : user_create, + "user_edit" : user_edit, + "time_create" : date_create, + "time_edit" : time_edit, + } + + 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.importBulkList(bulk_data, index_name_o) + 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, index_name_o) + bulk_data = [] + + + + +def import_from_crition_entities(elastic, index_name_i, index_name_o): + body={} + try: + list_ = elastic.es_iterate_all_documents(index_name_i, body) + except Exception as e: + save_error(0, e) + + data = list(list_) + total = len(data) + count = 0 + bulk_data = [] + total_count = 0 + + for mentry in data: + count += 1 + entry = mentry["source"] + _id = entry.get("id", "") + type_id = entry.get("type_id", 4) + type_id = 4 # معیار + + title = entry.get("title", "") + files = entry.get("files", []) + if isinstance(files, dict) : + files = [files] + + unfavorite = entry.get("unfavorite", "") + if not isinstance(unfavorite, list) : + unfavorite = unfavorite.split("•") + + favorite = entry.get("favorite", "") + if not isinstance(favorite, list) : + favorite = favorite.split("•") + + text = entry.get("text", "") + description = entry.get("description", "") + summary = entry.get("summary", "") + + result = entry.get("result", {}) + # { + # "type": "پیامدهای ساختاری-نهادی در هر منطقه", + # "title": "ساختارهای توسعه حضور آزادانه، آگاهانه، مسئولانه و احسان و ایثارپایۀ مردم، با محوریت علماء دینی، و ارتباط صمیمانه و محبت‌آمیز با حاکمیت در مدیریت تحقق پیشرفت و سبک زندگی اسلامی‌ـ‌ایرانی شکل ‌گرفته و نهادینه شده است (چرخه حضور)." + # } + + links = entry.get("links", []) + + + fish = entry.get("fish", []) + # { + # "text": "کار جهادی باید هدفمند، درست متوجه به آرمانها و هوشمندانه و عاقلانه و دشمن‌شکن باشد. یعنی به همان معنائی که ما مبارزه ‌را در اصطلاح معمولی به کار میبریم: «دارم مبارزه میکنم؛ این یک مبارزه است.» این، یک تعبیر مصطلح است. در جهاد، این معنا ‌هست.", + # "resource": "بیانات در بازدید از پژوهشکده رویان", + # "description": "", + # "date_text": "25/04/1386" + # } + + subject = entry.get("subject", []) + # { + # "order": 0, + # "id": 884, + # "title": "نظام سلامت" + # }, + # { + # "order": 0, + # "id": 888, + # "title": "کشاورزی" + # } + + + + # entities = entry.get("entities", []) + + #--- فیلدهای خالی یا بی ارزش + # html = entry.get("html", "") + # keywords = entry.get("keywords", []) + # tags = entry.get("tags", []) + # date_edit = entry.get("date_edit", "") + + time_edit = entry.get("time_edit", "") + date_create = entry.get("date_create", "") + user_edit = entry.get("user_edit", 0) + user_create = entry.get("user_create", 0) + user_log = entry.get("user_log", []) + + + file_main = {} + files_new = [] + for file in files : + if "file" in file : + file["link"] = file["file"] + del file["file"] + if not file_main and is_image_file(file["link"]) : + file_main = file + else : + files_new.append(file) + + relations = [] + sections = [] + title_new = "" + + if favorite : + child_order = 0 + for item in favorite : + item = item.replace("•", "").strip(" *•") + if not item : + continue + if not title_new : + title_new = item + child_order = child_order + 1 + sections.append({ + "id" : _id + "_fa" + str(child_order), + "section_type" : "مطلوب", + "child_order" : child_order, + "title" : item, + }) + + + if unfavorite : + child_order = 0 + for item in unfavorite : + item = item.replace("•", "").strip(" *•") + if not item : + continue + if not title_new : + title_new = item + child_order = child_order + 1 + sections.append({ + "id" : _id + "_uf" + str(child_order), + "section_type" : "نامطلوب", + "child_order" : child_order, + "title" : item, + }) + dict_title = {} + if fish : + child_order = 0 + for item in fish : + child_order = child_order + 1 + title = "" + if "resource" in item and item["resource"]: + title += item["resource"] + if "date_text" in item and item["date_text"]: + title += " " + item["date_text"] + title = title.strip() + + dict_title[title] = 1 + + desc = "" + if "text" in item and item["text"]: + desc += item["text"] + if "description" in item and item["description"]: + desc += "\n" + item["description"] + + + sections.append({ + "id" : _id + "_fs" + str(child_order), + "section_type" : "منبع", + "child_order" : child_order, + "title" : title, + "description" : desc, + }) + + + if description : + sections.append({ + "id" : _id + "_de", + "section_type" : "بیان", + "description" : description + }) + if text : + sections.append({ + "id" : _id + "_tx", + "section_type" : "توضیح", + "description" : text + }) + + if summary : + sections.append({ + "id" : _id + "_sm", + "section_type" : "چکیده", + "description" : summary + }) + + + # اصلاح عنوان خودکار قبلی + if title in dict_title and title_new : + title = title_new + + + # subject ?? + data = { + "id" : _id, + "main_type" : "معیار", + "other_type" : "جهادعلمی", + "title" : title, + "files" : files_new, + "file_main" : file_main, + "sections" : sections, + "relations" : relations, + "links" : links, + "user_logs" : user_log, + "user_create" : user_create, + "user_edit" : user_edit, + "time_create" : date_create, + "time_edit" : time_edit, + } + + 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.importBulkList(bulk_data, index_name_o, True) + 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, index_name_o) + bulk_data = [] + + + + +load_dotenv(dotenv_path="config.env") + +host = os.getenv("HOST_60") +password = os.getenv("HOST_60_PASS") + +# host = os.getenv("HOST_133") +# password = os.getenv("HOST_133_PASS") + +# print(host) +# print(password) + +path_base_map = "G:/Project2/tavasi_160/src/repo/_other/" + +# path_base_map = ( +# "G:\Project2\tavasi_60\src\repo\_other/" +# ) +elastic = ElasticHelper(host, password, "elastic", path_base_map) +text_normalize = TextNormalizer() + + +elastic.createIndex( 'gn_teamwork', 'general', 'gn_teamwork' ) + +import_from_jahat_entities(elastic, "mn_jahat", "gn_teamwork") + +import_from_crition_entities(elastic, "mn_crition", "gn_teamwork") + + + + +# .\.env\Scripts\activate +# python -m app.monir.monir_teamwork