from time import time import requests from general_functions import * from config_base import * from text_normalizer import TextNormalizer def create_ai_Index(elasticHelper, index_name, app_key, map_file="") : elasticHelper.createIndex(index_name, app_key, 'general/', map_file ) def import_data_embeding(elasticHelper, ai_url_vec, index_name_i = "mj_qa_section", index_name_o = "mj_qa_section_ai", field_content="content") : counter = 0 total = 0 id = "" body={} dicIds = {} try: try: list = elasticHelper.es_iterate_all_documents(index_name_i, body) except Exception as e: save_error(0, e) count = 0 for mentry in list: count += 1 entry = mentry["source"] id = entry.get("id", "") content = entry.get(field_content, "") data = entry if content : payload={ 'text':content } response = requests.request("POST", ai_url_vec, headers=HEADERS, json=payload) if response.status_code == 500 : print(response.text) bulk_data = [] continue resp = json.loads(response.text) data["embeddings"]=resp['result'] update = False eid = id try: resp = elasticHelper.update_index_doc(update, 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) # print("Exception: " + str(e)) # print( e) # print("%s -> %.2f " % (id , (counter / total) if total > 0 else 0)) # try: # print('ts_date:'+ts_date) # except: # print('not definded') print("=" * 10, "setting finished") def import_classification(elasticHelper, ai_url_class, index_name_o = "mj_qa_section_ai") : id = "" body={ "query": { "bool": { "must_not": [ { "exists": { "field": "code-ai" } } ] } } } dicIds = {} try: try: list = elasticHelper.es_iterate_all_documents(index_name_o, body) except Exception as e: save_error(0, e) count = 0 print('*' * 20 , ' count=', elasticHelper.total) for mentry in list: count += 1 entry = mentry["source"] id = entry.get("id", "") content = entry.get("content", "") data = {} if content : payload={ 'text':content } response = requests.request("POST", ai_url_class, headers=HEADERS, json=payload) if response.status_code == 500 : print(response.text) continue resp = json.loads(response.text) data["code-ai"]=resp['result'] update = True eid = id try: resp = elasticHelper.update_index_doc(update, 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) # print("Exception: " + str(e)) # print( e) # print("%s -> %.2f " % (id , (counter / total) if total > 0 else 0)) # try: # print('ts_date:'+ts_date) # except: # print('not definded') print("=" * 10, " finished") def fill_list_tags(elasticHelper, index_name_o = "mj_qa_section") : id = "" body = { # "sort": [ # {"qanon_id": {"order": "asc"}}, # {"child_order": {"order": "asc"}} # ] } text_normal = TextNormalizer() useful_titles = [] useful_titles_al = [] useful_finded = [] # خواندن کلمات موجود در فایل with open("qanon_usefull.txt", 'r', encoding='utf-8') as f: useful_titles = set(line.strip() for line in f) with open("qanon_usefull_al.txt", 'r', encoding='utf-8') as f: useful_titles_al = set(line.strip() for line in f) dicIds = {} try: try: list = elasticHelper.es_iterate_all_documents(index_name_o, body) except Exception as e: save_error(0, e) count = 0 print('*' * 20 , ' count=', elasticHelper.total) for mentry in list: count += 1 entry = mentry["source"] id = entry.get("id", "") qanon_etebar = entry.get("qanon_etebar", "") title_type = entry.get("title_type", "") qanon_title = entry.get("qanon_title", "") data = {} ## ---------------------------------------- title_type = getTitleType(qanon_title) data["title_type"] = title_type ## ---------------------------------------- list_tags = [] # بررسی شرایط مختلف if title_type in ["عادی", "اصلی", "موافقتنامه", "بودجه"]: list_tags.append("mom") if qanon_etebar in ["معتبر", "نامشخص", "تمدید", "تنفیذ"]: list_tags.append("motabar") elif qanon_etebar in ["منسوخه", "موقت", "آزمایشی", "بااجرامنتفی می شود"]: list_tags.append("mansokh") if qanon_title in useful_titles: list_tags.append("usefull") useful_finded.append(qanon_title) if qanon_title in useful_titles_al: if "usefull" not in list_tags : list_tags.append("usefull") useful_finded.append('---'+qanon_title) else: useful_finded.append('***'+qanon_title) data["list_tags"] = list_tags ## --------words len-------------------------------- content = entry.get("content", "") word_len = len(text_normal.split_words(content)) data["word_len"] = word_len ## ---------------------------------------- update = True eid = id try: resp = elasticHelper.update_index_doc(update, 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) print("Exception: " + str(e)) print( e) # print("%s -> %.2f " % (id , (counter / total) if total > 0 else 0)) # try: # print('ts_date:'+ts_date) # except: # print('not definded') useful_notfound = [] for item in useful_titles : if item in useful_finded : continue else : useful_notfound.append(item) for item in useful_titles_al : if '---'+item in useful_finded : continue if '***'+item in useful_finded : continue else : useful_notfound.append('***'+item) with open("qanon_usefull_notfound.txt", "w", encoding="utf-8") as f: for line in useful_notfound : f.write(f"{line}\n") print("=" * 10, " fill_list_tags finished") def fill_content_nlp(elasticHelper,index_name_o = "mj_qa_section_ai") : id = "" body = { "sort": [ {"qanon_id": {"order": "asc"}}, {"child_order": {"order": "asc"}} ] } dicIds = {} try: try: list = elasticHelper.es_iterate_all_documents(index_name_o, body) except Exception as e: save_error(0, e) count = 0 print('*' * 20 , ' count=', elasticHelper.total) for mentry in list: count += 1 entry = mentry["source"] id = entry.get("id", "") qanon_etebar = entry.get("qanon_etebar", "") title_type = entry.get("title_type", "") qanon_title = entry.get("qanon_title", "") other_info = entry.get("other_info", {}) parent_id = entry.get("parent_id", "0") html = entry.get("html", "") data = {} ## ---------------------------------------- content = entry.get("content", "") if content : data["content_nlp"]= content update = True eid = id try: resp = elasticHelper.update_index_doc(update, 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) # print("Exception: " + str(e)) # print( e) # print("%s -> %.2f " % (id , (counter / total) if total > 0 else 0)) # try: # print('ts_date:'+ts_date) # except: # print('not definded') print("=" * 10, " fill_content_nlp finished") def isMomSection(other_info, content): if not other_info["full_path"] : return True paths = other_info["full_path"].split('>') last_item = paths[len(paths)-1] print(paths) print(last_item) if "ماده" in last_item : return True content = content.strip() if re.match(r"^((ضمیمه)|(جدول))\s*\d*$", content): return True isMomSection({"full_path":"باب اول>فصل اول>ماده 8"})