Files
init_dataParser/app/for_ai/ai_repair_data.py
T
2025-09-18 15:48:59 +03:30

120 lines
3.5 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 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", {})
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
#----------------------------------------------
body = {}
if other_info.full_path == 'عنوان' or other_info.full_path=='موخره' or other_info.full_path=='امضاء' :
body = {}
else:
if ai_code :
body['ai_code'] = ai_code
if ai_codes :
body['ai_codes'] = ai_codes
if not body :
continue
eid = id
data={
'_id':eid,
'_source' : body
}
bulk_data.append(data)
i=i+1
if len(bulk_data) >= 100:
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")