Files
init_dataParser/app/update_mj_qa_rules_persian_text.py
T
2026-02-14 14:54:52 +03:30

75 lines
2.4 KiB
Python

from dotenv import load_dotenv
import os, sys
from app.common.elastic_helper import ElasticHelper
from app.common.data_helper import DataHelper
from pydantic import BaseModel
from typing import Literal, List, Dict, Any, Optional
from collections import defaultdict
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
load_dotenv(dotenv_path=r"D:\init_mahdi\dataParsers\config.env")
host = os.getenv("HOST_160")
password = os.getenv("HOST_160_PASS")
path_schema = r"D:\init_mahdi\dataParsers\schema"
elastic = ElasticHelper(
es_url=host, es_pass=password, es_user="elastic", path_mappings=path_schema
)
def update_mj_qa_rules_translate(
out_put_index_name="mj_qa_conflict",
):
in_data = DataHelper.load_orjson(
r"D:\init_mahdi\dataParsers\finall_data_translate.json"
)
f_data = defaultdict(dict)
for i in in_data:
f_data[i["id"]].update({i["type"]: i["result"]})
bulk_data = []
failed_data = []
total_count = 0
for id, dict_item in f_data.items():
db_data = elastic.get_document(out_put_index_name, id)
_id = db_data["_id"]
_source = db_data["_source"]
conflict_detection = dict_item.get("conflict_detection", None)
if conflict_detection:
_source['conflict_detection']["description"] = conflict_detection
conflict_type_detection = dict_item.get("conflict_type_detection", None)
if conflict_type_detection:
_source['conflict_type_detection']["description"] = conflict_type_detection
bulk_data.append({"_id": _id, "_source": _source})
# print(
# f'bulk_data {bulk_data}'
# )
# break
if len(bulk_data) > 200:
total_count += len(bulk_data)
print("=" * 10, "to_elastic , total=" + str(total_count))
elastic.importBulkList(bulk_data, out_put_index_name)
bulk_data = []
# break
if len(bulk_data) > 0:
total_count += len(bulk_data)
print("=" * 10, "to_elastic , total=" + str(total_count))
elastic.importBulkList(bulk_data, out_put_index_name)
bulk_data = []
print(f"failed_data {len(failed_data)}")
DataHelper.save_orjson(
data=failed_data,
path=r"D:\init_mahdi\dataParsers\failed_data_mj_qa_conflict.json",
)
update_mj_qa_rules_translate()
# python -m dataParsers.app.update_mj_qa_rules_persian_text