monir , majles update2

This commit is contained in:
hsafaei
2025-12-09 19:55:37 +03:30
parent 9bb61a60b2
commit f4e8832e61
13 changed files with 1492 additions and 222 deletions
+67
View File
@@ -0,0 +1,67 @@
import numbers
import os
from dotenv import load_dotenv
from elasticsearch import Elasticsearch, helpers
import json
import re
import zipfile
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
from app.common.elastic_helper import ElasticHelper
from app.common.general_functions import *
from app.common.text_normalizer import TextNormalizer
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 (
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 *
text_normal = TextNormalizer()
load_dotenv(dotenv_path="config.env")
host = os.getenv("HOST_160")
password = os.getenv("HOST_160_PASS")
# print( "---------------->>>>>>>>>>>>>>>", host )
# host=os.getenv('HOST_113')
# password=os.getenv('HOST_113_PASS')
print(".... majles_codes_update_steps ......", host)
path_base_map = "G:/Project2/gateway_php/repo/_other/"
elastic = ElasticHelper(host, password, "elastic", path_base_map)
# -------------------------------------
# ------ استخراج برخی کدهای باقیمانده که اکسل آنها از قبل موجود هست و تولید فایل مناسب برای درج فایل محور در بانک مجلس
# ---
all_codes = []
with open("./data/codes49.json", "r", encoding="utf8") as f:
codes_1 = json.load(f)
all_codes = codes_1
col_index = {
"row_number": 0,
"section_title": 1,
"ts_date": 2,
"qanon_title": 3,
"code_state": 4,
"code_label": 5,
"qanon_id": 6,
"section_id": 7,
"state_match": 8,
}
# run_codes_to_elastic_directory( elastic, "./data/tcodes/corrected_majles/not_edit2", "./data/tcodes/corrected_majles/not_edit2/result", all_codes, False, True, col_index, False)
# run_codes_extract_directory("./data/tcodes/corrected_majles/not_edit2/result","./data/tcodes/corrected_majles/not_edit2/result_json",all_codes,col_index,True)
@@ -44,91 +44,7 @@ path_base_map = "G:/Project2/gateway_php/repo/_other/"
elastic = ElasticHelper(host, password, "elastic", path_base_map)
# -------------------------------------
# --- مواردی از قانونهای جدید که فیلد زیر را ندارند باید اصلاح شوند
# --- main_type
# POST mj_qa_qanon/_update_by_query
body = {
"query": {"bool": {"must_not": [{"exists": {"field": "main_type"}}]}},
"script": {
"source": """
ctx._source.main_type = 'قانون'
""",
"lang": "painless",
},
}
# elastic.updateByQueryIndex("mj_qa_qanon", body)
# POST mj_qa_qanon/_update_by_query
body = {
"query": {"terms": {"main_type": ["اصلی", "تبعی"]}},
"script": {
"source": """
ctx._source.sub_type = ctx._source.main_type;
ctx._source.main_type = 'قانون'
""",
"lang": "painless",
},
}
# elastic.updateByQueryIndex("mj_qa_qanon", body)
# PUT mj_df_qanon/_mapping
# {
# "properties":{
# "mom_type": {
# "type": "keyword"
# },
# "main_type_key": {
# "type": "keyword"
# }
# }
# }
# POST mj_df_qanon/_update_by_query
body = {
"query": {"terms": {"main_type": ["اصلی", "تبعی"]}},
"script": {
"source": """
ctx._source.mom_type = ctx._source.main_type;
ctx._source.main_type = 'قانون';
ctx._source.main_type_key = 'qasection';
""",
"lang": "painless",
},
}
# elastic.updateByQueryIndex("mj_df_qanon", body)
# -------------------------------------
# ------ استخراج برخی کدهای باقیمانده که اکسل آنها از قبل موجود هست و تولید فایل مناسب برای درج فایل محور در بانک مجلس
# ---
all_codes = []
with open("./data/codes49.json", "r", encoding="utf8") as f:
codes_1 = json.load(f)
all_codes = codes_1
col_index = {
"row_number": 0,
"section_title": 1,
"ts_date": 2,
"qanon_title": 3,
"code_state": 4,
"code_label": 5,
"qanon_id": 6,
"section_id": 7,
"state_match": 8,
}
run_codes_to_elastic_directory( elastic, "./data/tcodes/corrected_majles/not_edit2", "./data/tcodes/corrected_majles/not_edit2/result", all_codes, False, True, col_index, False)
run_codes_extract_directory("./data/tcodes/corrected_majles/not_edit2/result","./data/tcodes/corrected_majles/not_edit2/result_json",all_codes,col_index,True)
# -------------------------------------
# ----- یک بار فقط
# ----- پشتیبان گیری از داده های جدید داتیک
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_rg_section_temp", "mj_rg_section_doctic_14040824")
@@ -136,110 +52,197 @@ run_codes_extract_directory("./data/tcodes/corrected_majles/not_edit2/result",".
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_opinion_temp", "mj_qa_opinion_doctic_14040824")
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_qanon_temp", "mj_qa_qanon_doctic_14040824")
# -------------------------------------
# ----- ترمیم داده های تاریخ ایندکس قبلی
# -- qaqanon
file_path = "G:/Project2/160_dotic/mj_qa_qanon_112.zip"
# ------------------------------------------ 1 ---------------------------------------------------------------------
# ###### اصلاح تاریخ فارسی به ذخیره سازی استاندارد date
# ---------------------------------------------------------------------------------------------------------------
# -------------------------------------
# ----- mj_qa_qanon
# ----- حذف فیزیکی mj_qa_qanon
#DELETE mj_qa_qanon
file_path = "G:/Project2/160_dotic/majles_14040918/mj_qa_qanon.zip"
index_name = "mj_qa_qanon"
# elastic.createIndex( 'mj_qa_qanon', 'majles', 'mj_qa_qanon')
###### اصلاح تاریخ فارسی به ذخیره سازی استاندارد date
# restore_correct_date_to_elastic(elastic, file_path, index_name, "G:/Project2/160_dotic/date_log_error_qaqanon.txt" )
#elastic.createIndex( 'mj_qa_qanon', 'majles', 'mj_qa_qanon', path_base_map+ 'majles/'+'mj_qa_qanon_new.json' )
#restore_correct_date_to_elastic(elastic, file_path, index_name, "G:/Project2/160_dotic/date_log_error_qaqanon.txt" )
# -------------------------------------
# ----- mj_df_qanon
# ----- حذف فیزیکی mj_df_qanon
#DELETE mj_df_qanon
file_path = "G:/Project2/160_dotic/majles_14040918/mj_df_qanon.zip"
index_name = "mj_df_qanon"
# elastic.createIndex( 'mj_df_qanon', 'majles', 'mj_df_qanon', path_base_map+ 'majles/'+'mj_df_qanon_new.json' )
# restore_correct_date_to_elastic(elastic, file_path, index_name, "G:/Project2/160_dotic/date_log_error_dfqanon.txt" )
# -------------------------------------
# ----- mj_qa_section
#### ------>> expire_date, ts_date, ref_state.qanon_date, ref_state.effect_date
# ----- حذف فیزیکی mj_qa_section
#DELETE mj_qa_section
file_path = "G:/Project2/160_dotic/majles_14040918/mj_qa_section.zip"
index_name = "mj_qa_section"
# elastic.createIndex( 'mj_qa_section', 'majles', 'mj_qa_section', path_base_map+ 'majles/'+'mj_qa_section_new.json' )
# restore_correct_date_to_elastic(elastic, file_path, index_name, "G:/Project2/160_dotic/date_log_error_qasection.txt" )
# -------------------------------------
# ----- mj_rg_section
file_path = "G:/Project2/160_dotic/mj_rg_section_doctic_14040824.zip"
# elastic.createIndex( 'mj_rg_section', 'majles', 'mj_rg_section', path_base_map+ 'majles/'+'mj_qa_section_new.json')
# restore_correct_date_to_elastic(elastic, file_path, "mj_rg_section", "G:/Project2/160_dotic/date_log_error_rgsection.txt" )
# --------------------------------------------- 1 ------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------
# --- مواردی از قانونهای جدید که فیلد زیر را ندارند باید اصلاح شوند
# --- main_type
# # POST mj_qa_qanon/_update_by_query
# body = {
# "query": {"bool": {"must_not": [{"exists": {"field": "main_type"}}]}},
# "script": {
# "source": """
# ctx._source.main_type = 'قانون'
# """,
# "lang": "painless",
# },
# }
# elastic.updateByQueryIndex("mj_qa_qanon", body)
# # POST mj_qa_qanon/_update_by_query
# body = {
# "query": {"terms": {"main_type": ["اصلی", "تبعی"]}},
# "script": {
# "source": """
# ctx._source.sub_type = ctx._source.main_type;
# ctx._source.main_type = 'قانون'
# """,
# "lang": "painless",
# },
# }
# elastic.updateByQueryIndex("mj_qa_qanon", body)
# #----------------------------------------------
# #---- در مپ إعمال شد و نیاز نیست دیگه
# # PUT mj_df_qanon/_mapping
# # {
# # "properties":{
# # "mom_type": {
# # "type": "keyword"
# # },
# # "main_type_key": {
# # "type": "keyword"
# # }
# # }
# # }
# #----------------------------------------------
# # POST mj_df_qanon/_update_by_query
# body = {
# "query": {"terms": {"main_type": ["اصلی", "تبعی"]}},
# "script": {
# "source": """
# ctx._source.mom_type = ctx._source.main_type;
# ctx._source.main_type = 'قانون';
# ctx._source.main_type_key = 'qasection';
# """,
# "lang": "painless",
# },
# }
# elastic.updateByQueryIndex("mj_df_qanon", body)
# --------------------------------------------- 3 ------------------------------------------------------------------
# --- درج داده های جدید مقررات و آراء در فهرست همه
# ---------------------------------------------------------------------------------------------------------------
# --- موقت
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_qanon", "mj_qa_qanon_1_date")
# -- حذف مقرره های قبلی
body = {"query": {"term": {"main_type": {"value": "مقرره"}}}}
# elastic.deleteByQueryIndex("mj_qa_qanon", body)
# --- موقت
#elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_qanon", "mj_qa_qanon_2_base")
# -------------------------------------
# ----- موقتی برای تستهای از این مرحله به بعد یه پشتیبان تمیز باشه ...
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_qanon", "mj_qa_qanon_base")
# -------------------------------------
# --- درج داده های جدید مقرره
# import_data_new_qaqanon(elastic, "G:/Project2/160_dotic/mj_qa_qanon_doctic_14040824.zip")
# ----- موقتی برای تستهای از این مرحله به بعد یه پشتیبان تمیز باشه ...
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_qanon", "mj_qa_qanon_base_dotic")
# elastic.createIndex( 'mj_qa_qanon', 'majles', 'mj_qa_qanon')
# elastic.restorFileToElastic2('G:/Project2/160_dotic/mj_qa_qanon_base_dotic.zip', 'mj_qa_qanon', 'majles', False, 'mj_qa_qanon')
# -------------------------------------
# -------------------------------------
# -------------------------------------
# ----- انتقال داده های آراء به
#### بخاطر لزوم تغییر روش کار، لازم شد محتوای آراء به اینجا منتفل شود
#### البته بقیه فراداده قبلا منتقل شده بودند
# file_path = 'G:/Project2/160_dotic/mj_qa_opinion_doctic_14040824.zip'
# index_name = "mj_qa_qanon"
# import_content_opinion_to_qaqanon(elastic, file_path, index_name )
# --- موقت
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_qanon", "mj_qa_qanon_3_base_dotic")
# --------------------------------------------- 4 ------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------
file_before = "G:/Project2/160_dotic/duplicate_before.json"
file_after = "G:/Project2/160_dotic/duplicate_after.json"
file_tasmim = "G:/Project2/160_dotic/duplicate_tasmim.json"
# #------------------------------------------
# duplicate_list = {}
# file_excel = r"G:\Project2\NLP\dataParsers\data\qavanin_files\qavanin_output4.xlsx"
# import_excel_before_to_elastic(elastic, file_excel, "mj_qa_qanon", duplicate_list)
# with open(file_before, 'w', encoding='utf8') as f :
# json.dump(duplicate_list, f, ensure_ascii=False, indent=4)
# elastic.createIndex( 'mj_qa_qanon', 'majles', 'mj_qa_qanon', path_base_map+ 'majles/'+'mj_qa_qanon_new.json' )
# elastic.restorFileToElastic2('G:/Project2/160_dotic/mj_qa_qanon_3_base_dotic.zip', 'mj_qa_qanon', 'majles', False, 'mj_qa_qanon')
# # #------------------------------------------
duplicate_list = {}
file_excel = r"G:\Project2\dataParsers\data\qavanin_files\qavanin_output4.xlsx"
import_excel_before_to_elastic(elastic, file_excel, "mj_qa_qanon", duplicate_list)
with open(file_before, 'w', encoding='utf8') as f :
json.dump(duplicate_list, f, ensure_ascii=False, indent=4)
# # # #------------------------------------------
# duplicate_list = {}
# with open(file_before, 'r', encoding='utf8') as f :
# duplicate_list = json.loads( f.read())
# file_excel = r"G:\Project2\NLP\dataParsers\data\qavanin_files\qavanin_output4.xlsx"
# file_excel = r"G:\Project2\dataParsers\data\qavanin_files\qavanin_output4.xlsx"
# import_excel_after_to_elastic(elastic, file_excel, "mj_qa_qanon", duplicate_list)
# with open(file_after, 'w', encoding='utf8') as f :
# json.dump(duplicate_list, f, ensure_ascii=False, indent=4)
# # #------------------------------------------
# # # #------------------------------------------
# duplicate_list = {}
# with open(file_after, 'r', encoding='utf8') as f :
# duplicate_list = json.loads( f.read())
# file_excel = r"G:\Project2\NLP\dataParsers\data\qavanin_files\فهرست تصمیمات قانونی.xlsx"
# file_excel = r"G:\Project2\dataParsers\data\qavanin_files\فهرست تصمیمات قانونی.xlsx"
# import_excel_tasmimat_to_elastic(elastic, file_excel, "mj_qa_qanon", duplicate_list)
# with open(file_tasmim, 'w', encoding='utf8') as f :
# json.dump(duplicate_list, f, ensure_ascii=False, indent=4)
# ----- موقتی برای تستهای از این مرحله به بعد یه پشتیبان تمیز باشه ...
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_qanon", "mj_qa_qanon_base_excel")
# elastic.createIndex( 'mj_qa_qanon', 'majles', 'mj_qa_qanon')
# elastic.restorFileToElastic2('G:/Project2/160_dotic/mj_qa_qanon_base_excel.zip', 'mj_qa_qanon', 'majles', False, 'mj_qa_qanon')
# # --- موقت
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_qanon", "mj_qa_qanon_4_base_excel")
# -------------------------------------
# ----- انتقال داده های آراء به
# -- qaqanon
# file_path = 'G:/Project2/160_dotic/mj_qa_opinion_doctic_14040824.zip'
# index_name = "mj_qa_qanon"
#### بخاطر لزوم تغییر روش کار، لازم شد محتوای آراء به اینجا منتفل شود
#### البته بقیه فراداده قبلا منتقل شده بودند
# import_content_opinion_to_qaqanon(elastic, file_path, index_name )
# -------------------------------------
# ---- اصلاح تاریخهای اجزاء طبق ساختار جدید date
# -- qasection
#### ------>> expire_date, ts_date, ref_state.qanon_date, ref_state.effect_date
file_path = "G:/Project2/160_dotic/mj_qa_section_doctic_14040824.zip"
# elastic.createIndex( 'mj_qa_section', 'majles', 'mj_qa_section')
###### اصلاح تاریخ فارسی به ذخیره سازی استاندارد date
# restore_correct_date_to_elastic(elastic, file_path, "mj_qa_section", "G:/Project2/160_dotic/date_log_error_qasection.txt" )
# ---- اصلاح تاریخهای اجزاء طبق ساختار جدید date
# -- rgsection
file_path = "G:/Project2/160_dotic/mj_rg_section_doctic_14040824.zip"
# elastic.createIndex( 'mj_rg_section', 'majles', 'mj_rg_section')
##### اصلاح تاریخ فارسی به ذخیره سازی استاندارد date
# restore_correct_date_to_elastic(elastic, file_path, "mj_rg_section", "G:/Project2/160_dotic/date_log_error_rgsection.txt" )
# .....
@@ -251,7 +254,15 @@ file_path = "G:/Project2/160_dotic/mj_rg_section_doctic_14040824.zip"
# ------------------------------------
# ------------------------------------
#--- mj_qa_section ???
# بررسی پر بودن main_type مجلس برای نوع اصلی/تبعی/مهذب
# و تغییر به mom_type
# schema ???
# 'key' => 'mom_type',
# 'label' => 'نوع اصلی/تبعی/مهذب',
# elastic.createIndex( 'mj_qa_qanon', 'majles', 'mj_qa_qanon')
# elastic.restorFileToElastic2('G:/Project2/160_dotic/mj_qa_qanon_base_excel.zip', 'mj_qa_qanon', 'majles', False, 'mj_qa_qanon')