diff --git a/app/common/qanon_functions.py b/app/common/qanon_functions.py index ad8f648..4fff67b 100644 --- a/app/common/qanon_functions.py +++ b/app/common/qanon_functions.py @@ -4,7 +4,7 @@ import traceback import json from dotenv import load_dotenv from app.common.config_base import * -# from app.common.general_functions import * +from app.common.general_functions import * from app.common.text_normalizer import TextNormalizer from app.common.elastic_helper import ElasticHelper # from excel_helper import ExcelHelper @@ -205,6 +205,13 @@ def get_dict_qanon_title(file_path): def searchQanonTitleOfContent(elastic, qanon_title, ts_date="", index_name="mj_qa_section"): score = 100 + ts_date_f = "" + try : + ts_date_f = jdate2Gregorian(ts_date) + except : + ts_date_f = "" + + body = { "query": { @@ -254,12 +261,12 @@ def searchQanonTitleOfContent(elastic, qanon_title, ts_date="", index_name="mj_q if len(list_equal) == 1 : item = list_equal[0] score = 90 - if ts_date == ts_date_ref : + if ts_date == ts_date_ref or ts_date_f == ts_date_ref : score = 100 return item['_source']["qanon_id"], score, item for item in list_equal : - if ts_date == ts_date_ref : + if ts_date == ts_date_ref or ts_date_f == ts_date_ref : score = 100 return item['_source']["qanon_id"], score, item @@ -288,6 +295,13 @@ def searchQanonId(elastic, qanon_title, ts_date="", index_name="mj_qa_qanon", ts } } } + + ts_date_f = "" + try : + ts_date_f = jdate2Gregorian(ts_date) + except : + ts_date_f = "" + s_res = elastic.es.search(index=index_name, body=body) total = s_res["hits"]["total"]['value'] if total == 1 : @@ -338,12 +352,12 @@ def searchQanonId(elastic, qanon_title, ts_date="", index_name="mj_qa_qanon", ts if len(list_equal) == 1 : item = list_equal[0] score = 90 - if ts_date == ts_date_ref : + if ts_date == ts_date_ref or ts_date_f == ts_date_ref: score = 100 return item["_id"], score, item for item in list_equal : - if ts_date and ts_date == ts_date_ref : + if ts_date and ts_date == ts_date_ref or ts_date_f == ts_date_ref : score = 100 return item["_id"], score, item diff --git a/app/for_ai/codes_import_from_excel.py b/app/for_ai/codes_import_from_excel.py index 5cb90af..755d59f 100644 --- a/app/for_ai/codes_import_from_excel.py +++ b/app/for_ai/codes_import_from_excel.py @@ -591,7 +591,8 @@ def importCodesToElastic(elastic, file_excel, index_name_o, tcode_label, file_lo file.close() -def extract_codes_to_json( file_excel, tcode_label, file_json, col_index, head_top=False, with_history_code={}): + +def extract_codes_to_json( file_excel, tcode_label, file_json, col_index, head_top=False, with_history_code={}, jump_ids = []): if head_top : @@ -618,6 +619,7 @@ def extract_codes_to_json( file_excel, tcode_label, file_json, col_index, head_t bulk_data = [] total_update=0 row_number = 0 + correct_rows = [] try: for row in rows_list: @@ -649,6 +651,9 @@ def extract_codes_to_json( file_excel, tcode_label, file_json, col_index, head_t data={} # ذخیره کد برای شناسه های یافت شده if section_id and section_id != 0: + + if jump_ids and section_id in jump_ids : + continue _source = {} history_codes= [] @@ -674,6 +679,8 @@ def extract_codes_to_json( file_excel, tcode_label, file_json, col_index, head_t '_source' : _source } + correct_rows.append(row_number) + bulk_data.append(data) @@ -683,6 +690,7 @@ def extract_codes_to_json( file_excel, tcode_label, file_json, col_index, head_t except Exception as e: print('######################## Exception error --- row_number : ' , str(row_number), '/', str(count) , ' ***' , str(e) ) print(str(traceback.print_exc()) ) + return correct_rows #/////---- ردیف های اکسل را در سامانه قانون یار شناسایی می کند و شناسه ها را در همان اکسل تولید میکند #//// البته با تنظیم فلگ مرتبط، امکان درج در الاستیک هم دارد @@ -757,7 +765,7 @@ def run_Etebar_directory(elastic, directory, output, all_codes, update_elastic = print('00000 error 000000 ', code_name) -def run_codes_extract_directory(directory, output, all_codes, col_index={}, head_top=False, version_key=''): +def run_codes_extract_directory(directory, output, all_codes, col_index={}, head_top=False, version_key='', by_repair_error = False, elastic= None): # List all files in the specified directory files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))] @@ -791,7 +799,17 @@ def run_codes_extract_directory(directory, output, all_codes, col_index={}, head item_found = [item for item in all_codes if item.get("title") == code_name] if item_found : - extract_codes_to_json(file_path, code_name, output + f"/{code_name}.json", col_index, head_top, with_history_code) + file_json = output + f"/{code_name}.json" + correct_rows = [] + if by_repair_error : + #-- در مواقعی که قبلا خطا خورد و فایل ارسال شده ، ولی نباید خطا می خورد + #-- و با کد جدید مشکل خطا حل شده + #-- موارد فوق استخراج و دوباره ارسال شود + file_json = output + f"/{code_name}_err.json" + correct_rows = extract_codes_to_json(file_path, code_name, file_json, col_index, head_top, with_history_code) + parseCodesToExcel(elastic, file_path, "mj_qa_section", code_name, log_file, "", col_index, head_top) + + extract_codes_to_json(file_path, code_name, output + f"/{code_name}.json", col_index, head_top, with_history_code, correct_rows) else : print('00000 error 000000 ', code_name) diff --git a/app/for_ai/majles_codes_update_2_steps.py b/app/for_ai/majles_codes_update_2_steps.py index bf16691..d7edaec 100644 --- a/app/for_ai/majles_codes_update_2_steps.py +++ b/app/for_ai/majles_codes_update_2_steps.py @@ -18,12 +18,12 @@ from app.for_ai.codes_import_from_excel import ( run_codes_extract_directory, run_codes_to_elastic_directory, ) -from app.parvande_qanon.import_qavanin_excels_to_elastic import ( +from app.parvande_qanon.s2_import_qavanin_excels_to_elastic import ( import_excel_before_to_elastic, import_excel_after_to_elastic, import_excel_tasmimat_to_elastic, ) -from app.parvande_qanon.repair_qa_qanon_for_map_new import * +from app.majles.repair_qa_qanon_for_map_new import * text_normal = TextNormalizer() @@ -134,3 +134,8 @@ col_index = { ### --- درج در الاستیک نمی کند update_elastic = False # run_codes_to_elastic_directory( elastic, "./data/tcodes/corrected_majles/not_edit4", "./data/tcodes/corrected_majles/not_edit4/result", all_codes, False, True, col_index, False) # run_codes_extract_directory("./data/tcodes/corrected_majles/not_edit4/result","./data/tcodes/corrected_majles/not_edit4/result_json",all_codes,col_index,True) + +##--- استخراج موارد خطای کد +run_codes_extract_directory("./data/tcodes/corrected_majles/not_edit4/result","./data/tcodes/corrected_majles/not_edit4/result_json",all_codes,col_index, head_top=False, by_repair_error = True, elastic=elastic) + + diff --git a/app/majles/import_context_rules_v2.py b/app/majles/import_context_rules_v2.py index 2d2b937..f453214 100644 --- a/app/majles/import_context_rules_v2.py +++ b/app/majles/import_context_rules_v2.py @@ -129,7 +129,7 @@ def extract_json_context_id_rules_labels( print(f"failed -=>{failed}<=-") -def extract_json_files(elastic, file_json, file_import_ids, file_type="groups", version_key="v2"): +def extract_groups_of_json_files(elastic, file_json, file_import_ids, file_type="groups", version_key="v2"): bulk_data = [] total_count = 0 @@ -161,6 +161,8 @@ def extract_json_files(elastic, file_json, file_import_ids, file_type="groups", version_key ) jump_ids.append(qanon_id) + + except Exception as e: print("error in file " + qanon_id + str(e)) @@ -791,7 +793,7 @@ def import_rules_qanon(elastic, qanon_id, data, bulk_data, total_count, index_na log_file.write(5 * "*" + error + "\n") continue - if "qanon_etebar" in source: + if "qanon_title" in source: qanon_title = source["qanon_title"] if "qanon_etebar" in source: qanon_etebar = source["qanon_etebar"] @@ -842,6 +844,93 @@ def import_rules_qanon(elastic, qanon_id, data, bulk_data, total_count, index_na return bulk_data, total_count +def import_rules_of_json(elastic, file, index_name_o, version_key="v2"): + + + + bulk_data = [] + total_count = 0 + + input_data = DataHelper.load_orjson(file) + index_name_i = "mj_qa_context" + + for item in input_data: + + context_id = item["groups_id"] + qanon_id = item["qanon_id"] + rule_order = 0 + rule_id = item["id"] + rule_order = int(rule_id.split('-')[1]) + 1 + content = item["content"] + rule_type = item["main_type"] + + + qanon_title = "" + qanon_etebar = "" + state_etebar = "" + ts_date = None + ts_ref = "" + group_section_ids = [] + + source = None + try: + res = elastic.get_document(index_name_i, context_id) + source = res["_source"] + except: + error = ( + " section_mom_id not found in qa_context :" + + qanon_id + + ":" + + context_id + ) + if log_file: + log_file.write(5 * "*" + error + "\n") + continue + + if "qanon_title" in source: + qanon_title = source["qanon_title"] + if "qanon_etebar" in source: + qanon_etebar = source["qanon_etebar"] + if "ts_date" in source: + ts_date = source["ts_date"] + if "ts_ref" in source: + ts_ref = source["ts_ref"] + if "state_etebar" in source: + state_etebar = source["state_etebar"] + + if "group_section_ids" in source: + group_section_ids = source["group_section_ids"] + + + eid = rule_id + data = { + "id": eid, + "context_id": context_id, + "version_key": "base_"+version_key, + "qanon_id": qanon_id, + "qanon_title": qanon_title, + "qanon_etebar": qanon_etebar, + "state_etebar": state_etebar, + "ts_date": ts_date, + "ts_ref": ts_ref, + "rule_order": rule_order, + "rule_type": rule_type, + "content": content, + "group_section_ids": group_section_ids, + "time_create": int(round(time.time() * 1000)), + } + bulk_data.append({"_id": eid, "_source": data}) + + if len(bulk_data) >= 100: + total_count += len(bulk_data) + print( + "=" * 10, "to_elastic , total=" + str(total_count), ", id=" + eid + ) + elastic.importBulkList(bulk_data, index_name_o) + bulk_data = [] + + return bulk_data, total_count + if __name__ == "__main__": @@ -854,7 +943,7 @@ if __name__ == "__main__": # log_file = open(f"G:/Project2/102_gpu/v2/log_{data_key}_1.txt", mode="w", encoding="utf8") # file = f"G:/Project2/102_gpu/v2/mj_qa_chunks.json" # file_import_ids = f"G:/Project2/102_gpu/v2/{data_key}_import_ids.txt" - # extract_json_files(elastic, file, file_import_ids, data_key, "v2") + # extract_groups_of_json_files(elastic, file, file_import_ids, data_key, "v2") # log_file.close() # ### ---------------------------------------------- @@ -874,17 +963,50 @@ if __name__ == "__main__": # file_import_ids = f"G:/Project2/102_gpu/{data_key}_import_ids.txt" # extract_zip_files(elastic, filezip, file_import_ids, data_key, "v2") # log_file.close() + + + ### -------- repai error 1 groups-------------------------------------- + ### ------------------------------------------- + # در گروه بندی که با nlp_child + # بود ، ترتیب بچه درست لحاظ نشده بود و باعث خطا در محتوای گروههای چند سکشنی شد + # لذا بعد از شناسایی و اصلاح موارد مذکور جدید اضافه شد و قبلی ها پاک شد + + # data_key = "groups" + # log_file = open(f"G:/Project2/102_gpu/v2/error_log_{data_key}_1.txt", mode="w", encoding="utf8") + # file = f"G:/Project2/102_gpu/v2/f1_valid_5099_mj_qa_chunked_ids_28_11_1404.json" + # file_import_ids = f"G:/Project2/102_gpu/v2/error1_{data_key}_import_ids.txt" + + + # import_error_groups_json(elastic, file, file_import_ids, data_key, "v2") + # log_file.close() + # fill_error_context_json(elastic, file, file_import_ids, data_key, "v2") + # log_file.close() + + + # #--- حذف موارد خطای قبل + # file_json = f"G:/Project2/102_gpu/v2/error1_delete_ids.json" + # data_json = DataHelper.load_orjson(file_json) + # body={ + # "query":{ + # "ids": {"values": data_json } + # } + # } + # elastic.deleteByQueryIndex("mj_qa_context", body) + + # ### ---------------------------------------------- # data_key = "rules" # log_file = open( - # f"G:/Project2/102_gpu/log_{data_key}_1.txt", mode="w", encoding="utf8" + # f"G:/Project2/102_gpu/v2/log_{data_key}_1.txt", mode="w", encoding="utf8" # ) - # filezip = f"G:/Project2/102_gpu/{data_key}.zip" - # file_import_ids = f"G:/Project2/102_gpu/{data_key}_import_ids.txt" - # extract_zip_files(elastic, filezip, file_import_ids, data_key) + # file = f"G:/Project2/102_gpu/v2/rules_v2_433252.json" + # file_import_ids = f"G:/Project2/102_gpu/v2/{data_key}_import_ids.txt" + # import_rules_of_json(elastic, file, "mj_qa_rule", "v2") # log_file.close() + + # ### ---------------------------------------------- # data_key = "rules_labels" # # label_object @@ -907,27 +1029,5 @@ if __name__ == "__main__": # ### ---------------------------------------------- - ### -------- repai error 1 groups-------------------------------------- - ### ------------------------------------------- - # در گروه بندی که با nlp_child - # بود ، ترتیب بچه درست لحاظ نشده بود و باعث خطا در محتوای گروههای چند سکشنی شد - # لذا بعد از شناسایی و اصلاح موارد مذکور جدید اضافه شد و قبلی ها پاک شد - - # data_key = "groups" - # log_file = open(f"G:/Project2/102_gpu/v2/error_log_{data_key}_1.txt", mode="w", encoding="utf8") - # file = f"G:/Project2/102_gpu/v2/f1_valid_5099_mj_qa_chunked_ids_28_11_1404.json" - # file_import_ids = f"G:/Project2/102_gpu/v2/error1_{data_key}_import_ids.txt" - - - # import_error_groups_json(elastic, file, file_import_ids, data_key, "v2") - # log_file.close() - - # fill_error_context_json(elastic, file, file_import_ids, data_key, "v2") - # log_file.close() - - - ### ------------------------------------------- - - # python -m dataParsers.app.import_context_rules # print(":D")