将一个字典中的值与另一个字典的键链接,并用正则表达式替换字符串中的值


import re
def normalize_intervals_to_2_digits(match):
input_text_substring = match.group()
input_text_substring = re.sub(r"(bd)(?!d)", r"01", input_text_substring, 2)
return input_text_substring

#Examples:
input_text = "hay 4 objetos los 3 primeros dias del mes de enero del 2020"  #example 1
input_text = "hay que ir alli los 10 ultimos dias del mes de julio del 2022"  #example 2
input_text = "suelen ser algo frios los primeros dias del mes de noviembre"  #example 3
input_text = "hay que plantar 5 calabazas los ultimos dias del mes de octubre del 2021"  #example 4

#month data dictionaries
es_month_dict = {"enero": "01", "febrero": "02", "marzo": "03", "abril": "04", "mayo": "05", "junio": "06", "julio": "07", "agosto": "08", "septiembre": "09", "octubre": "10", "noviembre": "11", "diciembre": "12"}
quantity_days_associated_to_month_num = { "01":"01_to_31", "02":"01_to_28", "03":"01_to_31", "04":"01_to_30", "05":"01_to_31", "06":"01_to_30", "07":"1_to_31", "08":"01_to_31", "09":"01_to_30", "10":"01_to_31", "11":"01_to_30", "12":"01_to_31" }
last_day_of_this_month = .replace("01_to_","") #for example, "01_to_31" --> "31"
#here do the day's replacements...
input_text_substring = re.sub( , , input_text)

input_text_substring = re.sub(r"(d{1,2} -- d{1,2})", normalize_intervals_to_2_digits, input_text)
print(repr(input_text)) #output

根据所属月份的日期字符串转换规则:

n为泛型数d{1,2}:

"los primeros n dias" ---> (01 -- 0n)

"los ultimos n dias" ---> (last_day_of_this_month - n -- last_day_of_this_month)

或者考虑5作为标准,如果没有明确指示数字,那么我们将假设n = 5天:

"los primeros dias" ---> (01 -- 05)

"los ultimos dias" ---> (last_day_of_this_month - 5 -- last_day_of_this_month)

正确的输出应该是:

"hay 4 objetos los (01 -- 03) del mes de enero del 2020"  #for the example 1
"hay que ir alli los (20 -- 30) del mes de julio del 2022"  #for the example 2, 30 - 10 = 20
"suelen ser algo frios los (01 -- 05) del mes de noviembre"  #for the example 3
"hay que plantar 5 calabazas (26 -- 31) del mes de octubre del 2021"  #for the example 4, 31 - 5 = 26

我如何将字典es_month_dict中的数据与字典quantity_days_associated_to_month_num中的数据链接起来,以在输入字符串中设置替换逻辑?

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
from datetime import datetime
from calendar import monthrange
def getRange(m):
# default 5 days
days  = m.group(1)
if days == None:
days = 5
else:
days = int(days)
# month text to int
month = m.group(3).lower()
month = monthToInt(month)
# default year todays year
year  = m.group(5)
if year == None:
year = datetime.now().strftime('%Y')
# first and last day by type
type  = m.group(2)
if type == 'primeros':
first_day = 1
last_day = first_day + days
elif type == 'ultimos':
last_day = int(getLastDayFromMonth(month, year))
first_day = last_day - days
first_day = setMaskToNumber(first_day, 2)
last_day = setMaskToNumber(last_day, 2)
result = first_day+" -- "+last_day
return result
def setMaskToNumber(number, mask):
result = str(number)
while(len(result)<mask):
result = "0"+result
return result
def monthToInt(month):
es_month_dict = {"enero": 1, "febrero": 2, "marzo": 3, "abril": 4, "mayo": 5, "junio": 6, "julio": 7, "agosto": 8, "septiembre": 9, "octubre": 10, "noviembre": 11, "diciembre": 12}
month = es_month_dict.get(month)
return month
def getLastDayFromMonth(month, year):
r = monthrange(int(year), int(month))
return r[1]
#Examples:
#input_text = "hay 4 objetos los 3 primeros dias del mes de enero del 2020"  #example 1
input_text = "hay que ir alli los 10 ultimos dias del mes de julio del 2022"  #example 2
#input_text = "suelen ser algo frios los primeros dias del mes de noviembre"  #example 3
#input_text = "hay que plantar 5 calabazas los ultimos dias del mes de octubre del 2021"  #example 4
"""
"hay 4 objetos los (01 -- 03) del mes de enero del 2020"  #for the example 1
"hay que ir alli los (20 -- 30) del mes de julio del 2022"  #for the example 2, 30 - 10 = 20
"suelen ser algo frios los (01 -- 05) del mes de noviembre"  #for the example 3
"hay que plantar 5 calabazas (26 -- 31) del mes de octubre del 2021"  #for the example 4, 31 - 5 = 26
"""
m = re.search(r'([0-9]+s)?(primeros|ultimos) dias del mes de (w+)( del ([0-9]+))?', input_text)
if m:
range = getRange(m)
output = re.sub(r'([0-9]+s*)?(primeros|ultimos) dias', range, input_text)
print(output)

我把月份dict改为int,如果年份没有出现在句子中,我就用这一刻的年份。

最新更新