117 lines
3.4 KiB
Python
117 lines
3.4 KiB
Python
from app.common.text_normalizer import TextNormalizer
|
|
from app.common.general_functions import *
|
|
from app.common.elastic_helper import ElasticHelper
|
|
|
|
|
|
dict_qanon = {}
|
|
|
|
def getContext(parent_id, qanon_title):
|
|
context = ''
|
|
context += f"\n** قانون: {qanon_title} "
|
|
pid = parent_id
|
|
|
|
path=''
|
|
while True:
|
|
if not pid or pid == '0' or pid not in dict_qanon:
|
|
break # اگر pid '0' است یا در dict_qanon وجود ندارد، حلقه متوقف میشود
|
|
|
|
# دریافت content والد
|
|
parent_content = dict_qanon[pid].get("content", "")
|
|
if parent_content :
|
|
path += f"\\{parent_content}"
|
|
|
|
# بروزرسانی pid به والد بالاتر
|
|
pid = dict_qanon[pid].get("parent_id", '0')
|
|
|
|
if path :
|
|
context += f"\n** سرفصل: {qanon_title} "
|
|
|
|
return context
|
|
|
|
|
|
def create_context_ai(elastic, index_name_i, fields=["qanon_title", "tree" ], index_name_o = '' ):
|
|
|
|
body={
|
|
# "query": {
|
|
# "bool": {
|
|
# "must_not": [
|
|
# {"exists": {"field": "nlp_parser.type"}},
|
|
# {"match": {"content_len": 0}},
|
|
# {"match": {"parse_state": 1}},
|
|
# {"match": {"parse_state": 2}}
|
|
# ]
|
|
# }
|
|
# },
|
|
"sort": [
|
|
{
|
|
"qanon_id": {"order": "asc"},
|
|
"child_order": {"order": "asc"}
|
|
}
|
|
]
|
|
|
|
}
|
|
|
|
if index_name_o == '' :
|
|
index_name_o = index_name_i
|
|
|
|
try:
|
|
try:
|
|
list = elastic.es_iterate_all_documents(index_name_i, body)
|
|
except Exception as e:
|
|
save_error(0, e)
|
|
|
|
count = 0
|
|
cur_qanon_id = 0
|
|
text_normal = TextNormalizer()
|
|
result = []
|
|
|
|
data={}
|
|
for mentry in list:
|
|
count += 1
|
|
entry = mentry["source"]
|
|
id = entry.get("id", "")
|
|
|
|
content = entry.get("content", "")
|
|
content = text_normal.normalize_qanon(content)
|
|
qanon_title = entry.get("qanon_title", "")
|
|
parent_id = entry.get("parent_id", "")
|
|
other_info = entry.get("other_info", "")
|
|
level = other_info["level"]
|
|
qanon_id = entry.get("qanon_id", "")
|
|
|
|
|
|
data = {}
|
|
### /////////////////////////////////
|
|
# جمله اصلی برای سند فعلی
|
|
|
|
if parent_id == '0' or parent_id == 0 or parent_id == '':
|
|
dict_qanon = {}
|
|
|
|
if qanon_id != cur_qanon_id :
|
|
dict_qanon = {}
|
|
cur_qanon_id = qanon_id
|
|
|
|
dict_qanon[id] = {
|
|
"content" : content,
|
|
"other_info" : other_info,
|
|
"id" : id,
|
|
"parent_id" : parent_id,
|
|
}
|
|
|
|
context = getContext(parent_id, qanon_title)
|
|
data["context_llm"] = context
|
|
|
|
#### ///////////////////////////////////
|
|
|
|
|
|
eid = id
|
|
try:
|
|
resp = elastic.update_index_doc(True, index_name_o, eid, data)
|
|
#if not update :
|
|
# dicIds[eid] = True
|
|
|
|
except Exception as e:
|
|
save_error(id, e)
|
|
|
|
except Exception as e:
|
|
save_error(id, e) |