当前位置:网站首页>修改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月”了。
边栏推荐
- 把 ”中台“ 的思想迁移到代码中去
- Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
- 超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
- Ctfshow web entry command execution
- What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
- MySQL表字段调整
- How to introduce devsecops into enterprises?
- Redis distributed lock principle and its implementation with PHP (2)
- Your childhood happiness was contracted by it
- 【 note 】 résoudre l'erreur de code IDE golang
猜你喜欢

keep-alive

Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking

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

亿咖通科技通过ISO27001与ISO21434安全管理体系认证

"Sequelae" of the withdrawal of community group purchase from the city

How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection

Bugku's Ping

Optional parameters in the for loop

Object. defineProperty() - VS - new Proxy()

Thymeleaf uses background custom tool classes to process text
随机推荐
Advanced level of static and extern
"Sequelae" of the withdrawal of community group purchase from the city
OSI seven layer model
Xiao Sha's arithmetic problem solving Report
数据库学习——数据库安全性
mapper. Comments in XML files
Crud de MySQL
lv_ font_ Conv offline conversion
Bugku's eyes are not real
爱可可AI前沿推介(7.5)
Definition of episodic and batch
Where is the operation of convertible bond renewal? Is it safer and more reliable to open an account
华为哈勃化身硬科技IPO收割机
[JVM] operation instruction
Brief introduction of machine learning framework
Go language programming specification combing summary
Common interview questions about swoole
Temporary cramming before DFS examination
MySQL5.7的JSON基本操作
Go learning ----- relevant knowledge of JWT