当前位置:网站首页>修改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月
”了。
边栏推荐
- Common redis data types and application scenarios
- [brief notes] solve the problem of IDE golang code red and error reporting
- Bugku's Eval
- How to introduce devsecops into enterprises?
- What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
- 把 ”中台“ 的思想迁移到代码中去
- wxml2canvas
- sql server char nchar varchar和nvarchar的区别
- Common MySQL interview questions
- I spring and autumn blasting-1
猜你喜欢
Your childhood happiness was contracted by it
P6183 [USACO10MAR] The Rock Game S
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
CSRF, XSS science popularization and defense
lvgl 显示图片示例
Talk about your understanding of microservices (PHP interview theory question)
Bugku alert
亿咖通科技通过ISO27001与ISO21434安全管理体系认证
Bugku easy_ nbt
OSI 七层模型
随机推荐
qt creater断点调试程序详解
Common interview questions about swoole
What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
Usage and usage instructions of JDBC connection pool
把 ”中台“ 的思想迁移到代码中去
[JVM] operation instruction
DVWA range clearance tutorial
数据库学习——数据库安全性
OSI 七层模型
Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
【简记】解决IDE golang 代码飘红报错
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
String modification problem solving Report
The difference between SQL Server char nchar varchar and nvarchar
Huiyuan, 30, is going to have a new owner
基于OpenHarmony的智能金属探测器
Reasons and solutions for redis cache penetration and cache avalanche
Brief introduction of machine learning framework
Creation and optimization of MySQL index
PHP high concurrency and large traffic solution (PHP interview theory question)