当前位置:网站首页>修改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月”了。
边栏推荐
- F. Min cost string problem solving Report
- OSI 七层模型
- Brief introduction of machine learning framework
- qt creater断点调试程序详解
- 六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)
- Ionic Cordova project modification plug-in
- Anti shake and throttling
- Example of lvgl display picture
- Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
- I spring and autumn blasting-2
猜你喜欢

D-snow halo solution

Redis' transaction mechanism

OSI 七层模型

【简记】解决IDE golang 代码飘红报错

RepLKNet:不是大卷积不好,而是卷积不够大,31x31卷积了解一下 | CVPR 2022

一文搞定vscode编写go程序

swiper. JS to achieve barrage effect

lvgl 显示图片示例

How to paste the contents copied by the computer into mobaxterm? How to copy and paste

Bugku's eyes are not real
随机推荐
lvgl 显示图片示例
I include of spring and Autumn
F. Min cost string problem solving Report
SQL Server learning notes
Appium automation test foundation - appium basic operation API (I)
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Where is the operation of convertible bond renewal? Is it safer and more reliable to open an account
Appium automation test foundation - appium basic operation API (II)
力扣今日题-729. 我的日程安排表 I
go学习 ------jwt的相关知识
CODING DevSecOps 助力金融企业跑出数字加速度
What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
P1451 求细胞数量/1329:【例8.2】细胞
Array sorting num ranking merge in ascending order
Common redis data types and application scenarios
Value series solution report
Redis distributed lock principle and its implementation with PHP (2)
OSI 七层模型
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Coding devsecops helps financial enterprises run out of digital acceleration