diff --git a/elastic_helper.py b/elastic_helper.py index 3425cf9..f206a43 100644 --- a/elastic_helper.py +++ b/elastic_helper.py @@ -3,7 +3,7 @@ import sys import os import json from time import sleep -from elasticsearch import Elasticsearch,helpers +from elasticsearch8 import Elasticsearch,helpers class ElasticHelper(): diff --git a/qanon_functions.py b/qanon_functions.py index e44ed60..7fdb463 100644 --- a/qanon_functions.py +++ b/qanon_functions.py @@ -110,6 +110,86 @@ def get_dict_qanon_title(file_path): return dict +def searchQanonTitleOfContent(elastic, index_name, qanon_title, ts_data=""): + score = 100 + body = { + "query": { + "bool": { + "must": [ + { + "match": { + "title.keyword": { + "value": qanon_title + } + } + } + ] + } + } + } + s_res = elastic.es.search(index=index_name, body=body) + total = s_res["hits"]["total"]['value'] + if total == 1 : + item = s_res["hits"]["hits"][0] + return item["_id"], score, item + + if total == 0 : + body = { + "query": { + "bool": { + "must": [ + "more_like_this": { + "fields": [ + "content.fa" + ], + "like": qanon_title, + "min_term_freq": 1, + "max_query_terms": 12 + } + } + ] + } + } + s_res = elastic.es.search(index=index_name, body=body) + total = s_res["hits"]["total"]['value'] + + qanon_title_sample = re.sub(r"( |\u200C)+", "", qanon_title) + list_equal = [] + for item in s_res["hits"]["hits"]: + title = item['_source']['title'] + # حذف اسپیسها + title_sample = re.sub(r"( |\u200C)+", "", title) + + if title_sample == qanon_title_sample : + list_equal.append(item) + else : + qanon_title_sample = get_qanon_title_sample(qanon_title) + + title_sample = get_qanon_title_sample(title) + + if title_sample == qanon_title_sample : + list_equal.append(item) + + if len(list_equal) == 1 : + item = list_equal[0] + score = 90 + if ts_data == item['_source']['ts_date'] : + score = 100 + return item["_id"], score, item + + for item in list_equal : + if ts_data == item['_source']['ts_date'] : + score = 100 + return item["_id"], score, item + + if len(list_equal) : + score = 90 + item = list_equal[0] + return item["_id"], score, item + + return 0, 0, {} + + def searchQanonId(elastic, index_name, qanon_title, ts_data=""): score = 100 body = { diff --git a/r6_before_excel.py b/r6_before_excel.py index f871320..b520075 100644 --- a/r6_before_excel.py +++ b/r6_before_excel.py @@ -9,7 +9,7 @@ import pandas as pd from qanon_functions import * -def importBeforeInfosToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_id , file_log, result_path = ""): +def parseBeforeInfos(elastic, file_excel, file_log, result_path = ""): df = pd.read_excel(file_excel, sheet_name=0, header=None) count = 0 rows_list = df.to_dict(orient='records') @@ -28,6 +28,54 @@ def importBeforeInfosToElastic(elastic, file_excel, index_name_o, tcode_label, t rows_list_new = [] bulk_data = [] total_count=0 + try: + for row in rows_list: + row_id = row[column_names[0]] + qanon_title = row[column_names[1]] + ts_date = row[column_names[3]] + + qanon_id = 0 + + count = count + 1 + if (count % 10) == 0 : + print("%s, %.2f " % ( '================== ', (count / total) * 100 if total > 0 else 0)) + + qanon_title = text_normalizer.normalize_search_qanon(qanon_title) + + # جداسازی اعداد چسبیده به کلمات قبل یا بعد مثل + # قانون بودجه سال 1369کل کشور + qanon_title = re.sub(r"([^\/])(\d+)([\u0600-\u06FF\u0750-\u077F]+?)", r"\1\2 \3", qanon_title) + + score = 100 + + qanon_id = 0 + # ابتدا از قانونهای خاص که شناسه ذخیره شده دارند جستجو کند + if qanon_title in dict_qanon_id : + score = 100 + qanon_id = dict_qanon_id[qanon_title] + state = 'قانون-دستی' + # if qanon_id == 0 : + # continue # این قانون در بانک ما وجود ندارد + + # اگر پیدا نشد، از الاستیک جستجو کند + else : + qanon_id, score, item = searchQanonId(elastic, "mj_qa_qanon", qanon_title, ts_date) + # اگر قابل اطمینان نبود، خطا ثبت کند + if score < 90 : + dic_error_qanon_title[qanon_title]=count + print('-'*10 , str(count) , ' qanon error', qanon_title ) + file.write('---- qanon error ---' + str(count) + ' ----- ' + qanon_title + '\n') + qanon_id = 0 + state = 'خطا-قانون' + else: + state = 'قانون-جستجو' + + # ذخیره شناسه ها و وضعیت برای اکسل جدید + # row[5]=qanon_id + # row[6]=section_id + # row[7]=state + # rows_list_new.append(row) + load_dotenv(dotenv_path='config.env') @@ -47,4 +95,5 @@ path_base_map = "G:/Project2/gateway2_main/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) -#importBeforeInfosToElastic(elastic, "./data/tcodes", "./data/tcodes/result") \ No newline at end of file +parseBeforeInfos(elastic, "./data/جداول بانک قوانین_info-شناسه قانون یار.xlsx" , "پیشاقانون1.txt", "./data/result") +#parseBeforeInfos(elastic, "./data/tcodes", "./data/tcodes/result") \ No newline at end of file