rules_labels

This commit is contained in:
init_mahdi
2026-02-02 19:26:02 +03:30
parent 8a62498106
commit a0c00f4b77
+117 -9
View File
@@ -1,4 +1,4 @@
import mariadb # import mariadb
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os
import datetime import datetime
@@ -9,6 +9,7 @@ import zipfile
import sys import sys
import time import time
from tqdm import tqdm from tqdm import tqdm
from pydantic import BaseModel
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
@@ -16,12 +17,31 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from app.common.elastic_helper import ElasticHelper from app.common.elastic_helper import ElasticHelper
from app.common.general_functions import * from app.common.general_functions import *
from app.common.text_normalizer import TextNormalizer from app.common.text_normalizer import TextNormalizer
from app.common.data_helper import DataHelper
from app.majles_options import * from app.majles_options import *
load_dotenv(dotenv_path="config.env") class OutputLabels(BaseModel):
class Subject(BaseModel):
subject_type: str
subject_detail: str
class Clause(BaseModel):
exists: bool
text: str
subject: Subject
action: str
object: str
condition: Clause
rule_verdict: str
scope: Clause | None
exception: Clause | None
load_dotenv(dotenv_path=r"D:\init_mahdi\dataParsers\config.env")
host = os.getenv("HOST_160") host = os.getenv("HOST_160")
password = os.getenv("HOST_160_PASS") password = os.getenv("HOST_160_PASS")
@@ -33,16 +53,80 @@ password = os.getenv("HOST_160_PASS")
# print(password) # print(password)
path_base_map = ( # path_base_map = (
"G:\Project2\gateaway_py\elastic_backend/elastic_backend/app/schemas/mappings/" # r"G:/Project2/gateaway_py/elastic_backend/elastic_backend/app/schemas/mappings/"
) # )
elastic = ElasticHelper(host, password, "elastic", path_base_map) elastic = ElasticHelper(es_url=host, es_pass=password, es_user="elastic")
text_normalize = TextNormalizer() text_normalize = TextNormalizer()
log_file = None log_file = None
def convert_OutputLabels_to_es_schema(item: OutputLabels):
return {
"action": item.action,
"condition": item.condition.text,
"exception": item.exception.text if item.exception.text else None,
"object": item.object,
"rule_verdict": item.rule_verdict,
"scope": item.scope.text if item.scope.text else None,
"subject_detail": item.subject.subject_detail,
"subject_type": item.subject.subject_type,
}
def extract_json_context_id_rules_labels(
json_path,
elastic,
index_name="mj_qa_rule",
):
input_data = DataHelper.load_orjson(json_path)
print(
f'input_data {len(input_data)}'
)
bulk_data = []
total_count = 0
failed = 0
for _id1, (context_id, rule_label_data) in enumerate(input_data.items(), start=1):
print(f"context_id {context_id}")
for _id2, item in enumerate(rule_label_data, start=1):
rule_id = context_id + "-" + str(_id2)
try:
old_data = elastic.get_document(index_name, rule_id)
old_data = old_data["_source"]
item_obj = OutputLabels.model_validate(item)
new_item = convert_OutputLabels_to_es_schema(item_obj)
old_data["label_object"] = new_item
except:
failed += 1
continue
bulk_data.append(
{"_id": old_data["id"], "_source": old_data}
)
# qs648706-1
if len(bulk_data) >= 100:
total_count += len(bulk_data)
print(
"=" * 10, "to_elastic , total=" + str(total_count)
)
elastic.updateBulkList(bulk_data, index_name)
bulk_data = []
if len(bulk_data) >= 0:
total_count += len(bulk_data)
print(
"=" * 10, "to_elastic , total=" + str(total_count)
)
elastic.updateBulkList(bulk_data, index_name)
bulk_data = []
# qs648706-1
print(f"failed -=>{failed}<=-")
def extract_zip_files(elastic, filezip, file_import_ids, file_type="groups"): def extract_zip_files(elastic, filezip, file_import_ids, file_type="groups"):
bulk_data = [] bulk_data = []
@@ -83,7 +167,14 @@ def extract_zip_files(elastic, filezip, file_import_ids, file_type="groups"):
if qanon_id in jump_ids: if qanon_id in jump_ids:
continue continue
print(f"***** {file_type} : ", index_name_o, "=>", filename, " qanon_id=" , qanon_id) print(
f"***** {file_type} : ",
index_name_o,
"=>",
filename,
" qanon_id=",
qanon_id,
)
with zObject.open(filename) as file: with zObject.open(filename) as file:
data = json.loads(file.read()) data = json.loads(file.read())
@@ -350,7 +441,12 @@ def import_rules_qanon(elastic, qanon_id, data, bulk_data, total_count, index_na
res = elastic.get_document(index_name_i, context_id) res = elastic.get_document(index_name_i, context_id)
source = res["_source"] source = res["_source"]
except: except:
error = " section_mom_id not found in qa_context :" + qanon_id + ":" + context_id error = (
" section_mom_id not found in qa_context :"
+ qanon_id
+ ":"
+ context_id
)
if log_file: if log_file:
log_file.write(5 * "*" + error + "\n") log_file.write(5 * "*" + error + "\n")
continue continue
@@ -398,7 +494,9 @@ def import_rules_qanon(elastic, qanon_id, data, bulk_data, total_count, index_na
if len(bulk_data) >= 100: if len(bulk_data) >= 100:
total_count += len(bulk_data) total_count += len(bulk_data)
print("=" * 10, "to_elastic , total=" + str(total_count), ", id=" + eid) print(
"=" * 10, "to_elastic , total=" + str(total_count), ", id=" + eid
)
elastic.importBulkList(bulk_data, index_name_o) elastic.importBulkList(bulk_data, index_name_o)
bulk_data = [] bulk_data = []
@@ -443,6 +541,14 @@ if __name__ == "__main__":
# extract_zip_files(elastic, filezip, file_import_ids, data_key) # extract_zip_files(elastic, filezip, file_import_ids, data_key)
# log_file.close() # log_file.close()
# ### ----------------------------------------------
data_key = "rules_labels"
# label_object
index_name = "mj_qa_rule"
json_path = r"D:\init_mahdi\dataParsers\merge_labels.json"
extract_json_context_id_rules_labels(
elastic=elastic, index_name=index_name, json_path=json_path
)
# ### ---------------------------------------------- # ### ----------------------------------------------
# elastic.backupIndexToZipfile("G:/Project2/102_gpu", "mj_qa_context", "mj_qa_context") # elastic.backupIndexToZipfile("G:/Project2/102_gpu", "mj_qa_context", "mj_qa_context")
@@ -455,3 +561,5 @@ if __name__ == "__main__":
# elastic.restorFileToElastic2('G:/Project2/102_gpu/mj_qa_rule.zip', 'mj_qa_rule', 'majles', False, 'mj_qa_rule', mapping_file_path) # elastic.restorFileToElastic2('G:/Project2/102_gpu/mj_qa_rule.zip', 'mj_qa_rule', 'majles', False, 'mj_qa_rule', mapping_file_path)
# ### ---------------------------------------------- # ### ----------------------------------------------
# python -m dataParsers.app.import_context_rules
print(":D")