refactor codes and names

This commit is contained in:
hsafaei
2026-02-12 11:19:58 +03:30
parent 56084b0c8e
commit 9c99d816db
25 changed files with 395 additions and 2360 deletions
+5 -24
View File
@@ -9,30 +9,11 @@ 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 getTitleType
from app.common.general_functions import getTitleType, get_files
from app.common.text_normalizer import TextNormalizer
text_normal = TextNormalizer()
def get_files(directory, pattern_file=r"output_(\d+)\.json$"):
pattern = None
if pattern_file :
pattern = re.compile(pattern_file)
max_num = 0
list_file = []
for filename in os.listdir(directory):
if pattern :
match = pattern.match(filename)
if match:
list_file.append(directory+"/"+filename)
else:
list_file.append(directory+"/"+filename)
return list_file
def parse_and_update_code(file_path, elastic, index_name_o = "mj_qa_section", update_to_elastic=False, by_ai_code = False):
try:
@@ -375,7 +356,7 @@ def genrate_list_negative(paths=[],list_minues=[]):
for path in paths :
print('*', 100)
print('******** ', path)
for file in get_files(path):
for file in get_files(path, r"output_(\d+)\.json$"):
print(file)
list_prccessed_negative_file = parse_and_update_code(file, elastic, "mj_qa_section", False)
list_prccessed_negative.extend(list_prccessed_negative_file)
@@ -402,7 +383,7 @@ def import_codes_to_elastic(elastic, all_codes, by_ai_code, paths=[], list_minue
for path in paths :
print('*', 100)
print('******** ', path)
for file in get_files(path):
for file in get_files(path, r"output_(\d+)\.json$"):
print(file)
if 'data_h100_full' in path : # data_h100_full_1 , ..., data_h100_full_4
list_prccessed_negative_file = parse_and_update_multi_code(file, elastic, all_codes, "mj_qa_section", True, by_ai_code)
@@ -427,7 +408,7 @@ def parse_notvalid_multi_codes(paths, all_codes=[], file_not_valid="processed_id
for path in paths :
print('*', 100)
print('******** ', path)
for file in get_files(path):
for file in get_files(path, r"output_(\d+)\.json$"):
print(file)
data = []
with open(file, 'r', encoding="utf8") as f:
@@ -471,7 +452,7 @@ def extract_sectionid_in_files(paths, sections=[], file_result="section_id_files
for path in paths :
print('*', 100)
print('******** ', path)
for file in get_files(path):
for file in get_files(path, r"output_(\d+)\.json$"):
print(file)
data = []
with open(file, 'r', encoding="utf8") as f:
@@ -115,3 +115,22 @@ col_index = {
### --- درج در الاستیک نمی کند update_elastic = False
# run_codes_to_elastic_directory( elastic, "./data/tcodes/corrected_majles/not_edit3", "./data/tcodes/corrected_majles/not_edit3/result", all_codes, False, True, col_index, False)
# run_codes_extract_directory("./data/tcodes/corrected_majles/not_edit3/result","./data/tcodes/corrected_majles/not_edit3/result_json",all_codes,col_index,True)
col_index = {
"section_title" : 0,
"ts_date" : 1,
"qanon_title" : 2,
"code_state" : 3,
"code_title" : 4,
"row_number" : 5,
"qanon_id" : 6,
"section_id" : 7,
"state_match" : 8,
}
### --- درج در الاستیک نمی کند update_elastic = False
# run_codes_to_elastic_directory( elastic, "./data/tcodes/corrected_majles/not_edit4", "./data/tcodes/corrected_majles/not_edit4/result", all_codes, False, True, col_index, False)
run_codes_extract_directory("./data/tcodes/corrected_majles/not_edit4/result","./data/tcodes/corrected_majles/not_edit4/result_json",all_codes,col_index,True)
-374
View File
@@ -1,374 +0,0 @@
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.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")
# host = os.getenv("HOST_112")
# password = os.getenv("HOST_112_PASS")
host = os.getenv("HOST_H102")
password = os.getenv("HOST_H102_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)
# -------------------------------------
# ----- یک بار فقط
# ----- پشتیبان گیری از داده های جدید داتیک
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_rg_section_temp", "mj_rg_section_doctic_14040824")
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_section_temp", "mj_qa_section_doctic_14040824")
# 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")
# ------------------------------------------ 1 ---------------------------------------------------------------------
# ###### اصلاح تاریخ فارسی به ذخیره سازی استاندارد date
# ---------------------------------------------------------------------------------------------------------------
# -------------------------------------
# ----- mj_qa_qanon
# ----- حذف فیزیکی mj_qa_qanon
#DELETE mj_qa_qanon
file_path = "G:/Project2/160_dotic/majles_14040926/mj_qa_qanon.zip"
index_name = "mj_qa_qanon"
# elastic.createIndex( 'mj_qa_qanon', 'majles', 'mj_qa_qanon', path_base_map+ 'majles/'+'mj_qa_qanon.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_14040926/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.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_14040926/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.json' )
# restore_correct_date_to_elastic(elastic, file_path, index_name, "G:/Project2/160_dotic/date_log_error_qasection.txt" )
# # ---فقط برای یک بار
# # ---- حذف یک مورد اشتباه با عنوان خالی
# # POST mj_qa_section/_delete_by_query
# # {
# # "query": {
# # "term": {
# # "qanon_id": {
# # "value": "df_6914887106dba"
# # }
# # }
# # }
# # }
# -------------------------------------
# ----- 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.json')
# restore_correct_date_to_elastic(elastic, file_path, "mj_rg_section", "G:/Project2/160_dotic/date_log_error_rgsection.txt" )
# -------------------------------------
# ----- mj_mashruh
file_path = "G:/Project2/160_dotic/mj_mashruh.zip"
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_mashruh", "mj_mashruh")
# elastic.createIndex( 'mj_mashruh', 'majles', 'mj_mashruh', path_base_map+ 'majles/'+'mj_mashruh.json' )
# restore_correct_date_to_elastic(elastic, file_path, "mj_mashruh", "G:/Project2/160_dotic/date_log_error_mashruh.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)
# elastic.backupIndexToZipfile("G:/Project2/160_dotic/majles_14040926", "mj_df_qanon", "mj_df_qanon_1_date")
# --------------------------------------------- 3 ------------------------------------------------------------------
# --- درج داده های جدید مقررات و آراء در فهرست همه
# ---------------------------------------------------------------------------------------------------------------
# --- موقت
# elastic.backupIndexToZipfile("G:/Project2/160_dotic/majles_14040926", "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/majles_14040926", "mj_qa_qanon", "mj_qa_qanon_2_base")
# -------------------------------------
# -------------------------------------
# --- درج داده های جدید مقرره
# import_data_new_qaqanon(elastic, "G:/Project2/160_dotic/dotic_14040824/mj_qa_qanon_doctic_14040824.zip")
# -------------------------------------
# ----- انتقال داده های آراء به
#--- بخاطر لزوم تغییر روش کار، لازم شد محتوای آراء به اینجا منتفل شود
#--- البته بقیه فراداده قبلا منتقل شده بودند
file_path = 'G:/Project2/160_dotic/dotic_14040824/mj_qa_opinion_doctic_14040824.zip'
# import_content_opinion_to_qaqanon(elastic, file_path, "mj_qa_qanon" )
# # -------------------------------------
# # ----- انتقال داده های مشروح مذاکرات به فهرست همه قوانین
# ??????فعلا انجام نشده # file_path = 'G:/Project2/160_dotic/mj_mashruh.zip'
# --- موقت
# elastic.backupIndexToZipfile("G:/Project2/160_dotic/majles_14040926", "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\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\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\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/majles_14040926", "mj_qa_qanon", "mj_qa_qanon_4_base_excel")
#---------تنظیم شد mj_qa_qanon_new.json
# #----------------------------
# PUT mj_qa_qanon/_settings
# {
# "index" : {
# "max_result_window" : 200000
# }
# }
# .....
# --------------
# ---- تذکر فرانت و بک
# -- اصلاح ساختار اسکیما نماش و تغییرات برای ساختار تاریخ تغییر یافته و تست و بررسی
# .....
# .....
# ------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------
#------------------------------------
#--- mj_qa_section
#elastic.backupIndexToZipfile("G:/Project2/160_dotic/majles_14040926", "mj_qa_section", "mj_qa_section_1_base")
# بررسی پر بودن main_type مجلس برای نوع اصلی/تبعی/مهذب # و تغییر به mom_type
#### schema changed --> qasection_mom_type
# # POST mj_qa_section/_update_by_query
# body = {
# "query": {"terms": {"main_type": ["اصلی", "تبعی"]}},
# "script": {
# "source": """
# ctx._source.mom_type = ctx._source.main_type;
# ctx._source.main_type = 'قانون'
# """,
# "lang": "painless"
# }
# }
# elastic.updateByQueryIndex("mj_qa_section", body)
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#--- پشتیبان گیری نهایی برای انتقال
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_mashruh", "mj_mashruh")
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_qanon", "mj_qa_qanon")
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_df_qanon", "mj_df_qanon")
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_section", "mj_qa_section")
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_rg_section", "mj_rg_section")
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------
# elastic.createIndex( 'mj_qa_section-m1', 'majles', 'mj_qa_section', path_base_map+ 'majles/'+'mj_qa_section.json' )
# elastic.restorFileToElastic2('G:/Project2/160_dotic/mj_qa_section-m1.zip', 'mj_qa_section-m1', 'majles', False)
#-------------------------------------------------------------------------------------------------
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_mashruh", "mj_mashruh")
# elastic.createIndex( 'mj_mashruh', 'majles', 'mj_mashruh', path_base_map+ 'majles/'+'mj_mashruh.json' )
# elastic.restorFileToElastic2('G:/Project2/160_dotic/mj_mashruh.zip', 'mj_mashruh', 'majles', False, 'mj_mashruh')
# 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.createIndex( 'mj_qa_section', 'majles', 'mj_qa_section', path_base_map+ 'majles/'+'mj_qa_section.json' )
# elastic.restorFileToElastic2('G:/Project2/160_dotic/mj_qa_section.zip', 'mj_qa_section', 'majles', False, 'mj_qa_section')
# elastic.createIndex( 'mj_df_qanon', 'majles', 'mj_df_qanon', path_base_map+ 'majles/'+'mj_df_qanon.json' )
# elastic.restorFileToElastic2('G:/Project2/160_dotic/majles_14040926/mj_df_qanon_1_date.zip', 'mj_df_qanon', 'majles', False, 'mj_df_qanon')
# elastic.createIndex( 'mj_qa_qanon', 'majles', 'mj_qa_qanon', path_base_map+ 'majles/'+'mj_qa_qanon.json' )
# elastic.restorFileToElastic2('G:/Project2/160_dotic/mj_qa_qanon_3_base_dotic.zip', 'mj_qa_qanon', 'majles', False, 'mj_qa_qanon')
# elastic.backupIndexToZipfile("G:/Project2/102_gpu", "mj_qa_semantic_section", "mj_qa_semantic_section")
# elastic.backupIndexToZipfile("G:/Project2/102_gpu", "mj_qa_section_rule", "mj_qa_section_rule")
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_mashruh", "mj_mashruh")
# elastic.backupIndexToZipfile("G:/Project2/160_dotic", "mj_qa_section", "mj_qa_section")
-55
View File
@@ -1,55 +0,0 @@
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")
# host = os.getenv("HOST_112")
# password = os.getenv("HOST_112_PASS")
# host = os.getenv("HOST_H102")
# password = os.getenv("HOST_H102_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)
-8
View File
@@ -1,8 +0,0 @@
file_path = r'C:\Users\TAVASI-3\Desktop\init_mahdi\project\dataParsers\data\FLC\cleaned\temp.json'
with open(file_path, "r", encoding="utf-8") as f:
text = f.read()
import re
matches = re.findall(r'persian', text)
print("تعداد دقیق:", len(matches))