This commit is contained in:
hsafaei
2025-02-09 16:45:52 +03:30
parent d2b56991a0
commit b54683638a
11 changed files with 765 additions and 216 deletions
+179 -131
View File
@@ -66,138 +66,137 @@ def fill_nlp_tags(elasticHelper, index_name_o = "mj_qa_section_ai", path_resourc
parent_id = entry.get("parent_id", "0")
content = entry.get("content", "")
full_path = other_info["full_path"]
nlp_tags = entry.get("nlp_tags", "")
# nlp_tags = []
nlp_tags = []
# #"full_path": "ماده واحده>فصل ششم>بخش سوم>قسمت چهارم>فصل هشتم>ماده 153>بند ب>جزء 4",
# paths = full_path.split('>')
# last_path = ''
# if paths :
# last_path = paths[-1]
#"full_path": "ماده واحده>فصل ششم>بخش سوم>قسمت چهارم>فصل هشتم>ماده 153>بند ب>جزء 4",
paths = full_path.split('>')
last_path = ''
if paths :
last_path = paths[-1]
# level_2 = len(paths)
level_2 = len(paths)
# ###--------------------شناسایی بزرگی کوچکی متن -------------------
# word_len = len(split_words(content))
###--------------------شناسایی بزرگی کوچکی متن -------------------
word_len = len(split_words(content))
# ###--------------------شناسایی بزرگی کوچکی متن -------------------
# if word_len > 100 :
# nlp_tags.append("words_big")
# elif word_len > 50 :
# nlp_tags.append("words_medium")
# else :
# nlp_tags.append("words_small")
###--------------------شناسایی بزرگی کوچکی متن -------------------
if word_len > 100 :
nlp_tags.append("words_big")
elif word_len > 50 :
nlp_tags.append("words_medium")
else :
nlp_tags.append("words_small")
# ###--------------------شناسایی هدینگ بودن -------------------
# prefix, title, content_type = normalHeadingTitle(content, last_path)
# if not parent_id or parent_id == '0' or parent_id==0 :
# path_heading = []
# level_parent = 0
###--------------------شناسایی هدینگ بودن -------------------
prefix, title, content_type = normalHeadingTitle(content, last_path)
if not parent_id or parent_id == '0' or parent_id==0 :
path_heading = []
level_parent = 0
###--------------------شناسایی قوانین پرکاربرد -------------------
if qanon_id in useful_ids:
nlp_tags.append("qanon_usefull")
# ###-------------------- تعیین مسیر هدینگ -------------------
# ## هرچند با پرنت آی دی این خالی می شود ولی برای مطمئن کاری این شرط اضافه شد
# elif full_path=='عنوان' or full_path=='موخره' or full_path=='امضاء' :
# path_heading = []
# level_parent = 0
###-------------------- تعیین مسیر هدینگ -------------------
## هرچند با پرنت آی دی این خالی می شود ولی برای مطمئن کاری این شرط اضافه شد
elif full_path=='عنوان' or full_path=='موخره' or full_path=='امضاء' :
path_heading = []
level_parent = 0
# elif level_2 < level_parent :
# diff = level_parent - level_2
# while diff > 0 and path_heading :
# path_heading.pop()
# diff = diff - 1
elif level_2 < level_parent :
diff = level_parent - level_2
while diff > 0 and path_heading :
path_heading.pop()
diff = diff - 1
# level_parent = level_2
# # print('id :', id , ' --diff ', diff, path_heading )
level_parent = level_2
# print('id :', id , ' --diff ', diff, path_heading )
# if level_2 >= level_parent :
if level_2 >= level_parent :
# # برای اجزاء بعدی شناسایی و ذخیره می شود نه جزء جاری
# if prefix != '' and title != '':
# if level_2 == level_parent and path_heading :
# path_heading.pop()
# برای اجزاء بعدی شناسایی و ذخیره می شود نه جزء جاری
if prefix != '' and title != '':
if level_2 == level_parent and path_heading :
path_heading.pop()
# if len(title) < 256 :
# path_heading.append(title)
# nlp_tags.append("heading")
# level_parent = level_2
# ###--------------------------------------------------------
# full_path_prev = full_path
# full_path = title_type + ' :: ' + full_path
# heading_in_content = ''
if len(title) < 256 :
path_heading.append(title)
nlp_tags.append("heading")
level_parent = level_2
###--------------------------------------------------------
full_path_prev = full_path
full_path = title_type + ' :: ' + full_path
heading_in_content = ''
# if "heading" in nlp_tags :
# nlp_tags.append("concept_no")
# elif 'تفسیر' in state_etebar or 'تفسیر' in last_path :
# nlp_tags.append("concept_side")
# elif 'عنوان' in last_path or 'موخره' in last_path or 'امضاء' in last_path or 'سربرگ' in last_path or 'فهرست' in last_path or 'منشور' in last_path :
# nlp_tags.append("concept_no")
# elif 'ماده' in last_path or 'اصل' in last_path or 'متن' in last_path or 'مقدمه' in last_path or 'نامه' in last_path or 'کلیات' in last_path or 'قسمت' in last_path or 'توضیح' in last_path or 'سوال' in last_path or 'استفساریه' in last_path :
# nlp_tags.append("concept_base")
# elif 'بند' in last_path or 'جزء' in last_path or 'زیرجزء' in last_path or 'شماره' in last_path or 'ردیف' in last_path or 'پاورقی' in last_path or 'اشاره' in last_path or 'تنبیه' in last_path or 'بخش' in last_path or 'جواب' in last_path or 'مقرره' in last_path :
# nlp_tags.append("concept_week")
# elif 'تبصره' in last_path or 'بخش' in last_path or 'تذکر' in last_path or 'توصیه' in last_path or 'توجه' in last_path :
# nlp_tags.append("concept_middle")
# elif 'الحاقی' in state_etebar or 'الحاقی' in last_path :
# nlp_tags.append("concept_week")
# elif 'جدول' in last_path :
# nlp_tags.append("concept_bad")
# elif content_type in ['تبصره','بخش','تذکر'] :
# nlp_tags.append("concept_middle")
# elif content_type in ['ماده'] :
# nlp_tags.append("concept_base")
# # elif qanon_etebar == 'منسوخه' :
# # pass
# elif 'پروتکل' in full_path or 'موافقتنامه' in full_path or 'قطعنامه' in full_path or 'پیوست' in full_path or 'عهدنامه' in full_path or 'توافقنامه' in full_path or 'معاهده' in full_path or 'کنوانسیون' in full_path or 'قرارداد' in full_path :
# if not title :
# nlp_tags.append("concept_bad")
# elif len(title) < 50 :
# if prefix :
# nlp_tags.append("concept_head")
# heading_in_content = title
# else :
# nlp_tags.append("concept_week")
# else :
# nlp_tags.append("concept_base")
# elif prefix :
# if title and len(title) < 50 :
# file_error.write('---***---- data id=' + id + ' full_path : ' + full_path + ' prefix : '+ prefix +' --- '+ title + '\n')
# print('---***---- data id=', id, ' full_path : ', full_path , ' prefix : ', prefix ,' --- ', title)
# else :
# if title and len(title) < 50:
# if 'فصل' in last_path or 'مبحث' in last_path :
# nlp_tags.append("concept_head")
# heading_in_content = title
# if full_path_prev == '' :
# nlp_tags.append("concept_bad")
# else :
# file_error.write('------- data id='+ id + ' full_path : '+ full_path + ' prefix : '+ prefix +' --- '+ title + '\n')
# print('------- data id=', id, ' full_path : ', full_path , ' prefix : ', prefix ,' --- ', title)
# elif len(title) >= 50 :
# if full_path_prev == '' :
# nlp_tags.append("concept_bad")
# else :
# file_error.write('### ####### error data id='+ id + ' full_path : '+ full_path + ' prefix : '+ prefix +' --- '+ title + '\n')
# print('### ####### error data id=', id, ' full_path : ', full_path , ' prefix : ', prefix ,' --- ', title)
if "heading" in nlp_tags :
nlp_tags.append("concept_no")
elif 'تفسیر' in state_etebar or 'تفسیر' in last_path :
nlp_tags.append("concept_side")
elif 'عنوان' in last_path or 'موخره' in last_path or 'امضاء' in last_path or 'سربرگ' in last_path or 'فهرست' in last_path or 'منشور' in last_path :
nlp_tags.append("concept_no")
elif 'ماده' in last_path or 'اصل' in last_path or 'متن' in last_path or 'مقدمه' in last_path or 'نامه' in last_path or 'کلیات' in last_path or 'قسمت' in last_path or 'توضیح' in last_path or 'سوال' in last_path or 'استفساریه' in last_path :
nlp_tags.append("concept_base")
elif 'بند' in last_path or 'جزء' in last_path or 'زیرجزء' in last_path or 'شماره' in last_path or 'ردیف' in last_path or 'پاورقی' in last_path or 'اشاره' in last_path or 'تنبیه' in last_path or 'بخش' in last_path or 'جواب' in last_path or 'مقرره' in last_path :
nlp_tags.append("concept_week")
elif 'تبصره' in last_path or 'بخش' in last_path or 'تذکر' in last_path or 'توصیه' in last_path or 'توجه' in last_path :
nlp_tags.append("concept_middle")
elif 'الحاقی' in state_etebar or 'الحاقی' in last_path :
nlp_tags.append("concept_week")
elif 'جدول' in last_path :
nlp_tags.append("concept_bad")
elif content_type in ['تبصره','بخش','تذکر'] :
nlp_tags.append("concept_middle")
elif content_type in ['ماده'] :
nlp_tags.append("concept_base")
# elif qanon_etebar == 'منسوخه' :
# pass
elif 'پروتکل' in full_path or 'موافقتنامه' in full_path or 'قطعنامه' in full_path or 'پیوست' in full_path or 'عهدنامه' in full_path or 'توافقنامه' in full_path or 'معاهده' in full_path or 'کنوانسیون' in full_path or 'قرارداد' in full_path :
if not title :
nlp_tags.append("concept_bad")
elif len(title) < 50 :
if prefix :
nlp_tags.append("concept_head")
heading_in_content = title
else :
nlp_tags.append("concept_week")
else :
nlp_tags.append("concept_base")
elif prefix :
if title and len(title) < 50 :
file_error.write('---***---- data id=' + id + ' full_path : ' + full_path + ' prefix : '+ prefix +' --- '+ title + '\n')
print('---***---- data id=', id, ' full_path : ', full_path , ' prefix : ', prefix ,' --- ', title)
else :
if title and len(title) < 50:
if 'فصل' in last_path or 'مبحث' in last_path :
nlp_tags.append("concept_head")
heading_in_content = title
if full_path_prev == '' :
nlp_tags.append("concept_bad")
else :
file_error.write('------- data id='+ id + ' full_path : '+ full_path + ' prefix : '+ prefix +' --- '+ title + '\n')
print('------- data id=', id, ' full_path : ', full_path , ' prefix : ', prefix ,' --- ', title)
elif len(title) >= 50 :
if full_path_prev == '' :
nlp_tags.append("concept_bad")
else :
file_error.write('### ####### error data id='+ id + ' full_path : '+ full_path + ' prefix : '+ prefix +' --- '+ title + '\n')
print('### ####### error data id=', id, ' full_path : ', full_path , ' prefix : ', prefix ,' --- ', title)
# ###--------------------------------------------------------
# ## فقط در همین جزء ثبت شود
# path_heading2 = path_heading
# if heading_in_content :
# path_heading2.append(heading_in_content)
###--------------------------------------------------------
## فقط در همین جزء ثبت شود
path_heading2 = path_heading
if heading_in_content :
path_heading2.append(heading_in_content)
## ----------------------------------------
data = {}
data["nlp_tags"]= nlp_tags
# data["path_headings"]= path_heading2
# data["word_len"] = word_len
# data["level"]= level_2
data["path_headings"]= path_heading2
data["word_len"] = word_len
data["level"]= level_2
update = True
eid = id
@@ -343,6 +342,8 @@ def import_data_embeding(elasticHelper, ai_url_vec, index_name_i = "mj_qa_sectio
count = 0
qanon_id_prev = ""
parent_state = {}
parent_entry = {}
prev_entry_child = {}
for mentry in list:
count += 1
entry = mentry["source"]
@@ -357,19 +358,30 @@ def import_data_embeding(elasticHelper, ai_url_vec, index_name_i = "mj_qa_sectio
nlp_tags = entry.get("nlp_tags", [])
nlp_childs = entry.get("nlp_childs", [])
content_emb = ''
content_offset = ' '
qanon_id_prev = qanon_id
### نیازی به بردار سازی و جستجوی معنایی یا ارتباط یابی نیست
if 'concept_no' in nlp_tags or 'concept_bad' in nlp_tags or not content:
continue
content_emb = ''
content_offset = ' '
data = entry
word_len_childs = 0
if nlp_childs and len(nlp_childs) :
word_len_childs = 0
for item in nlp_childs :
word_len_childs += item["word_len"]
if nlp_childs and len(nlp_childs) :
## اگر پدر و فرزندهای قبلی چیزی هنوز مونده ، بردارسازی باید شود
if prev_entry_child :
eid = prev_entry_child.get("id", "")
content = prev_entry_child.get(field_content, "")
data = prev_entry_child
content_offset = parent_state["content_offset"]
content_emb = parent_state["content"] + "\n" + content + "\n"+ parent_state["content_emb"]
data["content_emb"] = content_emb
prev_entry_child = {}
# word_len_childs = 0
# for item in nlp_childs :
# word_len_childs += item["word_len"]
#word_len_total = word_len + word_len_childs
content_offset = 'در ' + qanon_title + ' درباره '
@@ -383,10 +395,11 @@ def import_data_embeding(elasticHelper, ai_url_vec, index_name_i = "mj_qa_sectio
"nlp_childs" : nlp_childs,
"content" : content,
"content_offset" : content_offset,
"word_len_childs" : word_len_childs,
"word_len_childs" : 0,
"content_emb" : "",
"is_parent_embedded" : 0
}
parent_entry = entry
### تا حد امکان با محتوای اصلی قبل، بردار شود و همانجا ذخیره شود
elif 'concept_week' in nlp_tags or 'concept_middle' in nlp_tags or 'concept_side' in nlp_tags :
if parent_state and parent_state["id"] == parent_id :
@@ -401,22 +414,52 @@ def import_data_embeding(elasticHelper, ai_url_vec, index_name_i = "mj_qa_sectio
## محتوای اصلی با بخشی از محتوای بندها و زیرجزءها، بردارسازی و ذخیره شود و مابقی در ادامه
else :
if parent_state["is_parent_embedded"] :
eid = id
else :
eid = parent_state["id"]
parent_state["is_parent_embedded"] = 1
content_offset = parent_state["content_offset"]
content_emb = parent_state["content"] + "\n" + parent_state["content_emb"]
data["content_emb"] = content_emb
if parent_state["is_parent_embedded"] == 1: ## قبلا محتوای پدر با فرزندان در حد طول 512 بردارسازی شده و ذخیره شده است
## حالا محتوای فرزندهای باقیمانده با کمک محتوای پدرا باید تا طول 512 بردارسازی شوند
if prev_entry_child :
eid = prev_entry_child.get("id", "")
content = prev_entry_child.get(field_content, "")
data = prev_entry_child
content_offset = parent_state["content_offset"]
content_emb = parent_state["content"] + "\n" + content + "\n"+ parent_state["content_emb"]
data["content_emb"] = content_emb
else : ## تست شد : پیش نمی آید
print('------ no prev entry for child ####### error data id=', id, ' wl_em : ', wl_em, ' parent_id : ', parent_id , ' parent_state : ', parent_state["id"] )
parent_state["word_len_childs"] = 0
parent_state["content_emb"] = ''
## پیش نمی آید
# else :
# print('### ####### error data id=', id, ' parent_id : ', parent_id , ' parent_state : ', parent_state["id"] )
prev_entry_child = entry
parent_state["word_len_childs"] = 0
parent_state["content_emb"] = ''
elif parent_entry :
parent_state["is_parent_embedded"] = 1
eid = parent_state["id"]
data = parent_entry
content_offset = parent_state["content_offset"]
content_emb = parent_state["content"] + "\n" + parent_state["content_emb"]
data["content_emb"] = content_emb
prev_entry_child = entry
parent_entry={}
parent_state["word_len_childs"] = 0
parent_state["content_emb"] = ''
else : ## تست شد : پیش نمی آید
print('### no parent for child ####### error data id=', id, ' parent_id : ', parent_id , ' parent_state : ', parent_state["id"] )
else :
#if parent_id != 0 :
## مواردی هستند که با پدر خود فاصله ایجاد شده است مثل qs918484
## فعلا کمک محتوایی براشون نمی کنیم
#print('### child withuot parent ####### error data id=', id, ' parent_id : ', parent_id , ' parent_state : ', parent_state["id"] )
eid = id
content_offset = 'در ' + qanon_title + ' درباره '
for hed in path_headings :
content_offset += ' ' + hed.strip()
content_emb = content
else :
eid = id
@@ -427,7 +470,7 @@ def import_data_embeding(elasticHelper, ai_url_vec, index_name_i = "mj_qa_sectio
content_emb = content
if content_emb :
payload={
@@ -444,6 +487,11 @@ def import_data_embeding(elasticHelper, ai_url_vec, index_name_i = "mj_qa_sectio
update = False
try:
if "pos_tags" in data:
del data["pos_tags"]
if "chunk_tags" in data:
del data["chunk_tags"]
resp = elasticHelper.update_index_doc(update, index_name_o, eid, data)
except Exception as e: