procces refactor majles
This commit is contained in:
@@ -1,712 +0,0 @@
|
||||
import os
|
||||
import re
|
||||
import traceback
|
||||
import json
|
||||
import pandas as pd
|
||||
from dotenv import load_dotenv
|
||||
# from app.common.general_functions import *
|
||||
from app.common.qanon_functions import *
|
||||
from app.common.elastic_helper import ElasticHelper
|
||||
from app.common.config_base import *
|
||||
|
||||
def importCodesToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_id , file_log, result_path = ""):
|
||||
# excel_helper = ExcelHelper()
|
||||
# df = excel_helper.load_data(file)
|
||||
|
||||
# df = pd.read_excel('records.xlsx', sheet_name='Cars', usecols=['Car Name', 'Car Price'])
|
||||
# print(df)
|
||||
df = pd.read_excel(file_excel, sheet_name=0, header=None)
|
||||
# print(df[0:10])
|
||||
# print(df.columns.ravel())
|
||||
# print(df.columns.tolist())
|
||||
# print(df['EmpName'].tolist())
|
||||
# print(df[0].tolist())
|
||||
count = 0
|
||||
rows_list = df.to_dict(orient='records')
|
||||
total = len(df[0])
|
||||
print('total : ', total)
|
||||
|
||||
column_names = df.columns.tolist()
|
||||
print(column_names)
|
||||
|
||||
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
|
||||
try:
|
||||
for row in rows_list:
|
||||
section_title = row[column_names[0]]
|
||||
ts_date = row[column_names[1]]
|
||||
qanon_title = row[column_names[2]]
|
||||
code_state = row[column_names[3]]
|
||||
row_number = row[column_names[4]]
|
||||
# if code_state :
|
||||
# print ( 'ddddddddddddddcode_state' , code_state)
|
||||
# else :
|
||||
# print ( 'ccccccccccccccode_state' , code_state)
|
||||
|
||||
qanon_id = 0
|
||||
section_id = 0
|
||||
state = ''
|
||||
|
||||
count = count + 1
|
||||
if (count % 10) == 0 :
|
||||
print("%s, %.2f " % ( '================== ', (count / total) * 100 if total > 0 else 0))
|
||||
|
||||
|
||||
if section_title is None or is_nan_or_none_or_empty(section_title) or qanon_title is None or is_nan_or_none_or_empty(qanon_title) :
|
||||
file.write('!!!!!! nan error !!!!!!!' + str(count) + '\n')
|
||||
state = 'خطا-پرش'
|
||||
else:
|
||||
section_title = text_normalizer.normalize_search_qanon(str(section_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
|
||||
|
||||
# ابتدا از قانونهای خاص که شناسه ذخیره شده دارند جستجو کند
|
||||
if qanon_title in dict_qanon_id :
|
||||
score = 100
|
||||
qanon_id = dict_qanon_id[qanon_title]
|
||||
state = 'قانون-دستی'
|
||||
# if qanon_id == 0 :
|
||||
# continue # این قانون در بانک ما وجود ندارد
|
||||
|
||||
# اگر پیدا نشد، از الاستیک جستجو کند
|
||||
if qanon_id == 0 :
|
||||
qanon_id, score, item = searchQanonId(elastic, qanon_title, ts_date, "mj_qa_qanon")
|
||||
# اگر قابل اطمینان نبود، خطا ثبت کند
|
||||
if score < 90 :
|
||||
dic_error_qanon_title[qanon_title]=count
|
||||
print('-'*10 , str(count) , ' qanon error', qanon_title )
|
||||
file.write('---- qanon error ---' + str(count) + ' ----- ' + qanon_title + '\n')
|
||||
qanon_id = 0
|
||||
state = 'خطا-قانون'
|
||||
else:
|
||||
state = 'قانون-جستجو'
|
||||
|
||||
# اگر شناسه قانون یافت شد، برای شناسه جزء جستجو کند ...
|
||||
if not qanon_id == 0 :
|
||||
section_id, score, section_item = searchSectionId(elastic, "mj_qa_section", qanon_id , section_title)
|
||||
# - اگر شناسه یافت شده جزء قابل اطمینان نبود خطا ثبت شود
|
||||
if score < 90 :
|
||||
dic_error_section_title[str(qanon_id)+'=='+section_title]=count
|
||||
print('='*10 , str(count) , ' section error', qanon_id, ' --> ', section_title )
|
||||
file.write('---- section error ---' + str(qanon_id) + ' ----- ' + section_title + '\n')
|
||||
section_id = 0
|
||||
state = 'خطا-جزء'
|
||||
else :
|
||||
state = 'صحیح'
|
||||
|
||||
# ذخیره شناسه ها و وضعیت برای اکسل جدید
|
||||
row[5]=qanon_id
|
||||
row[6]=section_id
|
||||
row[7]=state
|
||||
rows_list_new.append(row)
|
||||
|
||||
data={}
|
||||
# ذخیره کد در الاستیک برای شناسه های یافت شده
|
||||
if section_id and section_id != 0:
|
||||
item = elastic.get_doctument(index_name_o,section_id)
|
||||
tcode_mains = []
|
||||
tcode_subs = []
|
||||
if "tcode_mains" in item['_source'] :
|
||||
tcode_mains= item['_source']["tcode_mains"]
|
||||
if "tcode_subs" in item['_source'] :
|
||||
tcode_subs= item['_source']["tcode_subs"]
|
||||
|
||||
code = {
|
||||
"label" : tcode_label,
|
||||
"id" : tcode_id
|
||||
}
|
||||
if code_state == True :
|
||||
tcode_mains.append(code)
|
||||
else:
|
||||
tcode_subs.append(code)
|
||||
|
||||
data={
|
||||
'_id':section_id,
|
||||
'_source' : {
|
||||
'tcode_mains': tcode_mains,
|
||||
'tcode_subs': tcode_subs,
|
||||
}
|
||||
}
|
||||
bulk_data.append(data)
|
||||
|
||||
|
||||
# انتقال دسته جمعی کدها به الاستیک
|
||||
if len(bulk_data) >= 200:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count))
|
||||
elastic.updateBulkList(bulk_data, index_name_o)
|
||||
bulk_data = []
|
||||
|
||||
|
||||
|
||||
if len(bulk_data) > 0:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count) )
|
||||
elastic.updateBulkList(bulk_data, index_name_o)
|
||||
|
||||
|
||||
##### columns=['جزء', 'مصوب', 'قانون', 'اصلی', 'ردیف', 'شناسه قانون', 'شناسه جزء', 'وضعیت پردازش']
|
||||
# columns=[0, 1, 2, 3, 4, 5, 6, 7]
|
||||
# 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')
|
||||
|
||||
# print('rows_list_new', str(len(rows_list_new)), ' --> ', file_excel2)
|
||||
# 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))
|
||||
|
||||
file_log2= file_log.replace(".txt", "_2.txt")
|
||||
with open(file_log2, 'w', encoding='utf8') as file2:
|
||||
file2.write(json.dumps(dic_error_section_title, ensure_ascii=False, indent=4))
|
||||
|
||||
def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_id , file_log, result_path = ""):
|
||||
|
||||
df = pd.read_excel(file_excel, sheet_name=0)
|
||||
|
||||
count = 0
|
||||
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]])
|
||||
print('total : ', total)
|
||||
|
||||
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
|
||||
prevRow = None
|
||||
try:
|
||||
for row in rows_list:
|
||||
row_number = row[column_names[0]]
|
||||
title = row[column_names[1]]
|
||||
section_title = row[column_names[2]]
|
||||
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]]
|
||||
else:
|
||||
prevRow = row
|
||||
|
||||
|
||||
qanon_id = 0
|
||||
section_id = 0
|
||||
state = ''
|
||||
|
||||
count = count + 1
|
||||
if (count % 10) == 0 :
|
||||
print("%s, %.2f " % ( '================== ', (count / total) * 100 if total > 0 else 0))
|
||||
|
||||
|
||||
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)
|
||||
|
||||
# قانون ساختمان شاهراه تهران – كرج
|
||||
qanon_title = qanon_title.replace("–", "-")
|
||||
|
||||
score = 100
|
||||
# ابتدا از قانونهای خاص که شناسه ذخیره شده دارند جستجو کند
|
||||
if qanon_title in dict_qanon_id :
|
||||
score = 100
|
||||
qanon_id = dict_qanon_id[qanon_title]
|
||||
state = 'قانون-دستی'
|
||||
# if qanon_id == 0 :
|
||||
# continue # این قانون در بانک ما وجود ندارد
|
||||
|
||||
# اگر پیدا نشد، از الاستیک جستجو کند
|
||||
if qanon_id == 0 :
|
||||
qanon_id, score, item = searchQanonId(elastic, qanon_title, ts_date, "mj_qa_qanon")
|
||||
# اگر قابل اطمینان نبود، خطا ثبت کند
|
||||
if score < 90 :
|
||||
dic_error_qanon_title[qanon_title]=count
|
||||
print('-'*10 , str(count) , ' qanon error', qanon_title )
|
||||
file.write('---- qanon error ---' + str(count) + ' ----- ' + qanon_title + '\n')
|
||||
qanon_id = 0
|
||||
state = 'خطا-قانون'
|
||||
else:
|
||||
state = 'قانون-جستجو'
|
||||
|
||||
# اگر شناسه قانون یافت شد، برای شناسه جزء جستجو کند ...
|
||||
section_title = text_normalizer.normalize_search_qanon(str(section_title))
|
||||
result_section_title = get_section_correct(section_title)
|
||||
result_section_id = []
|
||||
if qanon_id != 0 :
|
||||
|
||||
for section_title in result_section_title:
|
||||
if section_title == 'all' :
|
||||
state = 'صحیح'
|
||||
result_section_id.append('all')
|
||||
continue
|
||||
|
||||
section_id, score, section_item = searchSectionId(elastic, "mj_qa_section", qanon_id , section_title)
|
||||
# - اگر شناسه یافت شده جزء قابل اطمینان نبود خطا ثبت شود
|
||||
if score < 90 :
|
||||
dic_error_section_title[str(qanon_id)+'=='+section_title]=count
|
||||
print('='*10 , str(count) , ' section error', qanon_id, ' --> ', section_title )
|
||||
file.write('---- section error ---' + str(qanon_id) + ' ----- ' + section_title + '\n')
|
||||
section_id = 0
|
||||
state = 'خطا-جزء'
|
||||
elif state == '':
|
||||
state = 'صحیح'
|
||||
|
||||
result_section_id.append(section_id)
|
||||
|
||||
# ذخیره شناسه ها و وضعیت برای اکسل جدید
|
||||
row[column_count+1]=qanon_id
|
||||
row[column_count+2]= '#'.join(str(item) for item in result_section_id)
|
||||
row[column_count+3]=state
|
||||
row[column_count+4]=qanon_title +'#'+ ts_date +'#'+ ts_ref
|
||||
row[column_count+5]='#'.join(result_section_title)
|
||||
rows_list_new.append(row)
|
||||
|
||||
data={}
|
||||
|
||||
tstate = 'نامعتبر'
|
||||
c_name = 'وضعیت اعتبار'
|
||||
if c_name in column_names :
|
||||
tstate = row[c_name].replace('#', '').replace('=', '')
|
||||
if tstate.startswith('نامعتبر') :
|
||||
tstate = 'نامعتبر-' + tstate
|
||||
|
||||
treason = ''
|
||||
c_name = 'مستند عدم اعتبار'
|
||||
if c_name in column_names :
|
||||
treason = row[c_name].replace('#','\n').replace('=','\n')
|
||||
|
||||
description = ''
|
||||
c_name = 'توضیحات'
|
||||
if c_name in column_names :
|
||||
description = row[c_name].replace('*', '')
|
||||
|
||||
# ذخیره کد در الاستیک برای شناسه های یافت شده
|
||||
for section_id in result_section_id :
|
||||
|
||||
if section_id == '' or section_id == 0 :
|
||||
continue
|
||||
|
||||
if section_id =='all':
|
||||
tstate = tstate + ' کل '
|
||||
result = get__all_section_qanon(elastic, index_name_o, qanon_id)
|
||||
for sid in result :
|
||||
tvalid_state={
|
||||
"tstate": tstate.strip(),
|
||||
"description": description,
|
||||
"treason": treason,
|
||||
# "treason_type": treason_type
|
||||
}
|
||||
|
||||
data={
|
||||
'_id':sid,
|
||||
'_source' : {
|
||||
'tvalid_state': tvalid_state
|
||||
}
|
||||
}
|
||||
bulk_data.append(data)
|
||||
break
|
||||
|
||||
tvalid_state={
|
||||
"tstate": tstate.strip(),
|
||||
"description": description,
|
||||
"treason": treason,
|
||||
# "treason_type": treason_type
|
||||
}
|
||||
|
||||
data={
|
||||
'_id':section_id,
|
||||
'_source' : {
|
||||
'tvalid_state': tvalid_state
|
||||
}
|
||||
}
|
||||
bulk_data.append(data)
|
||||
|
||||
|
||||
# انتقال دسته جمعی کدها به الاستیک
|
||||
if len(bulk_data) >= 200:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count))
|
||||
elastic.updateBulkList(bulk_data, index_name_o)
|
||||
bulk_data = []
|
||||
|
||||
|
||||
|
||||
if len(bulk_data) > 0:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count) )
|
||||
elastic.updateBulkList(bulk_data, index_name_o)
|
||||
|
||||
|
||||
# columns=['جزء', 'مصوب', 'قانون', 'اصلی', 'ردیف', 'شناسه قانون', 'شناسه جزء', 'وضعیت پردازش']
|
||||
column_names.append(column_count+1)
|
||||
column_names.append(column_count+2)
|
||||
column_names.append(column_count+3)
|
||||
column_names.append(column_count+4)
|
||||
column_names.append(column_count+5)
|
||||
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')
|
||||
|
||||
print('rows_list_new', str(len(rows_list_new)), ' --> ', file_excel2)
|
||||
# new_df.to_excel(file_excel2, index=False, engine='openpyxl')
|
||||
|
||||
|
||||
except Exception as 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()
|
||||
|
||||
# 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))
|
||||
|
||||
# file_log2= file_log.replace(".txt", "_2.txt")
|
||||
# with open(file_log2, 'w', encoding='utf8') as file2:
|
||||
# file2.write(json.dumps(dic_error_section_title, ensure_ascii=False, indent=4))
|
||||
|
||||
|
||||
def importCodesToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_id , file_log, result_path = ""):
|
||||
# excel_helper = ExcelHelper()
|
||||
# df = excel_helper.load_data(file)
|
||||
|
||||
# df = pd.read_excel('records.xlsx', sheet_name='Cars', usecols=['Car Name', 'Car Price'])
|
||||
# print(df)
|
||||
df = pd.read_excel(file_excel, sheet_name=0, header=None)
|
||||
# print(df[0:10])
|
||||
# print(df.columns.ravel())
|
||||
# print(df.columns.tolist())
|
||||
# print(df['EmpName'].tolist())
|
||||
# print(df[0].tolist())
|
||||
count = 0
|
||||
rows_list = df.to_dict(orient='records')
|
||||
total = len(df[0])
|
||||
print('total : ', total)
|
||||
|
||||
column_names = df.columns.tolist()
|
||||
print(column_names)
|
||||
|
||||
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
|
||||
try:
|
||||
for row in rows_list:
|
||||
section_title = row[column_names[0]]
|
||||
ts_date = row[column_names[1]]
|
||||
qanon_title = row[column_names[2]]
|
||||
code_state = row[column_names[3]]
|
||||
row_number = row[column_names[4]]
|
||||
|
||||
qanon_id = 0
|
||||
section_id = 0
|
||||
state = ''
|
||||
|
||||
count = count + 1
|
||||
if (count % 10) == 0 :
|
||||
print("%s, %.2f " % ( '================== ', (count / total) * 100 if total > 0 else 0))
|
||||
|
||||
|
||||
if section_title is None or is_nan_or_none_or_empty(section_title) or qanon_title is None or is_nan_or_none_or_empty(qanon_title) :
|
||||
file.write('!!!!!! nan error !!!!!!!' + str(count) + '\n')
|
||||
state = 'خطا-پرش'
|
||||
else:
|
||||
section_title = text_normalizer.normalize_search_qanon(str(section_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
|
||||
|
||||
# ابتدا از قانونهای خاص که شناسه ذخیره شده دارند جستجو کند
|
||||
if qanon_title in dict_qanon_id :
|
||||
score = 100
|
||||
qanon_id = dict_qanon_id[qanon_title]
|
||||
state = 'قانون-دستی'
|
||||
# if qanon_id == 0 :
|
||||
# continue # این قانون در بانک ما وجود ندارد
|
||||
|
||||
# اگر پیدا نشد، از الاستیک جستجو کند
|
||||
if qanon_id == 0 :
|
||||
qanon_id, score, item = searchQanonId(elastic, qanon_title, ts_date, "mj_qa_qanon")
|
||||
# اگر قابل اطمینان نبود، خطا ثبت کند
|
||||
if score < 90 :
|
||||
dic_error_qanon_title[qanon_title]=count
|
||||
print('-'*10 , str(count) , ' qanon error', qanon_title )
|
||||
file.write('---- qanon error ---' + str(count) + ' ----- ' + qanon_title + '\n')
|
||||
qanon_id = 0
|
||||
state = 'خطا-قانون'
|
||||
else:
|
||||
state = 'قانون-جستجو'
|
||||
|
||||
# اگر شناسه قانون یافت شد، برای شناسه جزء جستجو کند ...
|
||||
if not qanon_id == 0 :
|
||||
section_id, score, section_item = searchSectionId(elastic, "mj_qa_section", qanon_id , section_title)
|
||||
# - اگر شناسه یافت شده جزء قابل اطمینان نبود خطا ثبت شود
|
||||
if score < 90 :
|
||||
dic_error_section_title[str(qanon_id)+'=='+section_title]=count
|
||||
print('='*10 , str(count) , ' section error', qanon_id, ' --> ', section_title )
|
||||
file.write('---- section error ---' + str(qanon_id) + ' ----- ' + section_title + '\n')
|
||||
section_id = 0
|
||||
state = 'خطا-جزء'
|
||||
else :
|
||||
state = 'صحیح'
|
||||
|
||||
# ذخیره شناسه ها و وضعیت برای اکسل جدید
|
||||
row[5]=qanon_id
|
||||
row[6]=section_id
|
||||
row[7]=state
|
||||
rows_list_new.append(row)
|
||||
|
||||
data={}
|
||||
# ذخیره کد در الاستیک برای شناسه های یافت شده
|
||||
if section_id and section_id != 0:
|
||||
item = elastic.get_doctument(index_name_o,section_id)
|
||||
tcode_mains = []
|
||||
tcode_subs = []
|
||||
if "tcode_mains" in item['_source'] :
|
||||
tcode_mains= item['_source']["tcode_mains"]
|
||||
if "tcode_subs" in item['_source'] :
|
||||
tcode_subs= item['_source']["tcode_subs"]
|
||||
|
||||
code = {
|
||||
"label" : tcode_label,
|
||||
"id" : tcode_id
|
||||
}
|
||||
if code_state :
|
||||
tcode_mains.append(code)
|
||||
else:
|
||||
tcode_subs.append(code)
|
||||
data={
|
||||
'_id':section_id,
|
||||
'_source' : {
|
||||
'tcode_mains': tcode_mains,
|
||||
'tcode_subs': tcode_subs,
|
||||
}
|
||||
}
|
||||
bulk_data.append(data)
|
||||
|
||||
|
||||
# انتقال دسته جمعی کدها به الاستیک
|
||||
if len(bulk_data) >= 200:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count))
|
||||
elastic.updateBulkList(bulk_data, index_name_o)
|
||||
bulk_data = []
|
||||
|
||||
|
||||
|
||||
if len(bulk_data) > 0:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count) )
|
||||
elastic.updateBulkList(bulk_data, index_name_o)
|
||||
|
||||
|
||||
# columns=['جزء', 'مصوب', 'قانون', 'اصلی', 'ردیف', 'شناسه قانون', 'شناسه جزء', 'وضعیت پردازش']
|
||||
columns=[0, 1, 2, 3, 4, 5, 6, 7]
|
||||
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')
|
||||
|
||||
print('rows_list_new', str(len(rows_list_new)), ' --> ', file_excel2)
|
||||
# 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))
|
||||
|
||||
file_log2= file_log.replace(".txt", "_2.txt")
|
||||
with open(file_log2, 'w', encoding='utf8') as file2:
|
||||
file2.write(json.dumps(dic_error_section_title, ensure_ascii=False, indent=4))
|
||||
|
||||
|
||||
|
||||
def run_Codes_directory(elastic, directory, output):
|
||||
dic_code_id={
|
||||
"آب" : 1,
|
||||
"آمار، برنامه و بودجه" : 2,
|
||||
"آموزش و پرورش" : 3,
|
||||
"آیین دادرسی مدنی" : 5,
|
||||
"سلامت" : 30,
|
||||
"فرهنگ و هنر" : 33,
|
||||
"مسکن و شهرسازی" : 42,
|
||||
"مناطق آزاد و ویژه اقتصادی" : 45,
|
||||
"نفت و گاز" : 49
|
||||
}
|
||||
# List all files in the specified directory
|
||||
files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
|
||||
for file in files :
|
||||
file_name = os.path.basename(file)
|
||||
code_name = os.path.splitext(file_name)[0]
|
||||
w1 = text_normalizer.split_words(code_name)[0]
|
||||
log_file = output + "/log_r_"+w1+".txt"
|
||||
file_path = os.path.join(directory, file)
|
||||
try:
|
||||
os.makedirs(output, exist_ok=True)
|
||||
except Exception as e:
|
||||
print(output)
|
||||
|
||||
code_id = 0
|
||||
if code_name in dic_code_id :
|
||||
code_id = dic_code_id[code_name]
|
||||
importCodesToElastic(elastic, file_path, "mj_qa_section", code_name, code_id, log_file)
|
||||
else :
|
||||
print('00000 error 000000 ', code_name)
|
||||
|
||||
|
||||
|
||||
def run_Etebar_directory(elastic, directory, output):
|
||||
dic_code_id={
|
||||
"آب" : 1,
|
||||
"آمار، برنامه و بودجه" : 2,
|
||||
"آموزش و پرورش" : 3,
|
||||
"آیین دادرسی مدنی" : 5,
|
||||
"سلامت" : 30,
|
||||
"فرهنگ و هنر" : 33,
|
||||
"مسکن و شهرسازی" : 42,
|
||||
"مناطق آزاد و ویژه اقتصادی" : 45,
|
||||
"نفت و گاز" : 49,
|
||||
}
|
||||
# List all files in the specified directory
|
||||
files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
|
||||
for file in files :
|
||||
file_name = os.path.basename(file)
|
||||
if '~$' in file_name :
|
||||
continue
|
||||
code_name = os.path.splitext(file_name)[0]
|
||||
code_name = code_name.replace("نامعتبر-", "").strip()
|
||||
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:
|
||||
print(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]
|
||||
# importEtebarToElastic(elastic, file_path, "mj_qa_section", code_name, code_id, log_file)
|
||||
# else :
|
||||
# print('00000 error 000000 ', code_name)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
#run_Codes_directory(elastic, "./data/tcodes", "./data/tcodes/result")
|
||||
|
||||
run_Etebar_directory(elastic, "./data/tetebar", "./data/tetebar/result")
|
||||
|
||||
# importCodesToElastic(elastic, "./data/tcodes/مسکن و شهرسازی.xlsx", "mj_qa_section", "مسکن و شهرسازی", "./data/log_r3.txt")
|
||||
# importCodesToElastic(elastic, "./data/tcodes/آمار، برنامه و بودجه.xlsx", "mj_qa_section", "مسکن و شهرسازی", "./data/log_r_آمار.txt")
|
||||
# importCodesToElastic(elastic, "./data/tcodes/آیین دادرسی مدنی.xlsx", "mj_qa_section", "مسکن و شهرسازی", "./data/log_r_دادرسی.txt")
|
||||
# importCodesToElastic(elastic, "./data/tcodes/نفت و گاز.xlsx", "mj_qa_section", "مسکن و شهرسازی", "./data/log_r_نفت.txt")
|
||||
|
||||
# text="قانون بلدیه"
|
||||
#text = text.replace("\u00a0", " ") # No Break space
|
||||
# text="قانون تشکیلات، وظایف و انتخابات شوراهای اسلامی کشور و انتخاب شهرداران مصوب 1 /3 /1375 و اصلاحات بعدی آن"
|
||||
# text="ائیننامه[آئین نامه] اجرای مالیات اراضی بایر موضوع تبصره 4 ماده 214 قانون مالیاتهای مستقیم مصوب 28ر12ر1345"
|
||||
# text="قانون بودجه سال 1369کل کشور"
|
||||
|
||||
# text="قانون تشکیلات، وظایف و انتخابات شوراهای اسلامی کشور و انتخاب شهرداران مصوب 1/3/1375 و اصلاحات بعدی آن"
|
||||
text="قانون بودجه سال 1369کل کشور"
|
||||
|
||||
text="پیوست 3>متن >ضمیمه 1>قسمت الف>فصل پنجم>جدول الف-1-1-1 /5"
|
||||
# searchSectionId(elastic, "mj_qa_section", "qq240970", text )
|
||||
|
||||
# text = text_normalizer.date_correction(text)
|
||||
# text = re.sub(r"(\d+?) /(\d+?) /(\d+?)", r"\1/\2/\3", text)
|
||||
# text = re.sub(r"(\d)/(\d)/(\d{4})", r"0\1/0\2/\3", text)
|
||||
# print(text.split('/'))
|
||||
# text1 = re.sub(r"([^\/])(\d+)([\u0600-\u06FF\u0750-\u077F]+?)", r"\1\2 \3", text)
|
||||
# print(text)
|
||||
# print(text1)
|
||||
# print(text1==text)
|
||||
# print( searchQanonId(elastic, text) )
|
||||
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
import json
|
||||
import requests
|
||||
import pyodbc
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from html import escape
|
||||
import json
|
||||
from decimal import Decimal
|
||||
import datetime
|
||||
import time
|
||||
from app.common.config_base import *
|
||||
from app.common.general_functions import *
|
||||
from app.common.text_normalizer import TextNormalizer
|
||||
from app.common.elastic_helper import ElasticHelper
|
||||
|
||||
##--------------------------------
|
||||
# لازم است این فیلد پرشود تا مجلس بتواند دسترسی تغییر داشته باشد
|
||||
##------------------------------------
|
||||
|
||||
def fill_doc_state( elastic, index_name_i, index_name_o, file_log) :
|
||||
body = {
|
||||
"sort": [
|
||||
{"qanon_id": {"order": "asc"}},
|
||||
{"child_order": {"order": "asc"}}
|
||||
]
|
||||
}
|
||||
|
||||
try:
|
||||
list = elastic.es_iterate_all_documents(index_name_i, body)
|
||||
except Exception as e:
|
||||
save_error(0, e)
|
||||
count = 0
|
||||
text_normal = TextNormalizer()
|
||||
result = {}
|
||||
result_new = {}
|
||||
total_count = 0
|
||||
bulk_data = []
|
||||
bulk_data_section = []
|
||||
|
||||
t1= datetime.datetime.now()
|
||||
print(t1)
|
||||
|
||||
file = open(file_log, 'a+', encoding='utf8')
|
||||
i = 0
|
||||
doc_tags=[]
|
||||
qanon_id=0
|
||||
prev_qanon_id = 0
|
||||
siyasatha_list = ["qq85859", "qq260090", "qq121403", "qq222873", "qq198729", "qq121413", "qq118359", "qq305009", "qq118373", "qq286578"]
|
||||
for mentry in list:
|
||||
|
||||
entry = mentry["source"]
|
||||
id = entry.get("id", "")
|
||||
qanon_id = entry.get("qanon_id", "")
|
||||
qanon_title = entry.get("qanon_title", "")
|
||||
nlp_tags = entry.get("nlp_tags", [])
|
||||
|
||||
ref_state = entry.get("ref_state", [])
|
||||
# title_type = entry.get("title_type", "عادی")
|
||||
title_type = getTitleType(qanon_title)
|
||||
|
||||
if prev_qanon_id == 0 :
|
||||
prev_qanon_id = qanon_id
|
||||
elif prev_qanon_id != qanon_id :
|
||||
data={
|
||||
'_id':prev_qanon_id,
|
||||
'_source' : {
|
||||
'doc_tags': doc_tags
|
||||
}
|
||||
}
|
||||
body = {
|
||||
"script": {
|
||||
"source": "ctx._source.doc_tags=params.doc_tags",
|
||||
"lang": "painless",
|
||||
"params" : { 'doc_tags' : doc_tags }
|
||||
},
|
||||
"query": {
|
||||
"term": {
|
||||
"qanon_id": { "value" : prev_qanon_id }
|
||||
}
|
||||
}
|
||||
}
|
||||
elastic.updateByQueryIndex(index_name_i, body)
|
||||
bulk_data.append(data)
|
||||
doc_tags=[]
|
||||
|
||||
if len(ref_state) > 0 :
|
||||
if "مهذب" not in doc_tags :
|
||||
doc_tags.append("مهذب")
|
||||
elif title_type not in ["عادی", "اصلی", "موافقتنامه", "بودجه"] :
|
||||
if "تبعی" not in doc_tags :
|
||||
doc_tags.append("تبعی")
|
||||
elif title_type in ["عادی", "اصلی", "موافقتنامه", "بودجه"] :
|
||||
if "اصلی" not in doc_tags :
|
||||
doc_tags.append("اصلی")
|
||||
|
||||
|
||||
if qanon_id in siyasatha_list :
|
||||
if "سیاستهای کلی" not in doc_tags :
|
||||
doc_tags.append("سیاستهای کلی")
|
||||
|
||||
if "qanon_usefull" in nlp_tags :
|
||||
if "پرکاربرد" not in doc_tags :
|
||||
doc_tags.append("پرکاربرد")
|
||||
|
||||
prev_qanon_id = qanon_id
|
||||
i=i+1
|
||||
|
||||
|
||||
if len(bulk_data) >= 10:
|
||||
total_count += len(bulk_data)
|
||||
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + qanon_id)
|
||||
elastic.updateBulkList(bulk_data, index_name_o)
|
||||
bulk_data = []
|
||||
|
||||
if prev_qanon_id != 0:
|
||||
data={
|
||||
'_id':prev_qanon_id,
|
||||
'_source' : {
|
||||
'doc_tags': doc_tags
|
||||
}
|
||||
}
|
||||
body = {
|
||||
"script": {
|
||||
"source": "ctx._source.doc_tags=params.doc_tags",
|
||||
"lang": "painless",
|
||||
"params" : { 'doc_tags' : doc_tags }
|
||||
},
|
||||
"query": {
|
||||
"term": {
|
||||
"qanon_id": { "value" : prev_qanon_id }
|
||||
}
|
||||
}
|
||||
}
|
||||
elastic.updateByQueryIndex(index_name_i, body)
|
||||
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=' + qanon_id )
|
||||
elastic.updateBulkList(bulk_data, index_name_o)
|
||||
|
||||
file.close()
|
||||
print('end success import_law_section_to_elastic')
|
||||
t2= datetime.datetime.now()
|
||||
print(t2-t1)
|
||||
|
||||
|
||||
load_dotenv(dotenv_path='config.env')
|
||||
|
||||
host=os.getenv('HOST_160')
|
||||
password=os.getenv('HOST_160_PASS')
|
||||
|
||||
path_base_map = "G:/Project2/gateway2_main/repo/_other/"
|
||||
elastic = ElasticHelper(host, password, "elastic", path_base_map)
|
||||
|
||||
|
||||
# print(dict_effected_sections)
|
||||
|
||||
# try:
|
||||
# item = elastic.get_doctument("mj_qa_section", "3223423")
|
||||
# except Exception as e:
|
||||
# print(e)
|
||||
# print(type(e).__name__)
|
||||
# print(str(e))
|
||||
|
||||
fill_doc_state( elastic, "mj_qa_section", "mj_qa_qanon", "./output/log_r1.txt")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user