from app.common.normalizer import Normalizer from app.common.tokenizer import * from app.common.jalali import Gregorian,Persian import re import os import sys from re import sub import textwrap from html import escape, unescape from bs4 import BeautifulSoup import json from decimal import Decimal import traceback # from lxml import etree import datetime from datetime import timedelta import pandas as pd #enumerate(token_list): _normalizer = Normalizer(date_normalizing_needed=True) yeAr = r"ﻱ|ې|ێ|ے|ى|ي|ئ" yeFr= r"ی" keAr = r"ڭ|ﻚ|ﮎ|ﻜ|ﮏ|ګ|ﻛ|ﮑ|ﮐ|ڪ|ك" keFr = r"ک" mark1 = r'#\[#' mark2 = r'#\]#' hTag1 = r'<' hTag2 = r'>' tableTag=["table","tr", "th", "td", "TABLE", "TR", "TH", "TD"] strTable = '' for tag in tableTag: if strTable != '': strTable += '|' strTable += '('+tag+')' regTable = r'<(?P\/)*(?P'+strTable+')(?P[^>]+)*>' regTableReplace = r'#[#\g\g\g#]#' class JSONEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, Decimal): return float(obj) return json.JSONEncoder.default(self, obj) ##-------------------------------- # لازم است این فیلد پرشود تا مجلس بتواند دسترسی تغییر داشته باشد ##------------------------------------ def is_section_empty(section_path): filtered_keys = ['فصل','موخره','امضاء','عنوان'] if '>' in section_path: path_parts = section_path.split('>') for key in filtered_keys: if key in path_parts[-1]: return True else: for key in filtered_keys: if key in section_path: return True return False def isNeedHtml(html): if " str: """ Convert HTML -> plain text while preserving line breaks for tags like

