import os import re import traceback import pandas as pd import json from dotenv import load_dotenv from common.config_base import * from common.elastic_helper import ElasticHelper from common.qanon_functions import * def parseBeforeInfos_SearchQanonTitle(elastic, file_excel, file_log, result_path = ""): # df = pd.read_excel(file_excel, sheet_name=0, header=None) df = pd.read_excel(file_excel, sheet_name=0) count = 0 rows_list = df.to_dict(orient='records') total = len(rows_list) print('total : ', total) column_names = df.columns.tolist() print(column_names) id2_index = column_names.index("_id2") id3_index = column_names.index("_id3") id_index = column_names.index("_id") if not id_index : id_index = len(column_names) column_names.append("_id") error_index = column_names.index("error") if not error_index : error_index = len(column_names)+1 column_names.append("error") file = open(file_log, 'w+', encoding='utf8') dict_qanon_id = get_dict_qanon_title("qanon_title_ids.txt") # Print the list of rows dic_error_qanon_title={} dic_error_section_title={} rows_list_new = [] bulk_data = [] total_count=0 row_number = 0 try: for row in rows_list: row_id = row[column_names[0]] qanon_title = row[column_names[1]] # print(qanon_title) ts_date = row[column_names[3]] _id2 = '' _id3 = '' if id2_index : _id2 = row[column_names[id2_index]] if id3_index : _id3 = row[column_names[id3_index]] # print(ts_date) row_number = row_number + 1 qanon_id = 0 count = count + 1 if (count % 10) == 0 : print("%s, %.2f " % ( '================== ', (count / total) * 100 if total > 0 else 0)) qanon_title = text_normalizer.normalize_search_qanon(qanon_title) # جداسازی اعداد چسبیده به کلمات قبل یا بعد مثل # قانون بودجه سال 1369کل کشور qanon_title = re.sub(r"([^\/])(\d+)([\u0600-\u06FF\u0750-\u077F]+?)", r"\1\2 \3", qanon_title) score = 100 state='قانون-جستجو' qanon_id = 0 # ابتدا از قانونهای خاص که شناسه ذخیره شده دارند جستجو کند if qanon_title in dict_qanon_id : score = 100 qanon_id = dict_qanon_id[qanon_title] state = 'قانون-دستی' # if qanon_id == 0 : # continue # این قانون در بانک ما وجود ندارد # اگر پیدا نشد، از الاستیک جستجو کند else : qanon_id, score, item = searchQanonId(elastic, qanon_title, ts_date, "mj_qa_qanon") # اگر قابل اطمینان نبود، خطا ثبت کند if score < 90 : qanon_id, score, item = searchQanonTitleOfContent(elastic, qanon_title, ts_date) if score < 90 : qanon_title = get_qanon_title_sample(qanon_title, False) qanon_title, ts_date, ts_ref = get_qanon_date(qanon_title) if ts_date or ts_ref : qanon_id, score, item = searchQanonId(elastic, qanon_title, ts_date, "mj_qa_qanon") if score < 90 : qanon_id, score, item = searchQanonTitleOfContent(elastic, qanon_title, ts_date) if score < 90 : if _id3 : qanon_id=_id3 state = 'دستی-مجلس' elif score == 80 : state = 'عنوان-تکراری' elif score < 80 : dic_error_qanon_title[qanon_title]= str(count) + ' - ' + str(row_number) print('-'*10 , str(count) , ' qanon error', qanon_title ) file.write('---- qanon error ---' + str(count) + ' ----- ' + qanon_title + '\n') qanon_id = 0 state = 'خطا-قانون' # ذخیره شناسه ها و وضعیت برای اکسل جدید row["_id"]=qanon_id row["error"]=state rows_list_new.append(row) columns = column_names new_df = pd.DataFrame(rows_list_new, columns=columns) if result_path == '' : directory_path = os.path.dirname(file_excel) result_path = directory_path + "/result/" file_name = os.path.basename(file_excel) file_excel2 = result_path + file_name.replace('.xlsx', '_result.xlsx') new_df.to_excel(file_excel2, index=False, engine='openpyxl') except Exception as e: file.write('----Exception error ---' + str(count) + ' ***' + str(e) + '\n') file.close() file_log1= file_log.replace(".txt", "_1.txt") with open(file_log1, 'w', encoding='utf8') as file1: file1.write(json.dumps(dic_error_qanon_title, ensure_ascii=False, indent=4)) def parseQaaninKol_SearchQanonTitle(elastic, file_excel, file_log, sheet_name=0, result_path = ""): # df = pd.read_excel(file_excel, sheet_name=0, header=None) df = pd.read_excel(file_excel, sheet_name=sheet_name) count = 0 rows_list = df.to_dict(orient='records') total = len(rows_list) print('total : ', total) column_names = df.columns.tolist() print(column_names) id_index=30 try: id_index = column_names.index("_id") except ValueError: id_index = len(column_names) column_names.append("_id") error_index=30 try: error_index = column_names.index("error") except ValueError: error_index = len(column_names) column_names.append("error") file = open(file_log, 'w+', encoding='utf8') dict_qanon_id = get_dict_qanon_title("qanon_title_ids.txt") # Print the list of rows dic_error_qanon_title={} dic_error_section_title={} rows_list_new = [] bulk_data = [] total_count=0 row_number = 0 try: for row in rows_list: if sheet_name == 0 : qanon_title = row[column_names[4]] ts_date = row[column_names[6]] else : qanon_title = row[column_names[4]] ts_date = row[column_names[5]] row_number = row_number + 1 qanon_id = 0 count = count + 1 if (count % 10) == 0 : print("%s, %.2f " % ( '================== ', (count / total) * 100 if total > 0 else 0)) qanon_title = text_normalizer.normalize_search_qanon(qanon_title) # جداسازی اعداد چسبیده به کلمات قبل یا بعد مثل # قانون بودجه سال 1369کل کشور qanon_title = re.sub(r"([^\/])(\d+)([\u0600-\u06FF\u0750-\u077F]+?)", r"\1\2 \3", qanon_title) score = 100 state='قانون-جستجو' qanon_id = 0 # ابتدا از قانونهای خاص که شناسه ذخیره شده دارند جستجو کند if qanon_title in dict_qanon_id : score = 100 qanon_id = dict_qanon_id[qanon_title] state = 'قانون-دستی' # if qanon_id == 0 : # continue # این قانون در بانک ما وجود ندارد # اگر پیدا نشد، از الاستیک جستجو کند else : qanon_id, score, item = searchQanonId(elastic, qanon_title, ts_date, "mj_qa_qanon") # اگر قابل اطمینان نبود، خطا ثبت کند if score < 90 : qanon_id, score, item = searchQanonTitleOfContent(elastic, qanon_title, ts_date) if score < 90 : qanon_title = get_qanon_title_sample(qanon_title, False) qanon_title, ts_date, ts_ref = get_qanon_date(qanon_title) if ts_date or ts_ref : qanon_id, score, item = searchQanonId(elastic, qanon_title, ts_date, "mj_qa_qanon") if score < 90 : qanon_id, score, item = searchQanonTitleOfContent(elastic, qanon_title, ts_date) if score == 80 : state = 'عنوان-تکراری' elif score < 80 : dic_error_qanon_title[qanon_title]= str(count) + ' - ' + str(row_number) print('-'*10 , str(count) , ' qanon error', qanon_title ) file.write('---- qanon error ---' + str(count) + ' ----- ' + qanon_title + '\n') qanon_id = 0 state = 'خطا-قانون' # ذخیره شناسه ها و وضعیت برای اکسل جدید row["_id"]=qanon_id row["error"]=state rows_list_new.append(row) columns = column_names new_df = pd.DataFrame(rows_list_new, columns=columns) if result_path == '' : directory_path = os.path.dirname(file_excel) result_path = directory_path + "/result/" file_name = os.path.basename(file_excel) file_excel2 = result_path + file_name.replace('.xlsx', '_'+str(sheet_name+1) + '_result.xlsx') new_df.to_excel(file_excel2, index=False, engine='openpyxl') except Exception as e: file.write('----Exception error ---' + str(count) + ' ***' + str(e) + '\n') file.close() file_log1= file_log.replace(".txt", "_1.txt") with open(file_log1, 'w', encoding='utf8') as file1: file1.write(json.dumps(dic_error_qanon_title, ensure_ascii=False, indent=4)) def convertField(name ): base_items = [ {"type": "ابلاغ به رئیس جمهور", "order": 1}, {"type": "توشیح و امضاء قانون", "order": 2}, {"type": "ابلاغ به مجریان", "order": 3}, {"type": "ارسال به روزنامه رسمی", "order": 4}, {"type": "انتشار در روزنامه رسمی", "order": 5}, ] res_type="" res_order=0 col="" if name == "شماره": col= "number" elif name == "تاریخ اعلام نظر": col="date" elif name == "متن نظریه": col="content" elif name == "تاریخ ابلاغ به رئیس جمهور": res_type=base_items[0]["type"] res_order=base_items[0]["order"] col="date" elif name == "شماره ابلاغ به رئیس جمهور": res_type=base_items[0]["type"] res_order=base_items[0]["order"] col="number" elif name == "تاریخ توشیح و امضا قانون": res_type=base_items[1]["type"] res_order=base_items[1]["order"] col="date" elif name == "تاریخ ابلاغ به مجریان": res_type=base_items[2]["type"] res_order=base_items[2]["order"] col="date" elif name == "شماره ابلاغ به مجریان": res_type=base_items[2]["type"] res_order=base_items[2]["order"] col="number" elif name == "تاریخ ارسال به روزنامه رسمی": res_type=base_items[3]["type"] res_order=base_items[3]["order"] col="date" elif name == "شماره ارسال به روزنامه رسمی": res_type=base_items[3]["type"] res_order=base_items[3]["order"] col="number" elif name == "تاریخ انتشار در روزنامه رسمی": res_type=base_items[4]["type"] res_order=base_items[4]["order"] col="date" elif name == "شماره انتشار در روزنامه رسمی": res_type=base_items[4]["type"] res_order=base_items[4]["order"] col="number" # elif name == "تاریخ نظریه شورای نگهبان": # col="date" # elif name == "شماره نظریه شورای نگهبان": # col="number" # elif name == "مغایرت نظریه شورای نگهبان": # col="content" # elif name == "استمحالی": # col="" # elif name == "استنکافی": # col="" # elif name == "دستگاههای اجرایی مرتبط": # col="" return col, res_type, res_order def getDictBeforeInfos(file_excel, sheet_name): df = pd.read_excel(file_excel, sheet_name=sheet_name) rows_list = df.to_dict(orient='records') total = len(rows_list) print('total : ', total) column_names = df.columns.tolist() result={} try: for row in rows_list: row_id = row[column_names[0]] if sheet_name == 1 : # eblagh letters = [] letters_dict = {} for col in column_names: name, type, order = convertField(col) if name != '' : if order not in letters_dict : letters.append({"order":order, "type":type}) letters_dict[order] = len(letters)-1 index = letters_dict[order] letters[index][name] = row[col] elif col == "استمحالی" or name == "استنکافی": pass elif col == "دستگاههای اجرایی مرتبط": result["exceuter_organs"] = row[col] except Exception as e: # file.write('----Exception error ---' + str(count) + ' ***' + str(e) + '\n') pass return result def parseBeforeInfos_SendData(elastic, file_excel, file_log, result_path = ""): # df = pd.read_excel(file_excel, sheet_name=0, header=None) df_main = pd.read_excel(file_excel, sheet_name=0) count = 0 rows_list = df_main.to_dict(orient='records') total = len(rows_list) print('total : ', total) column_names = df_main.columns.tolist() print(column_names) id2_index = column_names.index("_id2") id3_index = column_names.index("_id3") id_index = column_names.index("_id") if not id_index : id_index = len(column_names) column_names.append("_id") error_index = column_names.index("error") if not error_index : error_index = len(column_names)+1 column_names.append("error") file = open(file_log, 'w+', encoding='utf8') dict_qanon_id = get_dict_qanon_title("qanon_title_ids.txt") # Print the list of rows dic_error_qanon_title={} dic_error_section_title={} rows_list_new = [] bulk_data = [] total_count=0 row_number = 0 try: for row in rows_list: row_id = row[column_names[0]] qanon_title = row[column_names[1]] # print(qanon_title) ts_date = row[column_names[3]] _id2 = '' _id3 = '' if id2_index : _id2 = row[column_names[id2_index]] if id3_index : _id3 = row[column_names[id3_index]] # print(ts_date) row_number = row_number + 1 qanon_id = 0 count = count + 1 if (count % 10) == 0 : print("%s, %.2f " % ( '================== ', (count / total) * 100 if total > 0 else 0)) qanon_title = text_normalizer.normalize_search_qanon(qanon_title) # جداسازی اعداد چسبیده به کلمات قبل یا بعد مثل # قانون بودجه سال 1369کل کشور qanon_title = re.sub(r"([^\/])(\d+)([\u0600-\u06FF\u0750-\u077F]+?)", r"\1\2 \3", qanon_title) score = 100 state='قانون-جستجو' qanon_id = 0 # ابتدا از قانونهای خاص که شناسه ذخیره شده دارند جستجو کند if qanon_title in dict_qanon_id : score = 100 qanon_id = dict_qanon_id[qanon_title] state = 'قانون-دستی' # if qanon_id == 0 : # continue # این قانون در بانک ما وجود ندارد # اگر پیدا نشد، از الاستیک جستجو کند else : qanon_id, score, item = searchQanonId(elastic, qanon_title, ts_date, "mj_qa_qanon") # اگر قابل اطمینان نبود، خطا ثبت کند if score < 90 : qanon_id, score, item = searchQanonTitleOfContent(elastic, qanon_title, ts_date) if score < 90 : qanon_title = get_qanon_title_sample(qanon_title, False) qanon_title, ts_date, ts_ref = get_qanon_date(qanon_title) if ts_date or ts_ref : qanon_id, score, item = searchQanonId(elastic, qanon_title, ts_date, "mj_qa_qanon") if score < 90 : qanon_id, score, item = searchQanonTitleOfContent(elastic, qanon_title, ts_date) if score < 90 : if _id3 : qanon_id=_id3 state = 'دستی-مجلس' elif score == 80 : state = 'عنوان-تکراری' elif score < 80 : dic_error_qanon_title[qanon_title]= str(count) + ' - ' + str(row_number) print('-'*10 , str(count) , ' qanon error', qanon_title ) file.write('---- qanon error ---' + str(count) + ' ----- ' + qanon_title + '\n') qanon_id = 0 state = 'خطا-قانون' # ذخیره شناسه ها و وضعیت برای اکسل جدید row["_id"]=qanon_id row["error"]=state rows_list_new.append(row) columns = column_names new_df = pd.DataFrame(rows_list_new, columns=columns) if result_path == '' : directory_path = os.path.dirname(file_excel) result_path = directory_path + "/result/" file_name = os.path.basename(file_excel) file_excel2 = result_path + file_name.replace('.xlsx', '_result.xlsx') new_df.to_excel(file_excel2, index=False, engine='openpyxl') except Exception as e: file.write('----Exception error ---' + str(count) + ' ***' + str(e) + '\n') file.close() file_log1= file_log.replace(".txt", "_1.txt") with open(file_log1, 'w', encoding='utf8') as file1: file1.write(json.dumps(dic_error_qanon_title, ensure_ascii=False, indent=4)) # --------------------------------------------------------- # --------------------------------------------------------- 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_113') password=os.getenv('HOST_113_PASS') print(password) path_base_map = "G:/Project2/gateway2_main/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) # --------------------------------------------------------- # --------------------------------------------------------- # parseBeforeInfos_SearchQanonTitle(elastic, "./data/جداول بانک قوانین_info-شناسه قانون یار.xlsx" , "./data/result/پیشا قانون.txt", "./data/result/") #parseQaaninKol_SearchQanonTitle(elastic, "./data/فهرست تمام قوانین.xlsx", "./data/result/فهرست 1 تمام قوانین.txt", 0, "./data/result/") #parseQaaninKol_SearchQanonTitle(elastic, "./data/فهرست تمام قوانین.xlsx", "./data/result/فهرست 2 تمام قوانین.txt", 1, "./data/result/") # --------------------------------------------------------- # --------------------------------------------------------- parseBeforeInfos_SendData(elastic, "./data/جداول بانک قوانین_info-شناسه قانون یار.xlsx" , "./data/result/پیشا قانون.txt", "./data/result/") qanon_title=" قانون الحاق دو تبصره به عنوان تبصره های 3 و 4 به ماده 9 قانون روابط موجر و مستأجر مصوب 1362.2.13" ts_data="1369/07/08" # qanon_title = get_qanon_title_sample(qanon_title, False) # print(get_qanon_date(qanon_title)) # searchQanonId(elastic, qanon_title, ts_data) # searchQanonTitleOfContent(elastic, qanon_title, ts_data)