简体   繁体   English

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

[英]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: Day的字符串根据所属月份的转换规则:

With n as a generical number \d{1,2} :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)

Or considering 5 as standard, if a number is not explicitly indicated, so we will assume n = 5 days:或者以 5 为标准,如果没有明确指出数字,那么我们将假设n = 5天:

"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?我如何将字典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)

I changed the month dict to int and if the years doesn't appears in the sentence I used the year of this moment.我将 month dict 更改为 int,如果年份没有出现在我使用这一刻的年份的句子中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个字典的键和另一个字典的值构建一个新字典 - Build a new dictionary from the keys of one dictionary and the values of another dictionary 从另一个字典的值替换字典键 - Replace dictionary keys from values of another dictionary 通过比较来自另一本字典的键,从一本字典返回值 - Return values from one dictionary by comparing keys from another dictionary 如果两个字典的键匹配,则将一个字典的键替换为另一个字典的值 - Replace the keys of one dictionary with the values from another IF the keys of both dictionaries match 用另一本词典中的键替换一个词典中的值 - Replacing values in one dictionary with the keys in another dictionary 如何根据匹配键将一个字典中的值添加到另一个字典中? - How to add values from one dictionary to another based on matching keys? Python 使用相同的键将一个字典中的值附加到另一个字典 - Python appending values from one dictionary to another with the same keys 从一个字典的键和另一个字典 python 的对应值创建字典 - Create a dictionary from the keys of one dictionary and corresponding values of another dictionary python 将字典中的值作为键插入另一个字典 - Inserting values from a dictionary as keys in another dictionary 用Python中另一本词典的键值替换一个词典中的键 - Replace a key in one dictionary with the value of the key from another dictionary in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM