当前位置:网站首页>Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
2022-07-05 15:44:00 【Channing Lewis】
Because you need to use the function of converting time text into standard time , After investigating a variety of third-party packages, I chose pyunit_time, But there are still many unsatisfactory places , So copy out the whole bag , Modify yourself to meet your needs .
Found today pyunit_time I won't support it ‘xx~xx month ’ Statement of the , for example “6~9 month ” Will only recognize 9 month , however “6 month ~9 month ” Can recognize “6 month ” and “9 month ”, So it will “6~9 month ” Turn into “6 month ~9 month ” That's all right. , In fact, it is essentially a problem of omitting time units .
Here is the original code ( The file is /pyunit_time/filters.py):
def get_time_key(string) -> list:
""" Extract the time keyword in the string according to the string The proportion : The night before national day 8 Half past six return : [' National Day ', ' The night before 8 Half past six '] :param string: About the time string of dictation :return: Time keywords """
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
It uses regular matching ,pattern Call the regular expression prepared before , image “x year ”、‘x month ’ etc. , Individual numbers don't match any , So it will be ignored .
After modification :
def get_time_key(string) -> list:
""" Extract the time keyword in the string according to the string The proportion : The night before national day 8 Half past six return : [' National Day ', ' The night before 8 Half past six '] :param string: About the time string of dictation :return: Time keywords """
if '~' in string: # lyc Modify the point , Support “xx~xx month ” Statement of the
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
Add the last time unit to... Before the original processing “~” After the previous separate number , Completed the conversion , Retest and find that you can recognize “6~9 month ” 了 .
边栏推荐
- Noi / 1.5 06: element maximum span value of integer sequence
- SQL injection sqllabs (basic challenges) 1-10
- What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
- 六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)
- The elimination strategy of redis
- MySQL5.7的JSON基本操作
- Data communication foundation ACL access control list
- go学习 ------jwt的相关知识
- 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
- 把 ”中台“ 的思想迁移到代码中去
猜你喜欢

【 note 】 résoudre l'erreur de code IDE golang

Ionic Cordova project modification plug-in

Data communication foundation - Ethernet port mirroring and link aggregation

D-snow halo solution

Appium自动化测试基础 — APPium基础操作API(一)

JS knowledge points-01

Number protection AXB function! (essence)

wxml2canvas

Appium automation test foundation - appium basic operation API (I)

I spring and autumn blasting-1
随机推荐
Good article inventory
go学习 ------jwt的相关知识
The difference between abstract classes and interfaces in PHP (PHP interview theory question)
I spring and autumn blasting-2
Noi / 1.4 07: collect bottle caps to win awards
力扣今日题-729. 我的日程安排表 I
lv_ font_ Conv offline conversion
Array sorting num ranking merge in ascending order
Bugku's Ping
ICML 2022 | explore the best architecture and training method of language model
I spring web upload
Arduino控制微小的六足3D打印机器人
OceanBase社区版之OBD方式部署方式本地安装
Data communication foundation smart_ Link_&_ Monitor_ Link
CSRF, XSS science popularization and defense
2.3 learning content
Common interview questions about swoole
"Sequelae" of the withdrawal of community group purchase from the city
Redis distributed lock principle and its implementation with PHP (2)
如何将 DevSecOps 引入企业?