diff --git a/app/majles/ai_create_chunks.py b/app/majles/ai_create_chunks.py index c02db9b..50d9fd4 100644 --- a/app/majles/ai_create_chunks.py +++ b/app/majles/ai_create_chunks.py @@ -413,7 +413,7 @@ def recursive_collect_child_sections(section_id, all_sections): -def extract_qanon_group_text(elastic, qanon_id, path_out="data/chunks/" ): +def extract_qanon_group_text(elastic, qanon_id, all_sections={}, path_out="data/chunks/" ): text = "" body={ "query": { @@ -428,28 +428,31 @@ def extract_qanon_group_text(elastic, qanon_id, path_out="data/chunks/" ): {"child_order": {"order": "asc"}} ] } - all_sections = {} - try: - list_ = elastic.es_iterate_all_documents(index_name_o, ) - for item in list_ : - all_sections[item["_id"]]=item["_source"] - except Exception as e: - save_error(0, e) - - original_sections = [] - qanon_sections_dict = {} - for section_id in all_sections: - if all_sections[section_id]['qanon_id'] == qanon_id: - # ذخیره سکشن های اصلی هر قانون - section_data = all_sections[section_id] - section_data['section_id'] = section_id - # ریختن تمام اطلاعات مربوط به هر سکشن در یک دیکشنری با کلید child_order - qanon_sections_dict[int(all_sections[section_id]['child_order'])] = section_data + if not all_sections : + try: + list_ = elastic.es_iterate_all_documents(index_name_o, body) + for item in list_ : + all_sections[item["_id"]]=item["_source"] + except Exception as e: + print("Exception 1", str(e)) - # مرتب سازی سکشن های یک قانون بر اساس child_order - qanon_sections_dict = dict(sorted(qanon_sections_dict.items())) - for item in qanon_sections_dict: - original_sections.append(qanon_sections_dict[item]['section_id']) + + # original_sections = [] + # qanon_sections_dict = {} + # for section_id in all_sections: + # if all_sections[section_id]['qanon_id'] == qanon_id: + # # ذخیره سکشن های اصلی هر قانون + # section_data = all_sections[section_id] + # section_data['section_id'] = section_id + # # ریختن تمام اطلاعات مربوط به هر سکشن در یک دیکشنری با کلید child_order + # qanon_sections_dict[int(all_sections[section_id]['child_order'])] = section_data + + # # مرتب سازی سکشن های یک قانون بر اساس child_order + # qanon_sections_dict = dict(sorted(qanon_sections_dict.items())) + # for item in qanon_sections_dict: + # original_sections.append(qanon_sections_dict[item]['section_id']) + + # گرفتن تمام سکشن های مربوط به یک قانون all_chunk_sections = [] qanoon_text = '' @@ -457,22 +460,25 @@ def extract_qanon_group_text(elastic, qanon_id, path_out="data/chunks/" ): nlp_childs_ids = [] child_sections_ids = [] row_counter = 1 - for row_order, section_id in enumerate(original_sections, 1): - # لیست سکشن هایی که زیرجزء یک سکشن دیگر بوده اند در متغیر child_sections_ids قرار دارد و چک می کنیم که سکشن را در بازسازی متن قانون، تکرار نکنیم - if section_id in child_sections_ids: - continue - nlp_childs_ids = recursive_collect_child_sections(section_id, all_sections) - nlp_childs_ids.reverse() - # اضافه کردن سکشن های زیر مجموعه به لیست سکشن هایی که در دور بعد مجددا بررسی نشوند - child_sections_ids.extend(nlp_childs_ids) - - qanoon_text += f"بخش (-- {row_counter} --)\n{all_sections[section_id]['content']}\n" - for sub_id in nlp_childs_ids: - qanoon_text += f"{all_sections[sub_id]['content']}\n" - nlp_childs_ids.clear() - qanoon_text += f'\n****************************************************\n' - row_counter += 1 - + for section_id, data in all_sections.items(): + # لیست سکشن هایی که زیرجزء یک سکشن دیگر بوده اند در متغیر child_sections_ids قرار دارد و چک می کنیم که سکشن را در بازسازی متن قانون، تکرار نکنیم + if section_id in child_sections_ids: + continue + nlp_childs_ids = recursive_collect_child_sections(section_id, all_sections) + nlp_childs_ids.reverse() + # اضافه کردن سکشن های زیر مجموعه به لیست سکشن هایی که در دور بعد مجددا بررسی نشوند + child_sections_ids.extend(nlp_childs_ids) + + group_content = + # qanoon_text += f"بخش (-- {row_counter} --)\n{all_sections[section_id]['content']}\n" + # for sub_id in nlp_childs_ids: + # qanoon_text += f"{all_sections[sub_id]['content']}\n" + + nlp_childs_ids.clear() + + qanoon_text += f'\n****************************************************\n' + row_counter += 1 + # ذخیره کل قانون جدید(جزءنگاری جدید) با فرمت تکست with open(f"./data_nlpchild/sample_chunking_{qanoon}.txt", 'w') as file: