This commit is contained in:
hsafaei
2025-02-22 16:44:14 +03:30
parent 43f46a17a3
commit fd2b89ed65
13 changed files with 669 additions and 50 deletions
+103
View File
@@ -0,0 +1,103 @@
import os
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)
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")