paln tcode

This commit is contained in:
hsafaei
2025-06-07 15:17:39 +03:30
parent b2ae4b810c
commit 53764fd4ef
6 changed files with 642 additions and 81 deletions
+149 -57
View File
@@ -50,6 +50,10 @@ def get_qanon_title_sample(text):
text = text_normalizer.hamzah_normal(text)
text = text_normalizer.alaem_normal(text)
text = re.sub(r"( |\u200C)+", "", text)
if text.startswith('فصل'):
# در آدرس دهی چون براساس شماره ماده هست فصل مهم نیست. چون شماره ماده کلی هست
text = re.sub(r"^(فصل[^>]>)+", "", text)
return text
def get_section_title_sample(text):
@@ -406,6 +410,52 @@ def get_qanon_date(title):
return qanon_title, ts_date, ts_ref
def get_section_correct(title):
title=title.strip().strip(' #=').strip()
if title == 'کل قانون' or title == 'متن قانون' or title == 'متن' or title == 'تمام قانون' or title == 'کل مصوبه':
return ['all']
title=title.replace('"', '')
title=title.replace('اصلاحی', '')
title = re.sub(r"(\(|\)|\>|\<|-|ـ)+", "", title)
subs = title.split('،')
result = []
for item in subs :
item=re.sub(r"(جزء|بند|ماده|فصل|مبحث|ردیف|جدول|تبصره|پیوست|قسمت)", r"#\1", item)
items = item.split('#')
items.reverse()
item = '> '.join(items).strip(' >').replace(' ', '')
item = text_normalizer.word_digit_correction(item)
result.append(item)
return result
def get__all_section_qanon(elastic, index_name, qanon_id):
body = {
"_source" : ["ref_id"],
"query": {
"bool": {
"must": [
{
"term": {
"qanon_id": {
"value": qanon_id
}
}
}
]
}
}
}
s_res = elastic.es.search(index=index_name, body=body)
result = []
for item in s_res["hits"]["hits"] :
result.append(item["_id"])
return result
def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_id , file_log, result_path = ""):
df = pd.read_excel(file_excel, sheet_name=0)
@@ -486,69 +536,113 @@ def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_
else:
state = 'قانون-جستجو'
# # اگر شناسه قانون یافت شد، برای شناسه جزء جستجو کند ...
# if not qanon_id == 0 :
# section_title = text_normalizer.normalize_search_qanon(str(section_title))
# section_id, score, section_item = searchSectionId(elastic, "mj_qa_section", qanon_id , section_title)
# # - اگر شناسه یافت شده جزء قابل اطمینان نبود خطا ثبت شود
# if score < 90 :
# dic_error_section_title[str(qanon_id)+'=='+section_title]=count
# print('='*10 , str(count) , ' section error', qanon_id, ' --> ', section_title )
# file.write('---- section error ---' + str(qanon_id) + ' ----- ' + section_title + '\n')
# section_id = 0
# state = 'خطا-جزء'
# else :
# state = 'صحیح'
# اگر شناسه قانون یافت شد، برای شناسه جزء جستجو کند ...
section_title = text_normalizer.normalize_search_qanon(str(section_title))
result_section_title = get_section_correct(section_title)
result_section_id = []
if qanon_id != 0 :
for section_title in result_section_title:
if section_title == 'all' :
state = 'صحیح'
result_section_id.append('all')
continue
section_id, score, section_item = searchSectionId(elastic, "mj_qa_section", qanon_id , section_title)
# - اگر شناسه یافت شده جزء قابل اطمینان نبود خطا ثبت شود
if score < 90 :
dic_error_section_title[str(qanon_id)+'=='+section_title]=count
print('='*10 , str(count) , ' section error', qanon_id, ' --> ', section_title )
file.write('---- section error ---' + str(qanon_id) + ' ----- ' + section_title + '\n')
section_id = 0
state = 'خطا-جزء'
elif state == '':
state = 'صحیح'
result_section_id.append(section_id)
# ذخیره شناسه ها و وضعیت برای اکسل جدید
row[column_count+1]=qanon_id
row[column_count+2]=section_id
row[column_count+2]= '#'.join(str(item) for item in result_section_id)
row[column_count+3]=state
row[column_count+4]=qanon_title +'#'+ ts_date +'#'+ ts_ref
row[column_count+5]='#'.join(result_section_title)
rows_list_new.append(row)
data={}
# # ذخیره کد در الاستیک برای شناسه های یافت شده
# if section_id and section_id != 0:
# item = elastic.get_doctument(index_name_o,section_id)
# tcode_mains = []
# tcode_subs = []
# if "tcode_mains" in item['_source'] :
# tcode_mains= item['_source']["tcode_mains"]
# if "tcode_subs" in item['_source'] :
# tcode_subs= item['_source']["tcode_subs"]
tstate = 'نامعتبر'
c_name = 'وضعیت اعتبار'
if c_name in column_names :
tstate = row[c_name].replace('#', '').replace('=', '')
if tstate.startswith('نامعتبر') :
tstate = 'نامعتبر-' + tstate
# code = {
# "label" : tcode_label,
# "id" : tcode_id
# }
# if code_state :
# tcode_mains.append(code)
# else:
# tcode_subs.append(code)
# data={
# '_id':section_id,
# '_source' : {
# 'tcode_mains': tcode_mains,
# 'tcode_subs': tcode_subs,
# }
# }
# bulk_data.append(data)
treason = ''
c_name = 'مستند عدم اعتبار'
if c_name in column_names :
treason = row[c_name].replace('#','\n').replace('=','\n')
description = ''
c_name = 'توضیحات'
if c_name in column_names :
description = row[c_name].replace('*', '')
# ذخیره کد در الاستیک برای شناسه های یافت شده
for section_id in result_section_id :
if section_id == '' or section_id == 0 :
continue
if section_id =='all':
tstate = tstate + ' کل '
result = get__all_section_qanon(elastic, index_name_o, qanon_id)
for sid in result :
tvalid_state={
"tstate": tstate.strip(),
"description": description,
"treason": treason,
# "treason_type": treason_type
}
data={
'_id':sid,
'_source' : {
'tvalid_state': tvalid_state
}
}
bulk_data.append(data)
break
tvalid_state={
"tstate": tstate.strip(),
"description": description,
"treason": treason,
# "treason_type": treason_type
}
data={
'_id':section_id,
'_source' : {
'tvalid_state': tvalid_state
}
}
bulk_data.append(data)
# # انتقال دسته جمعی کدها به الاستیک
# if len(bulk_data) >= 200:
# total_count += len(bulk_data)
# print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count))
# elastic.updateBulkList(bulk_data, index_name_o)
# bulk_data = []
# انتقال دسته جمعی کدها به الاستیک
if len(bulk_data) >= 200:
total_count += len(bulk_data)
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count))
elastic.updateBulkList(bulk_data, index_name_o)
bulk_data = []
# if len(bulk_data) > 0:
# total_count += len(bulk_data)
# print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count) )
# elastic.updateBulkList(bulk_data, index_name_o)
if len(bulk_data) > 0:
total_count += len(bulk_data)
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(count) )
elastic.updateBulkList(bulk_data, index_name_o)
# columns=['جزء', 'مصوب', 'قانون', 'اصلی', 'ردیف', 'شناسه قانون', 'شناسه جزء', 'وضعیت پردازش']
@@ -556,6 +650,7 @@ def importEtebarToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_
column_names.append(column_count+2)
column_names.append(column_count+3)
column_names.append(column_count+4)
column_names.append(column_count+5)
columns= column_names
new_df = pd.DataFrame(rows_list_new, columns=columns)
if result_path == '' :
@@ -623,10 +718,7 @@ def importCodesToElastic(elastic, file_excel, index_name_o, tcode_label, tcode_i
qanon_title = row[column_names[2]]
code_state = row[column_names[3]]
row_number = row[column_names[4]]
if code_state :
print ( 'ddddddddddddddcode_state' , code_state)
else :
print ( 'ccccccccccccccode_state' , code_state)
qanon_id = 0
section_id = 0
state = ''
@@ -819,8 +911,8 @@ def run_Etebar_directory(elastic, directory, output):
w1 = text_normalizer.split_words(code_name)[0]
log_file = output + "/log_r_"+w1+".txt"
file_path = os.path.join(directory, file)
if code_name != 'کشاورزی' :
continue
# if code_name != 'سلامت' :
# continue
try:
os.makedirs(output, exist_ok=True)
@@ -855,9 +947,9 @@ path_base_map = "G:/Project2/gateway2_main/repo/_other/"
elastic = ElasticHelper(host, password, "elastic", path_base_map)
# run_Codes_directory(elastic, "./data/tcodes", "./data/tcodes/result")
run_Codes_directory(elastic, "./data/tcodes", "./data/tcodes/result")
run_Etebar_directory(elastic, "./data/tetebar", "./data/tetebar/result")
# run_Etebar_directory(elastic, "./data/tetebar", "./data/tetebar/result")
# importCodesToElastic(elastic, "./data/tcodes/مسکن و شهرسازی.xlsx", "mj_qa_section", "مسکن و شهرسازی", "./data/log_r3.txt")
# importCodesToElastic(elastic, "./data/tcodes/آمار، برنامه و بودجه.xlsx", "mj_qa_section", "مسکن و شهرسازی", "./data/log_r_آمار.txt")