,
,

  • , / (tables) etc. """ soup = BeautifulSoup(html_str, "html.parser") #
    -> newline for br in soup.find_all("br"): br.replace_with("\n") #

    -> ensure separated paragraphs (one blank line between paragraphs) for p in soup.find_all("p"): # put newline before and after paragraph content (avoid duplicate) p.insert_before("\n") p.append("\n") #

  • -> prefix with "- " and newline before each item for li in soup.find_all("li"): li.insert_before("\n- ") # keep li content as-is; ensure newline after li.append("\n") # table: make cells separated by tabs and rows end with newline for td in soup.find_all(["td", "th"]): # append a tab after each cell (so cells become column-separated) td.append("\t") for tr in soup.find_all("tr"): tr.append("\n") # Optional: collapse multiple consecutive whitespace-only newlines later text = soup.get_text() # convert HTML entities (&   etc.) text = unescape(text) # Normalize: convert runs of >2 newlines to max 2, strip trailing spaces on lines text = re.sub(r'\n\s*\n\s*\n+', '\n\n', text) # 3+ newlines -> 2 text = '\n'.join(line.rstrip() for line in text.splitlines()) # rstrip each line text = text.strip("\n ") # remove leading/trailing blanks/newlines # convert tabs used for table cells to single spaces if you prefer: text = text.replace("\t", " ") # or keep \t for programmatic use return text # erab def remove_arabic_diacritics(text): # Regular expression to match all Arabic diacritics diacritics_pattern = r'[\u064B-\u0652\u0670]' return re.sub(diacritics_pattern, '', text) def removeHtmlTags(html, exeptionTag=[]): #reg1 = r'<[^>]+>' if exeptionTag.__len__ : exceptTags = '' for tag in exeptionTag: if exceptTags != '': exceptTags += '|' exceptTags += '('+tag+')' reg1 = r'<(?P/)*(?P'+exceptTags+')(?P[^>]+)*>' html = sub(reg1, regTableReplace, html) soup = BeautifulSoup(html, "html.parser") text = soup.get_text("\n", strip=True) if exeptionTag.__len__ : text = sub(mark1, hTag1, text) text = sub(mark2, hTag2, text) return text def removeHtmlNoTableTag(html): # خطا داره و هنگ می کنه در test2.py html = sub(regTable, regTableReplace, html) soup = BeautifulSoup(html, "html.parser") text = soup.get_text("\n", strip=True) text = sub(mark1, hTag1, text) text = sub(mark2, hTag2, text) return text def split_words(text) : if not text : return [] words = re.split(r"[\t\n\r\.\"«»\)\(\]\[,;،؛ \-!?:]+", text) return words def is_number_string(value): try: float(value) return True except ValueError: return False def normalHeadingTitle(text, last_path = '', keyTitles=[]): global _normalizer lines = text.split('\n') ## [مجمع عمومی] , (فصل هشتیم مالیات مستغلات) , -اجراء ... text = lines[0].strip("-: ()[]") ## متن > قسمت الف -> قسمت if last_path : last_path = last_path.split()[0] if not keyTitles : keyTitles=['کتاب','باب','فصل','بخش' ,'جلد','قسمت','مبحث','فقره','اصل','مقاله','نکته','دسته','جدول','فرم','ضمیمه', 'پیوست', 'کنوانسیون' , 'قرارداد', 'عهدنامه', 'توافقنامه'] stopWords = ['شماره', 'در', 'درباره', 'الف', 'ب', 'ج', 'د', 'ه', 'و'] words = split_words(text) prefix = '' title = '' i = 0 for word in words : if title == '' : if i == 0 : if word in keyTitles : prefix += ' ' + word elif last_path in keyTitles : prefix += ' ' + word elif prefix and word.isnumeric() : prefix += ' ' + word elif prefix and _normalizer.is_number(word) : prefix += ' ' + word elif prefix and word in stopWords : prefix += ' ' + word else : title += ' ' + word else : title += ' ' + word i +=1 # کلمات کلیدی فصل باید ابتدای متن شناسایی شده باشند if i > 5 and prefix == '' : title = text break #باب ششم #جرائم و مجازات if prefix and len(lines) > 1 and len(lines[1]) < 100 : title = lines[1].strip("-: ()[]") content_type = '' if words and words[0] in ['تبصره', 'بند', 'بخش','تذکر', 'ماده'] : content_type = words[0] elif words and words[0] in keyTitles : content_type = words[0] title = title.strip().strip("1234567890 ") return prefix.strip(), title, content_type def normalizerData(data): global _normalizer normalTitle, dates = _normalizer.normalize(data, return_dates=True) tdates = [] for d in dates: if not d.startswith("num"): try: tsd = jdate2timestamp(d) cdate = d except: try: d = d.replace("y", "") d = d.replace("m", "/") d = d.replace("d", "/") m = re.match(r"^(\d{4})\D(\d{1,2})\D(\d{1,2})$", d) if m: [year, month, day] = [ int(m.group(1)), int(m.group(2)), int(m.group(3)), ] if year > 1200 and year < 1550: if month < 1 or month > 12: month = 1 if day < 1 or day > 31: day = 1 cdate = str(year) + "/" + str(month) + "/" + str(day) tsd = jdate2timestamp(cdate) else: # cdate = "1403/03/03" # tsd = jdate2timestamp(cdate) continue else: # cdate = "1403/03/03" # tsd = jdate2timestamp(cdate) continue except: # print("Error in:"+ d +" for id: " + id) # cdate = "1403/03/03" # tsd = jdate2timestamp(cdate) continue tdates.append({"date": cdate, "timestamp": tsd, "index": 0, "slice": ""}) return normalTitle,tdates def normalizerDate2(inputString): global _normalizer normalizedString, dates, recognized_dates, recognized_numbers = _normalizer.normalize(inputString, return_dates=True) tdates = [] for date_item in recognized_dates: date_part = date_item['date'] date_token_index = date_item['date_token_index'] start_date_token_index = date_item['start_date_token_index'] end_date_token_index = date_item['end_date_token_index'] if not date_part.startswith("num"): try: cdate = date_part tsd = jdate2timestamp(date_part) except: try: date_part = date_part.replace("y", "") date_part = date_part.replace("m", "/") date_part = date_part.replace("d", "/") m = re.match(r"^(\d{4})\D(\d{1,2})\D(\d{1,2})$", date_part) if m: [year, month, day] = [ int(m.group(1)), int(m.group(2)), int(m.group(3)), ] if year > 1200 and year < 1550: if month < 1 or month > 12: month = 1 if day < 1 or day > 31: day = 1 cdate = str(year) + "/" + str(month) + "/" + str(day) tsd = jdate2timestamp(cdate) else: # cdate = "1403/03/03" # tsd = jdate2timestamp(cdate) continue # else: # # cdate = "1403/03/03" # # tsd = jdate2timestamp(cdate) # continue except: # print("Error in:"+ d +" for id: " + id) # cdate = "1403/03/03" # tsd = jdate2timestamp(cdate) continue import tokenizer as t inputString_token = t.Tokenizer.tokenize_words(None,inputString) # if start_date_token_index == end_date_token_index: # end_date_token_index += 1 # original_date_part = inputString_token[start_date_token_index:end_date_token_index] # else: original_date_part = inputString_token[start_date_token_index:end_date_token_index + 1] original_date = '' for part in original_date_part: original_date = original_date + ' ' + part original_date = original_date.strip() tdates.append({"converted_date": date_item['date'], "date": cdate , "original_date" : original_date, # "timestamp": tsd, "date_token_index": date_token_index, "start_date_token_index": start_date_token_index, "end_date_token_index":end_date_token_index}) ''' for d in dates: if not d.startswith("num"): try: tsd = jdate2timestamp(d) cdate = d except: try: d = d.replace("y", "") d = d.replace("m", "/") d = d.replace("d", "/") m = re.match(r"^(\d{4})\D(\d{1,2})\D(\d{1,2})$", d) if m: [year, month, day] = [ int(m.group(1)), int(m.group(2)), int(m.group(3)), ] if year > 1200 and year < 1550: if month < 1 or month > 12: month = 1 if day < 1 or day > 31: day = 1 cdate = str(year) + "/" + str(month) + "/" + str(day) tsd = jdate2timestamp(cdate) else: # cdate = "1403/03/03" # tsd = jdate2timestamp(cdate) continue else: # cdate = "1403/03/03" # tsd = jdate2timestamp(cdate) continue except: # print("Error in:"+ d +" for id: " + id) # cdate = "1403/03/03" # tsd = jdate2timestamp(cdate) continue tdates.append({"date": cdate, "timestamp": tsd, "index": 0, "slice": ""})''' return normalizedString,tdates,recognized_numbers def OtherDateFormatNormalizer(inputString,pattern): mainTextTemp = inputString regex_pattern_Mah = r"y(\d{1,4})m(\d{1,2})d(\d{1,2})\sماه\s(\d{1,4})\sو\s(\d{1,3})\sو\s(\d{1,2})\sو\s(\d{1})\s" # y0m4d4 ماه 1000 و 300 و 50 و 4 regex_pattern_MahSal = r"y(\d{1,4})m(\d{1,2})d(\d{1,2})\sماه\sسال\s(\d{1,4})\sو\s(\d{1,3})\sو\s(\d{1,2})\sو\s(\d{1})\s" # y0m4d4 ماه سال 1000 و 300 و 50 و 4 regex_pattern_MahSal2 = r"y(\d{1,4})m(\d{1,2})d(\d{1,2})\sماه\sسال\sy(\d{1,4})m(\d{1,2})d(\d{1,2})\sو\s(\d{1,3})\sو\s(\d{1,2})\sو\s(\d{1})\s" # y0m4d4 ماه سال y1000m0d0 و 300 و 50 و 4 regex_pattern_MahSal3 = r"y(\d{1,4})m(\d{1,2})d(\d{1,2})\sماه\sسال\sy(\d{1,4})m(\d{1,2})d(\d{1,2})" # y0m3d1 ماه سال y1353m0d0 if(pattern==1): regex = re.compile(regex_pattern_Mah) elif(pattern==2): regex = re.compile(regex_pattern_MahSal) elif(pattern==3): regex = re.compile(regex_pattern_MahSal2) elif(pattern==4): regex = re.compile(regex_pattern_MahSal3) matches = regex.finditer(inputString) for match in matches: foundedPattern = match.group() foundedPatternTemp = match.group() if(pattern==1): foundedPattern = foundedPattern.replace('ماه','') else: foundedPattern = foundedPattern.replace('سال','') foundedPattern = foundedPattern.replace('ماه','') foundedPattern = foundedPattern.strip() tempString = foundedPattern standardDatePattern = r"y(\d{1,4})m(\d{1,2})d(\d{1,2})" #regex = re.compile(regex_pattern_Mah) matchItems = re.finditer(standardDatePattern,tempString) for item in matchItems: tempPattern = item.group() tempString = tempString.replace(tempPattern,'') tempString = tempString.strip() tempString = tempString.replace('و','') tempString = tempString.strip() tempArray = tempString.split() year = 0 for item in tempArray: dateMatch = re.finditer(standardDatePattern,item) regexFlag = True for dateItem in dateMatch: yearStr = dateItem.group()[1:5] year += int(yearStr) regexFlag = False break if(item.isalnum() and regexFlag): year += int(item) tempPattern = tempPattern.replace('y0','y'+str(year)) mainTextTemp = mainTextTemp.replace(foundedPatternTemp,tempPattern+' ') return mainTextTemp #foundedPattern = jdate2timestamp(foundedPattern) #convertedText = regex.sub(foundedPattern,convertedText) def normalizerLongData(data): dates = [] if len(data) > 10000: textParts = textwrap.wrap(data, 10000, break_long_words=False) for part in textParts: dates.extend(normalizerData(part)) else: dates = normalizerData(data) return dates # ################## # در ویندوز برای اعداد منفی که تاریخهای قبلی بود را خطا می داد # rr = gdt.timestamp() # ################# def jdate2timestamp_old(dt): ndt = dt.replace("y", "") ndt = ndt.replace("m", "/") ndt = ndt.replace("d", "/") gd = Persian(ndt).gregorian_datetime() # print(gd) ztime = datetime.time(0, 0, 0, 0) gdt = datetime.datetime.combine(gd, ztime) # print(gdt) rr = gdt.timestamp() tst = int( round(rr) * 1000) return tst def jdate2timestamp(dt): text = dt if dt.startswith("y") : # y1404m1d14 text = dt.replace("y", "") text = text.replace("m", "/") text = text.replace("d", "/") gd = Persian(text).gregorian_datetime() base = datetime.date(1970, 1, 1) rr = (gd-base).total_seconds() tst = int( round(rr) * 1000) return tst def jdate2Gregorian(text): gd = Persian(text).gregorian_datetime() tst = gd.strftime("%Y-%m-%d") # tst = d.isoformat() return tst def dateTimestamp(gdate): empty_date = -15000000000 ts_ts = empty_date try: base = datetime.datetime(1970, 1, 1) gd = datetime.datetime.strptime(gdate, "%Y-%m-%d") rr = (gd-base).total_seconds() ts_ts = int( round(rr) * 1000) except Exception as e: ts_ts = empty_date return ts_ts def getSortTimestamp(ts_date): empty_date = -15000000000 ts_ts = empty_date try: if ts_date != "": ts_ts = jdate2timestamp(ts_date) except: ts_ts = empty_date return ts_ts def normalize_content(content): text = normalYehKe(content) text = _normalizer.sub_alphabets(text) # کلماتی که با نیم فاصله از هم جدا شده اند، را به هم می چسباند # در این صورت، اگر با یک اسپیس جایگزین شود، یک توکن به متن اصلی اضافه می کند text = sub('\u200c','',text) pattern = r',|٬|٫|‚|,|؟|ʕ|،|_|ـ' text = sub(pattern,'', text) if text.__contains__('\u200c'): print(text) return text def normalYehKe(text): if(text == None) : return '' c1 = sub(yeAr, yeFr, text) c2 = sub(keAr, keFr, c1) c2 = c2.replace('\u00A0', '') return c2.strip() _term_list = [] def setTermList(): global _term_list if(_term_list.__len__() > 0): return _term_list = [ { "begin": jdate2timestamp("1285/07/14"), "end": jdate2timestamp("1287/04/2"), "term": "مجلس شورای ملی-دوره1", "term_number": 1, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1288/8/24"), "end": jdate2timestamp("1290/10/3"), "term": "مجلس شورای ملی-دوره2", "term_number": 2, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1293/9/14"), "end": jdate2timestamp("1294/8/21"), "term": "مجلس شورای ملی-دوره3", "term_number": 3, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1300/4/1"), "end": jdate2timestamp("1302/3/30"), "term": "مجلس شورای ملی-دوره4", "term_number": 4, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1302/11/22"), "end": jdate2timestamp("1304/11/22"), "term": "مجلس شورای ملی-دوره5", "term_number": 5, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1305/4/19"), "end": jdate2timestamp("1307/5/22"), "term": "مجلس شورای ملی-دوره6", "term_number": 6, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1307/7/19"), "end": jdate2timestamp("1309/8/14"), "term": "مجلس شورای ملی-دوره7", "term_number": 7, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1309/9/24"), "end": jdate2timestamp("1311/10/24"), "term": "مجلس شورای ملی-دوره8", "term_number": 8, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1311/12/24"), "end": jdate2timestamp("1314/1/24"), "term": "مجلس شورای ملی-دوره9", "term_number": 9, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1314/3/15"), "end": jdate2timestamp("1316/3/22"), "term": "مجلس شورای ملی-دوره10", "term_number": 10, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1316/6/20"), "end": jdate2timestamp("1318/6/27"), "term": "مجلس شورای ملی-دوره11", "term_number": 11, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1318/8/3"), "end": jdate2timestamp("1320/8/9"), "term": "مجلس شورای ملی-دوره12", "term_number": 12, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1320/8/22"), "end": jdate2timestamp("1322/9/1"), "term": "مجلس شورای ملی-دوره13", "term_number": 13, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1322/12/16"), "end": jdate2timestamp("1324/12/21"), "term": "مجلس شورای ملی-دوره14", "term_number": 14, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1326/4/25"), "end": jdate2timestamp("1328/5/6"), "term": "مجلس شورای ملی-دوره15", "term_number": 15, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1328/11/20"), "end": jdate2timestamp("1330/11/29"), "term": "مجلس شورای ملی-دوره16", "term_number": 16, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1331/2/7"), "end": jdate2timestamp("1332/8/28"), "term": "مجلس شورای ملی-دوره17", "term_number": 17, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1332/12/27"), "end": jdate2timestamp("1335/1/26"), "term": "مجلس شورای ملی-دوره18", "term_number": 18, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1335/3/10"), "end": jdate2timestamp("1339/3/29"), "term": "مجلس شورای ملی-دوره19", "term_number": 19, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1339/12/2"), "end": jdate2timestamp("1340/2/19"), "term": "مجلس شورای ملی-دوره20", "term_number": 20, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1342/7/14"), "end": jdate2timestamp("1346/7/13"), "term": "مجلس شورای ملی-دوره21", "term_number": 21, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1346/7/14"), "end": jdate2timestamp("1350/6/9"), "term": "مجلس شورای ملی-دوره22", "term_number": 22, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1350/6/9"), "end": jdate2timestamp("1354/6/16"), "term": "مجلس شورای ملی-دوره23", "term_number": 23, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1354/6/17"), "end": jdate2timestamp("1357/11/20"), "term": "مجلس شورای ملی-دوره24", "term_number": 24, "majles_name": "شورای ملی", }, { "begin": jdate2timestamp("1359/3/7"), "end": jdate2timestamp("1363/3/6"), "term": "مجلس شورای اسلامی-دوره1", "term_number": 1, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1363/3/7"), "end": jdate2timestamp("1367/3/6"), "term": "مجلس شورای اسلامی-دوره2", "term_number": 2, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1367/3/7"), "end": jdate2timestamp("1371/3/6"), "term": "مجلس شورای اسلامی-دوره3", "term_number": 3, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1371/3/7"), "end": jdate2timestamp("1375/3/11"), "term": "مجلس شورای اسلامی-دوره4", "term_number": 4, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1375/3/12"), "end": jdate2timestamp("1379/3/6"), "term": "مجلس شورای اسلامی-دوره5", "term_number": 5, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1379/3/7"), "end": jdate2timestamp("1383/3/6"), "term": "مجلس شورای اسلامی-دوره6", "term_number": 6, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1383/3/7"), "end": jdate2timestamp("1387/3/6"), "term": "مجلس شورای اسلامی-دوره7", "term_number": 7, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1387/3/7"), "end": jdate2timestamp("1391/3/6"), "term": "مجلس شورای اسلامی-دوره8", "term_number": 8, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1391/3/7"), "end": jdate2timestamp("1395/3/7"), "term": "مجلس شورای اسلامی-دوره9", "term_number": 9, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1395/3/8"), "end": jdate2timestamp("1399/3/6"), "term": "مجلس شورای اسلامی-دوره10", "term_number": 10, "majles_name": "شورای اسلامی", }, { "begin": jdate2timestamp("1399/3/7"), "end": jdate2timestamp("1403/3/6"), "term": "مجلس شورای اسلامی-دوره11", "term_number": 11, "majles_name": "شورای اسلامی", }, ] def getTermQanon(ts_date_timestamp, ts_ref): setTermList() global _term_list term = "" term_number = 0 majles_name = "" if ts_ref == "هيات وزيران (دوره فترت)": term = ts_ref if ts_ref == "نخست وزير (مصدق)": term = ts_ref if ts_ref == "وزير عدليه (داور)": term = ts_ref if ts_ref == "شوراي انقلاب جمهوري اسلامي ايران": term = ts_ref majles_name = term if term == "": for i in range(len(_term_list) - 1, -1, -1): begin = _term_list[i]["begin"] end = _term_list[i]["end"] if ts_date_timestamp >= begin and ts_date_timestamp <= end: term = _term_list[i]["term"] term_number = _term_list[i]["term_number"] majles_name = _term_list[i]["majles_name"] break error = "" if term == "": # if ts_date_timestamp >= _term_list[0]["begin"] and ts_date_timestamp <= _term_list[len(_term_list)-1]["end"] : if ts_date_timestamp <= _term_list[len(_term_list) - 1]["end"]: for i in range(0, len(_term_list) - 1, 1): end = _term_list[i]["end"] if ts_date_timestamp <= end: term = _term_list[i]["term"] term_number = _term_list[i]["term_number"] majles_name = _term_list[i]["majles_name"] error = "تاریخ بین دو دوره" break else: term_number = -1 error = "تاریخ خارج از محدوده" return term, term_number, majles_name, error # این متد یک متن و ایندکس آغاز و پایان یک عبارت درون آن متن را دریافت می کند # و شماره توکن آغازین و توکن پایانی مربوط به عبارت در متن را بر می گرداند def token_state_finder(normalized_section_content, start_index, end_index): before_substring = normalized_section_content[0:start_index-1].strip() pattern_substring = normalized_section_content[start_index-1:end_index+1].strip() before_substring_token_list = before_substring.strip().split() pattern_token_list = pattern_substring.strip().split() start_token_state = len(before_substring_token_list) end_token_state = len(before_substring_token_list) + (len(pattern_token_list)-1) pattern_tokens_state ={ "start_token_state": start_token_state, "end_token_state" : end_token_state } return pattern_tokens_state def find_number_indexes_in_string(normalized_string,recognized_numbers): complete_recognized_numbers = [] for item in recognized_numbers: number_start_index, number_end_index = find_token_indexes_in_string(normalized_string,item['start_token_index'],item['end_token_index']) content = normalized_string.split() # if item['start_token_index']==item['end_token_index']: # # حذف این بخش و باقی گذاشتن دستور ذیل الز زیر در کفایت درست کار کردن متد بررسی شود # number_token_list = content[item['start_token_index']] # else: number_token_list = content[item['start_token_index']:item['end_token_index']+1] complete_recognized_numbers.append( { 'number_value' : item['number_value'], 'number_token_list' : number_token_list, 'start_token_index' : item['start_token_index'], 'end_token_index' : item['end_token_index'], "start_number_state": number_start_index, "end_number_state" : number_end_index } ) return complete_recognized_numbers # این متد متن اصلی یک متن، توکن آغازین و توکن پایانی مربوط به یک عبارت را می گیرد # و ایندکس آغاز و ایندکس پایان متن وارد شده را بر می گرداند def find_token_indexes_in_string(normalized_string,start_token_state,end_token_state): before_tokens = normalized_string.split()[0:start_token_state] content_tokens = normalized_string.split()[start_token_state:end_token_state + 1] content_start_index = 0 content_end_index = 0 # شمردن تعداد کاراکترهای هر توکن در لیست توکن قبل از عدد for token in before_tokens: content_start_index += len(token) # اضافه کردن تعداد فاصله های خالی یا همان اسپیس به عدد ایندکس شروع عدد content_start_index += len(before_tokens) + 1 # شمردن تعداد کاراکترهای هر توکن در لیست توکن مربوط به عدد for token in content_tokens: content_end_index += len(token) # اضافه کردن تعداد فاصله های خالی یا همان اسپیس به عدد ایندکس پایان عدد content_end_index += (content_start_index - 1) + (len(content_tokens) - 1) return content_start_index, content_end_index # این متد، متنی را دریافت می کند و الگوهای تعریف شده را در آن جستجو می کند و آرایه ای از عبارات مطابق با هر الگو، # شماره ایندکس شروع و پایان هر عبارت، عنوان و محتوای الگو، و شماره توکن شروع و توکن پایانی هر عبارت # پیدا شده را بر می گرداند def regex_patterns_finder(sectoin_content): regex_patterns = { "asle N asasi": r"اصل\s*شماره\s*(\d+)\s*قانون\s*اساسی\s*جمهوری\s*اسلامی\s*ایران", # اصل شماره فلان قانون اساسی جمهوری اسلامی ایران "qanone asasi": r"(? str: # عادی‌سازی متن ورودی caption = re.sub(r'\s+', ' ', caption.strip()) # حذف فاصله‌های اضافی caption = caption.replace('\u200c', ' ') # جایگزینی نیم‌فاصله‌ها با فاصله کامل if "موافقتنامه" in caption or "موافقت نامه" in caption or "کنوانسیون" in caption or \ "قرارداد بین المللی" in caption or "مقررات بین المللی" in caption or "الحاق دولت" in caption or \ "الحاق ایران" in caption or "قرارداد بین المللی" in caption or \ caption.startswith("قانون عضویت دولت") or caption.startswith("قانون عضویت ایران") or caption.startswith("قانون اجازه عضویت"): return "موافقتنامه" elif caption.startswith("الحاق ") or caption.startswith("قانون الحاق ") or caption.startswith("قانون الحاقیه "): return "الحاقی" elif re.match(r"^قانون اضافه.*قانون", caption): return "الحاقی" elif caption.startswith("اصلاح ") or re.match(r"^قانون اصلاح(?:ی)? .*قانون", caption) or re.match(r"^لایحه قانونی اصلاح(?:ی)? .*لایحه", caption): return "اصلاحی" elif re.match(r"^قانون اصلاحیه.*قانون", caption): return "اصلاحی" elif re.match(r"^قانون تغییر.*قانون", caption): return "اصلاحی" elif caption.startswith("نظر تفسیری ") or caption.startswith("استفساریه ") or caption.startswith("تفسیر ") or caption.startswith("قانون تفسیر ") or caption.startswith("قانون استفساریه "): return "تفسیری" elif caption.startswith("تصمیم "): return "تصمیم" elif caption.startswith("نظر "): return "نظر" elif caption.startswith("نامه "): return "نامه" elif caption.startswith("قانون لغو ") or caption.startswith("قانون الغای ") or caption.startswith("قانون الغاء ") or caption.startswith("قانون فهرست قوانین و احکام نامعتبر"): return "لغو" elif caption.startswith("قانون برنامه "): return "برنامه" elif caption.startswith("قانون بودجه "): return "بودجه" elif caption.startswith("قانون حذف "): return "حذفی" elif re.match(r"^قانون حذف.*قانون", caption): return "حذفی" elif caption.startswith("قانون تمدید "): return "تمدیدی" elif re.match(r"^قانون دایمی.*قانون", caption): return "تمدیدی" elif re.match(r"^قانون دائمی.*قانون", caption): return "تمدیدی" elif caption.startswith("قانون شمول "): return "شمولی" elif re.match(r"^قانون شمول.*قانون", caption): return "شمولی" elif re.match(r"^قانون نسخ.*قانون", caption): return "نسخی" elif re.match(r"^قانون متمم.*قانون", caption): return "مربوط" elif re.match(r"^قانون مربوط.*قانون", caption): return "مربوط" elif re.match(r"^قانون تکمیل.*قانون", caption): return "مربوط" elif re.match(r"^قانون راجع.*قانون", caption): return "مربوط" elif caption.startswith("قانون موقتی ") or caption.startswith("قانون موقت "): return "موقتی" else: return "اصلی" #//////////////////////////////////////////////////////////// #//////////////////////////////////////////////////////////// # print(os.getcwd()) def remove_signs(): str = read_file() # lines = pattern = r"\(|\)" str = re.sub(pattern,'', str) # str = re.sub(')','', str) # str = re.sub('/','', str) return str def read_file(): with open('./data/DATASET_2.txt', 'r', encoding='utf-8') as file: text = '' try: text = str(file.read()) except: pass return text def read_file_by_address(file_address): with open(file_address, 'a+', encoding='utf-8') as file: text = '' try: # Move the cursor to the beginning of the file to read its content file.seek(0) text = file.read() except: pass return text def save_to_file(result): with open('./data/DATASET_3.txt', 'a+', encoding='utf-8') as file: previous_result = '' try: previous_result = file.read() except: pass file.write(result) file.close() def save_to_file_by_address(file_address, text): with open(file_address, 'a+', encoding='utf-8') as file: previous_result = '' try: previous_result = file.read() except: pass file.write(text) file.close() # def read_from_excel(file_address, column_name): # # خواندن فایل اکسل # data = read_excel(file_address) # # استخراج محتوای ستون مورد نظر # column_data = data[column_name] # return column_data # def add_columndata_to_excel(file_address, column_name, columndata): # # خواندن فایل اکسل # data = read_excel(file_address) # # اضافه کردن ستون جدید به داده‌ها # data[column_name] = columndata # # ذخیره کردن داده‌ها در فایل اکسل # data.to_excel(file_address, index=False) def write_to_json(dict, file_address): # تبدیل دیکشنری به فرمت JSON json_data = json.dumps(dict, indent=2, ensure_ascii=False) # ذخیره فایل with open(file_address, 'w+', encoding='utf-8') as file: file.write(json_data) return True def read_from_json(file_address): data_dict = [] # خواندن اطلاعات از فایل JSON with open(file_address, 'r', encoding='utf-8') as file: loaded_data = json.load(file) # نمایش اطلاعات خوانده شده for item in loaded_data: data_dict.append(item) return data_dict def separated_date_format_finder(date_ner): result = False date_ner = date_ner.replace('.','/') date_ner = date_ner.replace('،','/') date_ner = date_ner.replace('ر','/') #date_pattern = r'\d{1,2} /\d{1,2} /\d{2,4}|\d{1,2}/\d{1,2}/\d{2,4}|\d{2,4} /\d{1,2} /\d{1,2}|\d{2,4}/\d{1,2}/\d{1,2}' date_pattern = r'\b(?:(?:1[0-2]|0?[1-9])/?(?:3[01]|[12][0-9]|0?[1-9])/?(?:14[0-7][0-9]|13[0-9][0-9]|128[0-9])|(?:1[0-2]|0?[1-9])/?(?:3[01]|[12][0-9]|0?[1-9])/?(?:14[0-7][0-9]|13[0-9][0-9]|128[0-9]|[0-9]{2}))\b' regex = re.compile(date_pattern) match_dates = regex.finditer(date_ner) for date_item in match_dates: result = True break return result def myTimestamp(days=0): dt = datetime.datetime.now() # tt = round(time.time() * 1000) if days != 0 : dt = (dt - timedelta(days)) tt = dt.timestamp() return round(tt) #//////////////////////////////////////////////////////////// #//////////////////////////////////////////////////////////// def is_match_persian_date_format(text): #چون NaN تنها مقداری است که خودش با خودش برابر نیست if not text or text != text : # True اگر NaN باشد: return False text = str(text) if text == '0000/00/00' : return False if text == '00/00/00' : return False # items = text.split('/') pattern = r'\d{4}/\d{1,2}/\d{1,2}' if re.match(pattern,text) : return True return False def convert_persian_date(value): res = '' _date = value if not _date or _date == '' : return res, '' #چون NaN تنها مقداری است که خودش با خودش برابر نیست if _date != _date : # True اگر NaN باشد: return res, '' _date = str(_date) if _date : _date = _date.replace('-', '/') _date = _date.replace('\\', '/').strip() if _date and not is_match_persian_date_format(_date): return res, "error match_persian_date_format" if _date : try : res = jdate2Gregorian(_date) except Exception as e: return res, "error convert persian_date" return res, '' def repair_object_persian_date(item, key, error_list=None, id='', key2=''): res = '' path_key = key2+'.'+key if key2 else key if key in item : _date = item[key] res, error = convert_persian_date(_date) if not res : if error and not error_list is None : error_list.append({'id':id, 'key': path_key, "value":_date, "message": error }) del item[key] return item if res : item[key] = res return item def excel_to_json(excel_file, required_columns=None, sheet_name=0): """ خواندن یک sheet از فایل اکسل و تبدیل به لیست دیکشنری. Args: sheet_name: نام sheet (رشته) یا شماره آن (عدد). پیش‌فرض: 0 (اولین sheet) """ sheet_names = pd.ExcelFile(excel_file).sheet_names print("all sheet") for i, name in enumerate(sheet_names, start=1): print(f" {i}. {name}") df = pd.read_excel(excel_file, sheet_name=sheet_name) if required_columns is None: selected_columns = df.columns.tolist() else: missing = [col for col in required_columns if col not in df.columns] if missing: raise ValueError(f"ستون‌های مفقوده در sheet '{sheet_name}': {missing}") selected_columns = required_columns return df[selected_columns].to_dict(orient="records") def extract_excel_fields(file_path: str, fields: list = [], sheet_name: str = None): """ فایل اکسل را باز کرده و فقط فیلدهای خواسته‌شده را به‌صورت لیست از دیکشنری‌ها برمی‌گرداند. :param file_path: مسیر فایل اکسل (.xlsx یا .xls) :param fields: لیست نام ستون‌هایی که باید خوانده شوند :return: لیستی از دیکشنری‌ها شامل داده‌های ستون‌های انتخاب‌شده """ try: # خواندن فایل df = pd.read_excel(file_path, sheet_name=sheet_name) df.columns = df.columns.map(str) if len(fields) > 0: missing = [f for f in fields if f not in df.columns] if missing: print(f"⚠️ This columns mot found in file {missing}") # for i in df.columns: # print(f'-field ->{i}<-') df = df[[f for f in fields if f in df.columns]] # تبدیل به لیست از دیکشنری‌ها data = df.to_dict(orient="records") return data except Exception as e: print(f"❌ خطا در خواندن فایل اکسل: {e}") return [] def get_files(directory, pattern_file=r"output_(\d+)\.json$"): pattern = None if pattern_file : pattern = re.compile(pattern_file) max_num = 0 list_file = [] for filename in os.listdir(directory): if pattern : match = pattern.match(filename) if match: list_file.append(directory+"/"+filename) else: list_file.append(directory+"/"+filename) return list_file # prefix, title = normalHeadingTitle("فصل اول - سیاستها و اصول") # print('prefix :', prefix) # print('title :', title)