Search code examples
pythonpython-3.xregexdictionaryregex-group

Link values from one dictionary with keys from another dictionary and replace one for another within a string with regex


import re

def normalize_intervals_to_2_digits(match):
    input_text_substring = match.group()
    input_text_substring = re.sub(r"(\b\d)(?!\d)", r"0\1", 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

Day's string conversion rules according to the month they belong to:

With n as a generical number \d{1,2} :

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

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

Or considering 5 as standard, if a number is not explicitly indicated , so we will assume n = 5 days :

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

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

The correct outputs should be these:

"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

How could I link the data from dictionary es_month_dict with those of the days from dictionary quantity_days_associated_to_month_num, to set up the logic of replacements in the input string?


Solution

  • #!/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)
    

    I changed the month dict to int and if the years doesn't appears in the sentence I used the year of this moment.