当前位置:网站首页>修改pyunit_time使得其支持‘xx~xx月’的时间文本
修改pyunit_time使得其支持‘xx~xx月’的时间文本
2022-07-05 15:10:00 【Channing Lewis】
因为需要使用时间文本转换为标准时间的功能,考察多种第三方包后选择了pyunit_time,但是还是有很多不尽人意的地方,所以把整个包拷出来,自己修改以满足需求。
今天发现pyunit_time不支持‘xx~xx月’的表述,例如“6~9月”只会识别出9月,但是“6月~9月”是可以识别出“6月”和“9月”,因此将“6~9月”转成“6月~9月”就可以了,其实本质上也是个时间单位省略的问题。
下面是原始代码(所在文件是/pyunit_time/filters.py):
def get_time_key(string) -> list:
"""根据字符串提取字符串中的时间关键词 比例: 国庆节的前一天晚上8点半 返回: ['国庆节', '前一天晚上8点半'] :param string: 关于口述话的时间字符串 :return: 时间关键词 """
keys, start, end = [], -1, -1
match = pattern.finditer(string)
for key in match:
start = key.start()
if start == end:
keys[-1] += key.group()
else:
keys.append(key.group())
end = key.end()
return keys
它采用的是正则匹配的方式,pattern调用了之前准备好正则表达式,像“x年”、‘x月’等,单独的数字匹配不上任何一个,所以会被忽略。
修改后:
def get_time_key(string) -> list:
"""根据字符串提取字符串中的时间关键词 比例: 国庆节的前一天晚上8点半 返回: ['国庆节', '前一天晚上8点半'] :param string: 关于口述话的时间字符串 :return: 时间关键词 """
if '~' in string: # lyc修改点,支持“xx~xx月”的表述
begin, *middle, finish = string.split('~')
if begin and finish and begin[-1].isdigit() and not finish[-1].isdigit():
string = string.replace('~', finish[-1] + '~')
keys, start, end = [], -1, -1
match = pattern.finditer(string)
for key in match:
start = key.start()
if start == end:
keys[-1] += key.group()
else:
keys.append(key.group())
end = key.end()
return keys
在原始处理之前将最后一个时间单位补充到“~”之前的单独的数字后,完成了转换,再测试就发现可以识别“6~9月”了。
边栏推荐
猜你喜欢

数据库学习——数据库安全性

Garbage collection mechanism of PHP (theoretical questions of PHP interview)

Explanation report of the explosion

MySQL之CRUD

Usage and usage instructions of JDBC connection pool

Crud de MySQL

超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜

qt creater断点调试程序详解

数学建模之层次分析法(含MATLAB代码)

Codasip为RISC-V处理器系列增加Veridify安全启动功能
随机推荐
Linear DP (basic questions have been updated)
Appium automation test foundation - appium basic operation API (II)
Bugku alert
Database learning - Database Security
The difference between abstract classes and interfaces in PHP (PHP interview theory question)
P6183 [USACO10MAR] The Rock Game S
Value series solution report
DVWA range clearance tutorial
Ten billion massage machine blue ocean, difficult to be a giant
百亿按摩仪蓝海,难出巨头
mapper.xml文件中的注释
Brief introduction of machine learning framework
lvgl 显示图片示例
wxml2canvas
JS topic - console log()
[recruitment position] infrastructure software developer
PHP high concurrency and large traffic solution (PHP interview theory question)
Common PHP interview questions (1) (written PHP interview questions)
Calculate weight and comprehensive score by R entropy weight method
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability