当前位置:网站首页>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
” 了 .
边栏推荐
猜你喜欢
Bugku cyberpunk
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
Anti shake and throttling
Good article inventory
18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度
RepLKNet:不是大卷积不好,而是卷积不够大,31x31卷积了解一下 | CVPR 2022
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Data communication foundation - route republication
lv_font_conv离线转换
Summary of the third class
随机推荐
Redis distributed lock principle and its implementation with PHP (1)
wyt 。。
MySQL 巨坑:update 更新慎用影响行数做判断!!!
I spring web upload
keep-alive
How can the boss choose programmers to help me with development?
F. Weights assignment for tree edges problem solving Report
SQL injection sqllabs (basic challenges) 1-10
Thymeleaf uses background custom tool classes to process text
How to introduce devsecops into enterprises?
I spring and autumn blasting-1
Object. defineProperty() - VS - new Proxy()
Bugku's steganography
The difference between SQL Server char nchar varchar and nvarchar
六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)
Bugku's Eval
vlunhub- BoredHackerBlog Social Network
Information collection of penetration test
"Sequelae" of the withdrawal of community group purchase from the city
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability