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:

其中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)

correct outputs名应该是:

"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,如果句子中没有年份,我就用这个时刻的年份.

Python相关问答推荐

C#使用程序从Python中执行Exec文件

如何在表中添加重复的列?

有没有一种ONE—LINER的方法给一个框架的每一行一个由整数和字符串组成的唯一id?

如何保持服务器发送的事件连接活动?

* 动态地 * 修饰Python中的递归函数

在单次扫描中创建列表

通过追加列表以极向聚合

在用于Python的Bokeh包中设置按钮的样式

在电影中向西北方向对齐""

504未连接IB API TWS错误—即使API连接显示已接受''

每次查询的流通股数量

python3中np. divide(x,y)和x/y有什么区别?'

上传文件并使用Panda打开时的Flask 问题

如何在Polars中创建条件增量列?

查找数据帧的给定列中是否存在特定值

使用pythonminidom过滤XML文件

排除NRRD文件中的多切片卷加载问题

如何仅使用数据帧操作获得特定的唯一数据帧组合?

对齐多个叠置多面Seborn CAT图

用考克斯回归的生存分析系列的真值是模棱两可的.