import os import zipfile from dotenv import load_dotenv from text_normalizer import TextNormalizer # from general import * from general_functions import * from elastic_helper import ElasticHelper ### -------------------------------------------------------------- ## این کد برای استخراج و بهینه سازی کلیدواژه های تولید شده با مدلهای زبانی بزرگ و انتقال به الاستیک نوشته شد ## کلیدواژه ها با ساختار مشخص و مقداری خطا تولید شده بودند def moveKeywords(elastic, index_name_o, path_file, fields=['content'], body={}): text_normal = TextNormalizer() dic_main = json.loads(open(path_file,'r', encoding="utf-8").read()) index = 0 percent_prev = 0 list=[] for item in dic_main: index += 1 percent = int((index / len(dic_main)) * 100) if percent != percent_prev: print("progress -> %.2f %%" % percent) percent_prev = percent id = item['id'] keywords = set() for key in item['keywords']: # "•\t1.\tقانون جدید", # "2.\tکلاه برداری" key = text_normal.normalize_qanon(key) key = key.replace('\t', ' ').strip() key = key.replace('\n', ' ').strip() key = key.replace('\r', ' ').strip() key = key.strip(' •.') key = re.sub(r"^[ \t]*[\d•]+[ .\t]*", '', key).strip() if key : key = text_normal.normalize_qanon(key) keywords.add(key) content = item['content'] words = text_normal.split_words(content) sorted_keys=[] if len(keywords) > 10 and (len(words) * 2) < len(keywords) : # print('id ', id , ' content', content) print('id ', id , 'len words ', len(words) , ' len keywords', len(keywords) ) list_temp = sorted(keywords, key=len) sorted_keys=list_temp[0:len(words)] # print( ' keywords : ', sorted_keys) # exit() else: sorted_keys = sorted(keywords, key=len) list.append({ '_id': id, '_source': { "ai_keywords" : sorted_keys } }) if len(list) > 200 : elastic.updateBulkList(list, index_name_o) list = [] if len(list) : elastic.updateBulkList(list, index_name_o) ### -------------------------------------------------------------- ## برای انتقال جملات ساده سازی شده توسط مدلهای زبانی بزرگ به الاستیک قانون نوشته شد def moveZipSimplifies(elastic, index_name, file_path): fileNo = 0 with zipfile.ZipFile(file_path, 'r') as zObject: fileNo +=1 print("="*10, " zip fileNo: " , fileNo ," - ( ", index_name," ) | File Numbers:" ,len(zObject.namelist()) , "=" * 10) packNo = 0 for filename in zObject.namelist(): packNo += 1 print("index:" , index_name , '=>' , filename ) with zObject.open(filename) as file: data = json.loads(file.read()) moveSimplifies(elastic, index_name, data) elastic.es.indices.refresh(index=index_name) print(elastic.es.cat.count(index=index_name, format="json")) print(" END Of Import to elastic ", index_name ,"\n") def moveSimplifies(elastic, index_name_o, data): text_normal = TextNormalizer() index = 0 percent_prev = 0 list_bulk=[] for item in data: index += 1 # percent = int((index / len(data)) * 100) # if percent != percent_prev: # print("progress -> %.2f %%" % percent) # percent_prev = percent id = item['id'] simplified = item['simplified'] list_simp = simplified.split('\n') simplifies = set() for key in list_simp: # "1.ddddd", key = key.replace('\t', ' ').strip() key = key.replace('\r', ' ').strip() key = key.strip(' •.') key = re.sub(r"^[ \t]*[\d•]+[ .\t]*", '', key).strip() if key : key = text_normal.normalize_qanon(key) simplifies.add(key) list_bulk.append({ '_id': id, '_source': { "simplifies" : list(simplifies) } }) if len(list_bulk) > 200 : elastic.updateBulkList(list_bulk, index_name_o) list_bulk = [] if len(list_bulk) : elastic.updateBulkList(list_bulk, index_name_o) load_dotenv(dotenv_path='config.env') host=os.getenv('HOST_160') password=os.getenv('HOST_160_PASS') # host=os.getenv('HOST_112') # password=os.getenv('HOST_112_PASS') # host=os.getenv('HOST_60') # password=os.getenv('HOST_60_PASS') print(host) # print(password) path_base_map = "G:/Project2/gateway2_main/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) #moveKeywords(elastic,"mj_qa_section", "data/sections_kw_llama8b_170k_concatenated.json") #moveKeywords(elastic,"mj_qa_section", "data/sections_kw_llama8b_40k_concatenated.json") moveZipSimplifies(elastic,"mj_qa_section", "G:\Work\_majles\_data\simpilified_final.zip")