diff --git a/create_ai_index.py b/ai_create_index.py similarity index 100% rename from create_ai_index.py rename to ai_create_index.py diff --git a/ai_json_move_to_index.py b/ai_json_move_to_index.py index 366c94a..fe2f234 100644 --- a/ai_json_move_to_index.py +++ b/ai_json_move_to_index.py @@ -161,8 +161,8 @@ path_base_map = "G:/Project2/gateway2_main/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) -#moveKeywords(elastic,"mj_qa_section", "data/sections_kw_llama8b_170k_concatenated.json") -#moveKeywords(elastic,"mj_qa_section", "data/sections_kw_llama8b_40k_concatenated.json") +moveKeywords(elastic,"mj_qa_section", "data/sections_kw_llama8b_170k_concatenated.json") +moveKeywords(elastic,"mj_qa_section", "data/sections_kw_llama8b_40k_concatenated.json") moveZipSimplifies(elastic,"mj_qa_section", "G:\Work\_majles\_data\simpilified_final.zip") diff --git a/all_sql_steps.py b/all_sql_steps.py index 92876b9..094e48f 100644 --- a/all_sql_steps.py +++ b/all_sql_steps.py @@ -29,7 +29,7 @@ from s11_Tanghih_plan_move import * # DELETE mj_qa_opinion_temp # ----------------------------------------------------------------- -cursor, cnxn = create_cursor() +# cursor, cnxn = create_cursor() ## ایجاد فهرست موضوعات قانون #projectid = insertProejct() @@ -39,20 +39,20 @@ cursor, cnxn = create_cursor() #import_law_section_to_elastic(cursor) -import_regulation_section_to_elastic(cursor) +# import_regulation_section_to_elastic(cursor) -import_opinion_to_elastic(cursor) +# import_opinion_to_elastic(cursor) ### از ابتدا تا اینجا حدودا 24 ساعت کشید -import_law_section_update_ref_state(cursor) +# import_law_section_update_ref_state(cursor) -import_regulation_section_update_ref_state(cursor) +# import_regulation_section_update_ref_state(cursor) -import_law_reg_qanon_to_elastic(cursor) +# import_law_reg_qanon_to_elastic(cursor) # ## تا اینجا رسید و خطا داد # import_relations_qanon_lwreg(cursor) @@ -63,13 +63,13 @@ import_law_reg_qanon_to_elastic(cursor) # import_lawreg_lwphase_info(cursor) -cursor.close() -cnxn.close() - - -# cursor, cnxn = create_cursor('db4minaei') -# #####import_tangih_tree_to_list(cursor) -# import_tanghih_plan_codes(cursor) - # cursor.close() # cnxn.close() + + +cursor, cnxn = create_cursor('db4minaei') +# #####import_tangih_tree_to_list(cursor) +import_tanghih_plan_codes(cursor) + +cursor.close() +cnxn.close() diff --git a/config_base.py b/config_base.py index 1eac6f2..01f2032 100644 --- a/config_base.py +++ b/config_base.py @@ -7,7 +7,8 @@ import pyodbc # TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3Mzk3ODc1NTQsImp0aSI6IlpKRnhHUjFPR2p6b2RqWGxoVzJpSzFSeks5VVNwdXE0IiwiaXNzIjpudWxsLCJleHAiOjE3NDEwODc1NTMsImF1ZCI6bnVsbCwiZGF0YSI6eyJpZCI6MSwiZmlyc3RfbmFtZSI6Ilx1MDY0NVx1MDYyZlx1MDZjY1x1MDYzMSBcdTA2NDFcdTA2NDZcdTA2Y2MiLCJsYXN0X25hbWUiOiJcdTA2MzNcdTA2MjdcdTA2NDVcdTA2MjdcdTA2NDZcdTA2NDciLCJlbWFpbCI6ImRldkBnbWFpbC5jb20iLCJ1c2VybmFtZSI6ImRldiIsInVzZXJfbGV2ZWwiOjJ9fQ.orS-RbJkYY2nT6r7WKDfL2Wy2M0rJ9Gh_RpZv5kvVvU' # TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3NDM4NTQ0MTEsImp0aSI6IjhDOWErR3BPMGk5U3Blc0ZFT251RDNoM2t3XC82R1BWTiIsImlzcyI6bnVsbCwiZXhwIjoxNzQ1MTU0NDEwLCJhdWQiOm51bGwsImRhdGEiOnsiaWQiOjEsImZpcnN0X25hbWUiOiJcdTA2NDVcdTA2MmZcdTA2Y2NcdTA2MzEgXHUwNjQxXHUwNjQ2XHUwNmNjIiwibGFzdF9uYW1lIjoiXHUwNjMzXHUwNjI3XHUwNjQ1XHUwNjI3XHUwNjQ2XHUwNjQ3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kygk' +TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3NDY2MDA0NjUsImp0aSI6Im9IRjJiRW81ZzhXZm9UdGdrb1BZWmpTb3Arem84N3llIiwiaXNzIjpudWxsLCJleHAiOjE3NDc5MDA0NjQsImF1ZCI6bnVsbCwiZGF0YSI6eyJpZCI6MiwiZmlyc3RfbmFtZSI6Ilx1MDYyZFx1MDY0NVx1MDZjY1x1MDYyZiIsImxhc3RfbmFtZSI6Ilx1MDYzNVx1MDY0MVx1MDYyN1x1MDZjY1x1MDZjYyIsImVtYWlsIjoic2FmYWVpLmhhbWlkQGdtYWlsLmNvbSIsInVzZXJuYW1lIjoiaHNhZmFlaSIsInVzZXJfbGV2ZWwiOjJ9fQ.rKb7fD7hnszRSK2720hvnTE28AFceBRWZIN1H1fC0sw' ACCEPT = "application/json" HEADERS = {"Authorization": TOKEN, "Accept": ACCEPT} diff --git a/r1_LawSection_repair1.py b/r1_LawSection_repair1.py index a5429b9..fc1cdfe 100644 --- a/r1_LawSection_repair1.py +++ b/r1_LawSection_repair1.py @@ -33,6 +33,7 @@ def extract_all_effecteds(elastic, index_name, body={}): count = 0 text_normal = TextNormalizer() result = {} + result_new = {} for mentry in list: entry = mentry["source"] @@ -42,7 +43,10 @@ def extract_all_effecteds(elastic, index_name, body={}): to_section_id = effect["to_section_id"] effect_type = effect["effect_type"] result[to_section_id] = effect_type - return result + if effect_type == 'الحاقی' or effect_type =='یتفسیر' : + id = effect["id"] + result_new[id] = to_section_id + return result, result_new def repair_law_section_text(cursor, elastic, index_name, dict_changed, file_log) : @@ -51,6 +55,7 @@ def repair_law_section_text(cursor, elastic, index_name, dict_changed, file_log) select s.ID as sectionId ,isnull(s.SECTIONTEXT,'') as sectionText -- متن ماده یا تبصره + ,isnull(l.APPROVEDATE,'') as aproveDate -- تاریخ تصویب from lwSection s left join lwLaw l on s.F_LWLAWID = l.ID where l.ISLAW = 1 @@ -99,22 +104,39 @@ def repair_law_section_text(cursor, elastic, index_name, dict_changed, file_log) content_len = text.__len__() eid='qs'+ str(row.sectionId) + ts_year = '' + if row.aproveDate : + items = row.aproveDate.split("/") + if items.__len__() > 0: + ts_year = items[0] + + data={} if eid in dict_changed : print( ' ------------------> chenged effected : ', dict_changed[eid]) file.write(' ------------------> chenged effected : '+ dict_changed[eid] + '\n') if dict_changed[eid] == 'اصلاحی' or dict_changed[eid] == 'جایگزین': - continue + # اصلاح تاریخ تصویب که بخاطر خطای سامانه ایجاد شده است + data={ + '_id':eid, + '_source' : { + 'ts_date': row.aproveDate, + 'ts_year': ts_year, + } + } + + + #word_len = len(split_words(text)) - - data={ - '_id':eid, - '_source' : { - 'content': text, - 'content_len': content_len, + if not data : + data={ + '_id':eid, + '_source' : { + 'content': text, + 'content_len': content_len, + } } - } item = {} try: @@ -146,6 +168,197 @@ def repair_law_section_text(cursor, elastic, index_name, dict_changed, file_log) + if len(bulk_data) > 0: + total_count += len(bulk_data) + print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid ) + elastic.updateBulkList(bulk_data, index_name) + + file.close() + print('end success import_law_section_to_elastic') + t2= datetime.datetime.now() + print(t2-t1) + +def repair_error_new_section_ts_date( elastic, index_name, dict_changed, file_log) : + + total_count = 0 + bulk_data = [] + + t1= datetime.datetime.now() + print(t1) + + file = open(file_log, 'a+', encoding='utf8') + i = 0 + for key in dict_changed.keys(): + # باعث خطای کوئری میشد و نیاز نبود + # print(i) + i=i+1 + + eid=key + + eid_prev = dict_changed[key] + ts_year = '' + item = {} + try: + item = elastic.get_doctument(index_name, eid_prev) + ts_date = item['_source']['ts_date'] + ts_year = item['_source']['ts_year'] + sort_date_timestamp = item['_source']['sort_date_timestamp'] + # در اینصورت محتوای ماده توسط مجلس تغییر یافته است + + except Exception as e: + continue + + data={ + '_id':eid, + '_source' : { + 'ts_date': ts_date, + 'ts_year': ts_year, + 'sort_date_timestamp': sort_date_timestamp, # created from repairQanon.getSortTimestamp + } + } + + if len(bulk_data) >= 10: + total_count += len(bulk_data) + print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid) + elastic.updateBulkList(bulk_data, index_name) + bulk_data = [] + + if data : + bulk_data.append(data) + + + + if len(bulk_data) > 0: + total_count += len(bulk_data) + print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid ) + elastic.updateBulkList(bulk_data, index_name) + + file.close() + print('end success import_law_section_to_elastic') + t2= datetime.datetime.now() + print(t2-t1) + + +def repair_error_section_ts_date(cursor, elastic, index_name, dict_changed, file_log) : + ids = '' + for key in dict_changed.keys(): + # باعث خطای کوئری میشد و نیاز نبود + + if '_' in key : + continue + + key = key.replace('qs', '') + # if key == 'qs1414840_2512221' or key == 'qs1414840_2511' or key=='qs67f53e80b9949_008' : + # continue + # number = False + # try : + # number = int(key) + # except : + # number = False + + # if not number : + # continue + + + if ids != '' : + ids = ids + ',' + + ids = ids + key[2:] + + + query = f''' + select + s.ID as sectionId + ,isnull(s.SECTIONTEXT,'') as sectionText -- متن ماده یا تبصره + ,isnull(l.APPROVEDATE,'') as aproveDate -- تاریخ تصویب + from lwSection s + left join lwLaw l on s.F_LWLAWID = l.ID + where l.ISLAW = 1 and s.ID in ({ids}) + order by s.F_LWLAWID,textOrder + ''' + + print(query) + + cursor.execute(query) + text_normal = TextNormalizer() + + total_count = 0 + bulk_data = [] + + t1= datetime.datetime.now() + print(t1) + + file = open(file_log, 'a+', encoding='utf8') + i = 0 + while 1: + # print(i) + i=i+1 + row = cursor.fetchone() + if not row: + break + + # if i > 1000: + # break + # print(row.sectionId) + + try: + soup = BeautifulSoup(row.sectionText, "html.parser") + text = soup.get_text("\n", strip=True) + # text = removeHtmlNoTableTag(row.sectionText) # خطا داره + except Exception as e: + text = row.sectionText + + + text = text_normal.normalize_qanon(text) + text2 = text_normal.normalize_qanon(text,True) + + # در این صورت مشکل خطای حذف خط تیره کشیده در این متن نبوده و نیازه به بروز رسانی متن نیست + if len(text) == len(text2) : + if text != text2 : + print(' ??????????> changed content : ', eid, "\ntext:", text , "\ntext2:", text2) + continue + + content_len = text.__len__() + + eid='qs'+ str(row.sectionId) + ts_year = '' + if row.aproveDate : + items = row.aproveDate.split("/") + if items.__len__() > 0: + ts_year = items[0] + + sort_date_timestamp = -15000000000 # 9/2/1494, 12:45:44 AM # خطا یا خالی + try: + sort_date_timestamp = getSortTimestamp(row.aproveDate) + except Exception as e: + sort_date_timestamp = -15000000000 # 9/2/1494, 12:45:44 AM # خطا یا خالی + + + + data={} + if eid in dict_changed : + # if dict_changed[eid] == 'اصلاحی' or dict_changed[eid] == 'جایگزین': + # اصلاح تاریخ تصویب که بخاطر خطای سامانه ایجاد شده است + data={ + '_id':eid, + '_source' : { + 'ts_date': row.aproveDate, + 'ts_year': ts_year, + 'sort_date_timestamp': sort_date_timestamp, # created from repairQanon.getSortTimestamp + } + } + + if len(bulk_data) >= 10: + total_count += len(bulk_data) + print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid) + elastic.updateBulkList(bulk_data, index_name) + bulk_data = [] + + if data : + bulk_data.append(data) + + + if len(bulk_data) > 0: total_count += len(bulk_data) print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid ) @@ -354,7 +567,7 @@ body={ "exists": {"field": "effects"} } } -dict_effected_sections = extract_all_effecteds(elastic, "mj_df_qanon", body) +dict_effected_sections, dict_effected_new_sections = extract_all_effecteds(elastic, "mj_df_qanon", body) # print(dict_effected_sections) # try: @@ -364,6 +577,13 @@ dict_effected_sections = extract_all_effecteds(elastic, "mj_df_qanon", body) # print(type(e).__name__) # print(str(e)) -#repair_law_section_text(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r1.txt") +###### step 1 repair text - ########################### +repair_law_section_text(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r1.txt") -repair_law_section_ref_state(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r2.txt") \ No newline at end of file +###### step 2 : repair date ############# +repair_error_section_ts_date(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r1.txt") +repair_error_new_section_ts_date( elastic, "mj_qa_section", dict_effected_new_sections, "./output/log_r1.txt") + + +###### step 3 : not nead ############# +#repair_law_section_ref_state(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r2.txt") \ No newline at end of file diff --git a/s11_Tanghih_plan_move.py b/s11_Tanghih_plan_move.py index 3c15979..a25afd3 100644 --- a/s11_Tanghih_plan_move.py +++ b/s11_Tanghih_plan_move.py @@ -130,12 +130,22 @@ def import_tanghih_plan_codes(cursor): eid='qs'+ str(prev_sectionid) + # data={ + # 'id' : eid, + # 'tcode_main' : tcode_main, + # 'tcode_sub' : tcode_sub, + # 'tcode_main_old' : tcode_main_old, + # 'tcode_sub_old' : tcode_sub_old, + # } + data={ 'id' : eid, - 'tcode_main' : tcode_main, - 'tcode_sub' : tcode_sub, - 'tcode_main_old' : tcode_main_old, - 'tcode_sub_old' : tcode_sub_old, + 'tcode':{ + 'mains' : tcode_main, + 'subs' : tcode_sub, + 'mains_old' : tcode_main_old, + 'subs_old' : tcode_sub_old, + } } # print(eid) @@ -162,7 +172,7 @@ def import_tanghih_plan_codes(cursor): response = requests.request("POST", url, headers=headers, data=payload) - if response.status_code == 500 : + if response.status_code != 200 : print(response.text) bulk_data = [] break @@ -202,12 +212,22 @@ def import_tanghih_plan_codes(cursor): eid='qs'+ str(prev_sectionid) data={ - 'id' : eid, - 'tcode_main' : tcode_main, - 'tcode_sub' : tcode_sub, - 'tcode_main_old' : tcode_main_old, - 'tcode_sub_old' : tcode_sub_old, - } + 'id' : eid, + 'tcode':{ + 'mains' : tcode_main, + 'subs' : tcode_sub, + 'mains_old' : tcode_main_old, + 'subs_old' : tcode_sub_old, + } + } + + # data={ + # 'id' : eid, + # 'tcode_main' : tcode_main, + # 'tcode_sub' : tcode_sub, + # 'tcode_main_old' : tcode_main_old, + # 'tcode_sub_old' : tcode_sub_old, + # } bulk_data.append(data) diff --git a/temp_codes.py b/temp_codes.py index b572f2b..27d932d 100644 --- a/temp_codes.py +++ b/temp_codes.py @@ -45,9 +45,11 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) #------------------------------------------------------- ## +# elastic.backupIndexToZipfile("G:/Project2/111", "dh_haditha") # elastic.backupIndexToZipfile("G:/Project2/111", "dh_parag_bge") # elastic.backupIndexToZipfile("G:/Project2/111", "gn_dataset") -# elastic.backupIndexToZipfile("G:/Project2/111", "mj_qa_section_ai") +#elastic.backupIndexToZipfile("G:/Project2/111", "mj_qa_section") +#elastic.backupIndexToZipfile("G:/Project2/111", "mj_qa_section_ai") # elastic.backupIndexToZipfile("G:/Project2/111", "mn_crition") #moveDataset1(elastic, "G:/Project2/111/gn_dataset_14031125.zip", "gn_dataset2") @@ -112,8 +114,8 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) ## # ## کد نمونه بازیابی از فایل -path_back = 'G:/Project2/111/mj_qa_section.zip' -elastic.createIndex('mj_qa_section', 'majles', 'mj_qa_section') +# path_back = 'G:/Project2/111/mj_qa_section.zip' +# elastic.createIndex('mj_qa_section', 'majles', 'mj_qa_section') # elastic.zipFileToElastic(path_back, 'mj_qa_section') #selastic.backupIndexToZipfile("G:/Project2/111", "dh_haditha") @@ -124,8 +126,9 @@ elastic.createIndex('mj_qa_section', 'majles', 'mj_qa_section') #elastic.backupIndexToZipfile("G:/Project2/111", "dh_volume_temp") #elastic.restorFileToElastic("G:/Project2/111", "mj_qa_section_ai", 'majles', True, "mj_qa_section") -# elastic.restorFileToElastic('G:/Project2/111', 'dh_haditha', 'dhadith', 'dh_haditha' ) -elastic.restorFileToElastic2('G:/Project2/111/mj_qa_section_2_majles_repair.zip', 'mj_qa_section', 'majles', False, 'mj_qa_section') +#elastic.restorFileToElastic('G:/Project2/111', 'mj_model', 'majles', True, 'mj_model' ) +elastic.restorFileToElastic2('G:/Project2/111/mj_model_20250512-003003.zip', 'mj_model', 'majles', True, 'mj_model') +# elastic.restorFileToElastic2('G:/Project2/111/mj_qa_section_2_majles_repair.zip', 'mj_qa_section', 'majles', False, 'mj_qa_section') body= { @@ -284,6 +287,28 @@ body= { } # elastic.exportToJsonForAI("./data/", "mj_qa_section", "keywords_extract_40K", body, ["content"]) +# body={ +# "query": { +# "term": { +# "main_type": { +# "value": "قانون" +# } +# } +# } +# } +#elastic.exportToJsonForAI("./data/", "mj_qa_qanon", "qanon_title", body, ["title", "ts_date", "ts_year"]) + +# body={ +# "query": { +# "term": { +# "main_type": { +# "value": "مقرره" +# } +# } +# } +# } +#elastic.exportToJsonForAI("./data/", "mj_qa_qanon", "regular_title", body, ["title", "ts_date", "ts_year"]) + #-------------------------------------------------------