diff --git a/elastic_helper.py b/elastic_helper.py index f206a43..3425cf9 100644 --- a/elastic_helper.py +++ b/elastic_helper.py @@ -3,7 +3,7 @@ import sys import os import json from time import sleep -from elasticsearch8 import Elasticsearch,helpers +from elasticsearch import Elasticsearch,helpers class ElasticHelper(): diff --git a/r3_import_excel_tcodes.py b/r3_import_excel_tcodes.py index a6049dd..e71218e 100644 --- a/r3_import_excel_tcodes.py +++ b/r3_import_excel_tcodes.py @@ -1,5 +1,7 @@ import os import re +import traceback +import json from dotenv import load_dotenv from config_base import * # from general_functions import * @@ -357,32 +359,49 @@ def importCodesToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_i def get_qanon_date(title): - regex = r"مصوب(\s*\d{1,4}/\d{1,4}/\d{1,4})\s*$" + # regex = r"مصوب(\s*\d{1,4}/\d{1,4}/\d{1,4})\s*$" regex = r"مصوب\s*(\d{1,4}/\s*\d{1,4}/\s*\d{1,4})\s*$" + regex_ex = r"مصوب\s*(\d{1,4}/\s*\d{1,4}/\s*\d{1,4})\s*((مجلس|کمیسیون|کمیسیونهای|هیات|شورای).{1,50})$" ts_ref = '' qanon_title = '' ts_date = '' + title=title.strip().strip(' #=').strip() + title=title.replace("=", "#") + title=title.replace("#مصوب#", "#مصوب ") + title=title.replace("#مصوب #", "#مصوب ") items = title.split('#') if len(items) == 1: qanon_title = items[0] - else: + elif items : last = items[-1].strip() - if 'مصوب' in last : - ts_date = last.replace('مصوب').strip() + if last.startswith('مصوب'): + ts_date = last.replace('مصوب', '').strip() + qanon_title= ' '.join(items[:-1]) + # قانون تشكيل#وزارت جهاد سازندگي مصوب 08/09/1362 مجلس شوراي اسلامي + elif 'مصوب' in last : + qanon_title= ' '.join(items) elif last : ts_ref=last + qanon_title= ' '.join(items[:-1]) - qanon_title= ' '.join(items[:-1]) - print('qanon_title', qanon_title) + # print('qanon_title', qanon_title) if ts_date == '' : # قانون كار مصوب 26/12/1337 match = re.search(regex, qanon_title) if match : - print(f"Match found: {match.group(1)} at position {match.start()}-{match.end()}") + #print(f"Match found: {match.group(1)} at position {match.start()}-{match.end()}") ts_date = match.group(1).strip() qanon_title = qanon_title[0:match.start()] + + if ts_date == '': + match = re.search(regex_ex, qanon_title) + if match : + #print(f"Match found: {match.group(1)} at position {match.start()}-{match.end()}") + ts_date = match.group(1).strip() + ts_ref = match.group(2).strip() + qanon_title = qanon_title[0:match.start()] return qanon_title, ts_date, ts_ref @@ -395,6 +414,7 @@ def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_ rows_list = df.to_dict(orient='records') column_names = df.columns.tolist() + column_count = len(column_names) # print(column_names) total = len(df[column_names[0]]) @@ -418,7 +438,9 @@ def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_ ts_date = "" code_state = "" if title is None or is_nan_or_none_or_empty(title) or title is None or is_nan_or_none_or_empty(title) : - title = prevRow[column_names[1]] + title = prevRow[column_names[1]] + else: + prevRow = row qanon_id = 0 @@ -431,18 +453,18 @@ def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_ title = text_normalizer.normalize_search_qanon(title) - qanon_title, ts_date, ts_ref = get_qanon_date(title) 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 + # قانون ساختمان شاهراه تهران – كرج + qanon_title = qanon_title.replace("–", "-") + score = 100 # ابتدا از قانونهای خاص که شناسه ذخیره شده دارند جستجو کند if qanon_title in dict_qanon_id : score = 100 @@ -479,9 +501,10 @@ def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_ # state = 'صحیح' # ذخیره شناسه ها و وضعیت برای اکسل جدید - row[5]=qanon_id - row[6]=section_id - row[7]=state + row[column_count+1]=qanon_id + row[column_count+2]=section_id + row[column_count+3]=state + row[column_count+4]=qanon_title +'#'+ ts_date +'#'+ ts_ref rows_list_new.append(row) data={} @@ -529,7 +552,11 @@ def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_ # columns=['جزء', 'مصوب', 'قانون', 'اصلی', 'ردیف', 'شناسه قانون', 'شناسه جزء', 'وضعیت پردازش'] - columns=[0, 1, 2, 3, 4, 5, 6, 7] + column_names.append(column_count+1) + column_names.append(column_count+2) + column_names.append(column_count+3) + column_names.append(column_count+4) + columns= column_names new_df = pd.DataFrame(rows_list_new, columns=columns) if result_path == '' : directory_path = os.path.dirname(file_excel) @@ -543,7 +570,10 @@ def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_ except Exception as e: - print(e) + tb = traceback.extract_tb(e.__traceback__) + print(tb) + line_number = tb[-1].lineno # Get the line number of the last call + print(f"Error: {e} on line {line_number}") file.write('----Exception error ---' + str(count) + ' ***' + str(e) + '\n') file.close() @@ -789,6 +819,9 @@ def run_Etebar_directory(elastic, directory, output): w1 = text_normalizer.split_words(code_name)[0] log_file = output + "/log_r_"+w1+".txt" file_path = os.path.join(directory, file) + if code_name != 'کشاورزی' : + continue + try: os.makedirs(output, exist_ok=True) except Exception as e: @@ -796,6 +829,7 @@ def run_Etebar_directory(elastic, directory, output): code_id = 0 importEtebarToElastic(elastic, file_path, "mj_qa_section", code_name, code_id, log_file) + # if code_name in dic_code_id : # code_id = dic_code_id[code_name]