law repair

This commit is contained in:
hsafaei
2025-05-14 10:51:27 +03:30
parent ad5bcb1c44
commit 20f115bbe2
7 changed files with 311 additions and 45 deletions
+232 -12
View File
@@ -33,6 +33,7 @@ def extract_all_effecteds(elastic, index_name, body={}):
count = 0
text_normal = TextNormalizer()
result = {}
result_new = {}
for mentry in list:
entry = mentry["source"]
@@ -42,7 +43,10 @@ def extract_all_effecteds(elastic, index_name, body={}):
to_section_id = effect["to_section_id"]
effect_type = effect["effect_type"]
result[to_section_id] = effect_type
return result
if effect_type == 'الحاقی' or effect_type =='یتفسیر' :
id = effect["id"]
result_new[id] = to_section_id
return result, result_new
def repair_law_section_text(cursor, elastic, index_name, dict_changed, file_log) :
@@ -51,6 +55,7 @@ def repair_law_section_text(cursor, elastic, index_name, dict_changed, file_log)
select
s.ID as sectionId
,isnull(s.SECTIONTEXT,'') as sectionText -- متن ماده یا تبصره
,isnull(l.APPROVEDATE,'') as aproveDate -- تاریخ تصویب
from lwSection s
left join lwLaw l on s.F_LWLAWID = l.ID
where l.ISLAW = 1
@@ -99,22 +104,39 @@ def repair_law_section_text(cursor, elastic, index_name, dict_changed, file_log)
content_len = text.__len__()
eid='qs'+ str(row.sectionId)
ts_year = ''
if row.aproveDate :
items = row.aproveDate.split("/")
if items.__len__() > 0:
ts_year = items[0]
data={}
if eid in dict_changed :
print( ' ------------------> chenged effected : ', dict_changed[eid])
file.write(' ------------------> chenged effected : '+ dict_changed[eid] + '\n')
if dict_changed[eid] == 'اصلاحی' or dict_changed[eid] == 'جایگزین':
continue
# اصلاح تاریخ تصویب که بخاطر خطای سامانه ایجاد شده است
data={
'_id':eid,
'_source' : {
'ts_date': row.aproveDate,
'ts_year': ts_year,
}
}
#word_len = len(split_words(text))
data={
'_id':eid,
'_source' : {
'content': text,
'content_len': content_len,
if not data :
data={
'_id':eid,
'_source' : {
'content': text,
'content_len': content_len,
}
}
}
item = {}
try:
@@ -146,6 +168,197 @@ def repair_law_section_text(cursor, elastic, index_name, dict_changed, file_log)
if len(bulk_data) > 0:
total_count += len(bulk_data)
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid )
elastic.updateBulkList(bulk_data, index_name)
file.close()
print('end success import_law_section_to_elastic')
t2= datetime.datetime.now()
print(t2-t1)
def repair_error_new_section_ts_date( elastic, index_name, dict_changed, file_log) :
total_count = 0
bulk_data = []
t1= datetime.datetime.now()
print(t1)
file = open(file_log, 'a+', encoding='utf8')
i = 0
for key in dict_changed.keys():
# باعث خطای کوئری میشد و نیاز نبود
# print(i)
i=i+1
eid=key
eid_prev = dict_changed[key]
ts_year = ''
item = {}
try:
item = elastic.get_doctument(index_name, eid_prev)
ts_date = item['_source']['ts_date']
ts_year = item['_source']['ts_year']
sort_date_timestamp = item['_source']['sort_date_timestamp']
# در اینصورت محتوای ماده توسط مجلس تغییر یافته است
except Exception as e:
continue
data={
'_id':eid,
'_source' : {
'ts_date': ts_date,
'ts_year': ts_year,
'sort_date_timestamp': sort_date_timestamp, # created from repairQanon.getSortTimestamp
}
}
if len(bulk_data) >= 10:
total_count += len(bulk_data)
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid)
elastic.updateBulkList(bulk_data, index_name)
bulk_data = []
if data :
bulk_data.append(data)
if len(bulk_data) > 0:
total_count += len(bulk_data)
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid )
elastic.updateBulkList(bulk_data, index_name)
file.close()
print('end success import_law_section_to_elastic')
t2= datetime.datetime.now()
print(t2-t1)
def repair_error_section_ts_date(cursor, elastic, index_name, dict_changed, file_log) :
ids = ''
for key in dict_changed.keys():
# باعث خطای کوئری میشد و نیاز نبود
if '_' in key :
continue
key = key.replace('qs', '')
# if key == 'qs1414840_2512221' or key == 'qs1414840_2511' or key=='qs67f53e80b9949_008' :
# continue
# number = False
# try :
# number = int(key)
# except :
# number = False
# if not number :
# continue
if ids != '' :
ids = ids + ','
ids = ids + key[2:]
query = f'''
select
s.ID as sectionId
,isnull(s.SECTIONTEXT,'') as sectionText -- متن ماده یا تبصره
,isnull(l.APPROVEDATE,'') as aproveDate -- تاریخ تصویب
from lwSection s
left join lwLaw l on s.F_LWLAWID = l.ID
where l.ISLAW = 1 and s.ID in ({ids})
order by s.F_LWLAWID,textOrder
'''
print(query)
cursor.execute(query)
text_normal = TextNormalizer()
total_count = 0
bulk_data = []
t1= datetime.datetime.now()
print(t1)
file = open(file_log, 'a+', encoding='utf8')
i = 0
while 1:
# print(i)
i=i+1
row = cursor.fetchone()
if not row:
break
# if i > 1000:
# break
# print(row.sectionId)
try:
soup = BeautifulSoup(row.sectionText, "html.parser")
text = soup.get_text("\n", strip=True)
# text = removeHtmlNoTableTag(row.sectionText) # خطا داره
except Exception as e:
text = row.sectionText
text = text_normal.normalize_qanon(text)
text2 = text_normal.normalize_qanon(text,True)
# در این صورت مشکل خطای حذف خط تیره کشیده در این متن نبوده و نیازه به بروز رسانی متن نیست
if len(text) == len(text2) :
if text != text2 :
print(' ??????????> changed content : ', eid, "\ntext:", text , "\ntext2:", text2)
continue
content_len = text.__len__()
eid='qs'+ str(row.sectionId)
ts_year = ''
if row.aproveDate :
items = row.aproveDate.split("/")
if items.__len__() > 0:
ts_year = items[0]
sort_date_timestamp = -15000000000 # 9/2/1494, 12:45:44 AM # خطا یا خالی
try:
sort_date_timestamp = getSortTimestamp(row.aproveDate)
except Exception as e:
sort_date_timestamp = -15000000000 # 9/2/1494, 12:45:44 AM # خطا یا خالی
data={}
if eid in dict_changed :
# if dict_changed[eid] == 'اصلاحی' or dict_changed[eid] == 'جایگزین':
# اصلاح تاریخ تصویب که بخاطر خطای سامانه ایجاد شده است
data={
'_id':eid,
'_source' : {
'ts_date': row.aproveDate,
'ts_year': ts_year,
'sort_date_timestamp': sort_date_timestamp, # created from repairQanon.getSortTimestamp
}
}
if len(bulk_data) >= 10:
total_count += len(bulk_data)
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid)
elastic.updateBulkList(bulk_data, index_name)
bulk_data = []
if data :
bulk_data.append(data)
if len(bulk_data) > 0:
total_count += len(bulk_data)
print('=' * 5, 'law_section ===', ', total='+str(total_count) , str(i)+ ', id=' + eid )
@@ -354,7 +567,7 @@ body={
"exists": {"field": "effects"}
}
}
dict_effected_sections = extract_all_effecteds(elastic, "mj_df_qanon", body)
dict_effected_sections, dict_effected_new_sections = extract_all_effecteds(elastic, "mj_df_qanon", body)
# print(dict_effected_sections)
# try:
@@ -364,6 +577,13 @@ dict_effected_sections = extract_all_effecteds(elastic, "mj_df_qanon", body)
# print(type(e).__name__)
# print(str(e))
#repair_law_section_text(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r1.txt")
###### step 1 repair text - ###########################
repair_law_section_text(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r1.txt")
repair_law_section_ref_state(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r2.txt")
###### step 2 : repair date #############
repair_error_section_ts_date(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r1.txt")
repair_error_new_section_ts_date( elastic, "mj_qa_section", dict_effected_new_sections, "./output/log_r1.txt")
###### step 3 : not nead #############
#repair_law_section_ref_state(cursor, elastic, "mj_qa_section", dict_effected_sections, "./output/log_r2.txt")