Files
init_dataParser/app/for_ai/ai_repair_data.py
T
2025-09-18 18:25:29 +03:30

189 lines
5.7 KiB
Python

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 is_section_empty(section_path):
filtered_keys = ['فصل','موخره','امضاء','عنوان']
if '>' in section_path:
path_parts = section_path.split('>')
for key in filtered_keys:
if key in path_parts[-1]:
return True
else:
for key in filtered_keys:
if key in section_path:
return True
return False
def repair_ai_codes( elastic, index_name_i, index_name_o, file_log) :
body = {}
try:
list = elastic.es_iterate_all_documents(index_name_i, body)
except Exception as e:
save_error(0, e)
text_normal = TextNormalizer()
total_count = 0
bulk_data = []
t1= datetime.datetime.now()
print(t1)
file = open(file_log, 'a+', encoding='utf8')
i = 0
doc_tags=[]
qanon_id=0
prev_qanon_id = 0
for mentry in list:
entry = mentry["source"]
id = entry.get("id", "")
qanon_id = entry.get("qanon_id", "")
other_info = entry.get("other_info", {})
code_ai = entry.get("code-ai", {})
tcode = entry.get("tcode", {})
body = {}
history_codes = []
if other_info and is_section_empty(other_info['full_path']) :
body['code-ai'] = {}
else :
if code_ai :
history_codes.append({
"version_key": "D54_v1",
"sub_key": "main",
"label": text_normal.normalize_qanon(code_ai['label']),
"score": code_ai['score'],
})
if tcode :
for item in tcode['mains_old'] :
history_codes.append({
"version_key": "TH40_V1",
"sub_key": "main",
"label": text_normal.normalize_qanon(item)
})
for item in tcode['subs_old'] :
history_codes.append({
"version_key": "TH40_V1",
"sub_key": "sub",
"label": text_normal.normalize_qanon(item)
})
for item in tcode['mains'] :
history_codes.append({
"version_key": "TH54_V1",
"sub_key": "main",
"label": text_normal.normalize_qanon(item)
})
for item in tcode['subs'] :
history_codes.append({
"version_key": "TH54_V1",
"sub_key": "sub",
"label": text_normal.normalize_qanon(item)
})
body['tcode'] = {}
ai_code = entry.get("ai_code", {})
ai_codes = entry.get("ai_codes", [])
#----------------------------------------------
for label_doc in ai_codes:
label = text_normal.normalize_qanon(label_doc["label"])
label_doc["label"] = label
if label_doc["score"] >= 1 :
label_doc["score"] = label_doc["score"] / 100
if ai_code :
if ai_code["score"] >= 1 :
ai_code["score"] = ai_code["score"] / 100
label = ai_code["label"]
label = text_normal.normalize_qanon(ai_code["label"])
ai_code["label"] = label
#----------------------------------------------
if other_info and is_section_empty(other_info['full_path']) :
body = {
'ai_code': {},
'ai_codes': []
}
else:
if ai_code :
body['ai_code'] = ai_code
if ai_codes :
body['ai_codes'] = ai_codes
# print(history_codes)
if history_codes :
body['history_codes'] = history_codes
if not body :
continue
eid = id
data={
'_id':eid,
'_source' : body
}
bulk_data.append(data)
i=i+1
if len(bulk_data) >= 200:
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 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')
host=os.getenv('HOST_113')
password=os.getenv('HOST_113_PASS')
path_base_map = "G:/Project2/gateway2_main/repo/_other/"
elastic = ElasticHelper(host, password, "elastic", path_base_map)
repair_ai_codes( elastic, "mj_qa_section", "mj_qa_section", "./output/log__ai_r1.txt")