From b005ef64ea3fecbce81851a8d084fcb390763d64 Mon Sep 17 00:00:00 2001 From: hsafaei Date: Thu, 18 Sep 2025 15:48:59 +0330 Subject: [PATCH] refactor --- app/common/text_normalizer.py | 4 +- app/for_ai/ai_json_move_to_index.py | 11 +-- app/for_ai/ai_repair_data.py | 119 ++++++++++++++++++++++++++++ مثالهای اجرا.txt | 1 + 4 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 app/for_ai/ai_repair_data.py create mode 100644 مثالهای اجرا.txt diff --git a/app/common/text_normalizer.py b/app/common/text_normalizer.py index f83dc9a..b578dbc 100644 --- a/app/common/text_normalizer.py +++ b/app/common/text_normalizer.py @@ -75,8 +75,7 @@ class TextNormalizer: text = re.sub(r"(\n)+", "\n", text) # نرمال کردن حروف اختلافی عربی و فارسی - text = text.replace("ي", "ی") - text = text.replace("ك", "ک") + text = self.yeke_normal(text) return text @@ -279,6 +278,7 @@ class TextNormalizer: text = re.sub(r'([\u0600-\u06FF])ـ([\u0600-\u06FF])', r'\1\2', text) # حذف حروف کشیده text = re.sub(r'([\u0600-\u06FF])ـ', r'\1', text) # حذف حروف کشیده + text = text.strip("\u200C").strip() return text diff --git a/app/for_ai/ai_json_move_to_index.py b/app/for_ai/ai_json_move_to_index.py index 0ac37ce..6a64b8b 100644 --- a/app/for_ai/ai_json_move_to_index.py +++ b/app/for_ai/ai_json_move_to_index.py @@ -157,11 +157,7 @@ def moveClassifierAiCodes(elastic, index_name_o, data): #---------------------------------------------- for label_doc in ai_codes: - label = label_doc["label"] - label = re.sub(r"\u00AD|\u00AC", "\u200C", label) # نیم اسپیس خاص - label = label.replace(" ", " ") - label = label.replace("ـ", "") - label = label.strip().strip("\u200C") + label = text_normal.normalize_qanon(label_doc["label"]) label_doc["label"] = label if label_doc["score"] >= 1 : label_doc["score"] = label_doc["score"] / 100 @@ -170,10 +166,7 @@ def moveClassifierAiCodes(elastic, index_name_o, data): ai_code["score"] = ai_code["score"] / 100 label = ai_code["label"] - label = re.sub(r"\u00AD|\u00AC", "\u200C", label) # نیم اسپیس خاص - label = label.replace(" ", " ") - label = label.replace("ـ", "") - label = label.strip().strip("\u200C") + label = text_normal.normalize_qanon(ai_code["label"]) ai_code["label"] = label #---------------------------------------------- diff --git a/app/for_ai/ai_repair_data.py b/app/for_ai/ai_repair_data.py new file mode 100644 index 0000000..4356948 --- /dev/null +++ b/app/for_ai/ai_repair_data.py @@ -0,0 +1,119 @@ +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") + + diff --git a/مثالهای اجرا.txt b/مثالهای اجرا.txt new file mode 100644 index 0000000..283d7b1 --- /dev/null +++ b/مثالهای اجرا.txt @@ -0,0 +1 @@ +G:/Project2/NLP/SqlToElasticMajles/.env/Scripts/python.exe -m app.for_ai.ai_json_move_to_index.py \ No newline at end of file