当前位置:网站首页>修改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月”了。
边栏推荐
- MySQL之CRUD
- 社区团购撤城“后遗症”
- What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
- MySQL 巨坑:update 更新慎用影响行数做判断!!!
- 百亿按摩仪蓝海,难出巨头
- PHP high concurrency and large traffic solution (PHP interview theory question)
- I spring and autumn blasting-1
- Write a go program with vscode in one article
- Bugku cyberpunk
- Thymeleaf uses background custom tool classes to process text
猜你喜欢

把 ”中台“ 的思想迁移到代码中去

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

First PR notes
![1330: [example 8.3] minimum steps](/img/69/9cb13ac4f47979b498fa2254894ed1.gif)
1330: [example 8.3] minimum steps

Ionic Cordova project modification plug-in
![[brief notes] solve the problem of IDE golang code red and error reporting](/img/b6/0b2ea06eb5fbe651ff9247b109fa15.png)
[brief notes] solve the problem of IDE golang code red and error reporting

Stop B makes short videos, learns Tiktok to die, learns YouTube to live?

Bugku's Ah Da

Nine hours, nine people, nine doors problem solving Report

ionic cordova项目修改插件
随机推荐
[brief notes] solve the problem of IDE golang code red and error reporting
Leetcode: Shortest Word Distance II
Explanation report of the explosion
Linear DP (basic questions have been updated)
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Common PHP interview questions (1) (written PHP interview questions)
Detailed explanation of C language branch statements
1330:【例8.3】最少步数
What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
Transfer the idea of "Zhongtai" to the code
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
lv_ font_ Conv offline conversion
Appium自动化测试基础 — APPium基础操作API(一)
CODING DevSecOps 助力金融企业跑出数字加速度
MySQL----函数
MySQL5.7的JSON基本操作
Misc Basic test method and knowledge points of CTF
F. Weights assignment for tree edges problem solving Report
Crud of MySQL
Bugku's eyes are not real