Skip to content

Commit 140ffcd

Browse files
committed
fix time_parser bug
1 parent 1cc856a commit 140ffcd

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<a alt="Downloads">
1212
<img src="https://pepy.tech/badge/jionlp/month" /></a>
1313
<a alt="Version">
14-
<img src="https://img.shields.io/badge/version-1.4.31-green" /></a>
14+
<img src="https://img.shields.io/badge/version-1.4.32-green" /></a>
1515
<a href="https://github.com/dongrixinyu/JioNLP/pulse" alt="Activity">
1616
<img src="https://img.shields.io/github/commit-activity/m/dongrixinyu/JioNLP?color=blue" /></a>
1717
</p>

jionlp/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# website: www.jionlp.com
99

1010

11-
__version__ = '1.4.31'
11+
__version__ = '1.4.32'
1212

1313

1414
import os

jionlp/gadget/time_parser.py

+38-3
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,19 @@ def _check_limit_time_base(self, first_time_string, second_time_string,
921921
else:
922922
return first_full_time_handler
923923

924-
def _adjust_underlying_future_time(self, time_string):
924+
def _adjust_underlying_future_time(
925+
self, time_string, first_full_time_handler, second_full_time_handler):
926+
"""
927+
928+
Args:
929+
time_string: 时间字符串,进行未来时间扩展
930+
first_full_time_handler: 假设不进行未来时间扩展,正常解析的时间起点
931+
second_full_time_handler: 假设不进行未来时间扩展,正常解析的时间终点
932+
933+
Returns:
934+
扩展后的未来时间
935+
"""
936+
925937
# 检查哪些时间字符串可以被扩展为 未来字符串,并将其调节
926938
ymd_time_patterns = [
927939
# 时间点型
@@ -977,7 +989,29 @@ def _adjust_underlying_future_time(self, time_string):
977989
time_string = '下' + time_string
978990

979991
elif matched_unit in ['时', '点']:
980-
time_string = '明天' + time_string
992+
# 此时需要判断 time_base 里的时,和字符串中的时,哪个发生在前,哪个在后。
993+
# 如果 time_base 发生在前,如 [2023, 1, 6, 16, 30, 0], 而时间字符串为 “下午6点”
994+
# 则此时不改变字符串,直接返回即可。
995+
if first_full_time_handler[3] != -1 and self.time_base_handler[3] != -1: # 小时时间均存在
996+
if first_full_time_handler[3] > self.time_base_handler[3]: # 在 小时 字段符合不扩增至第二天的条件
997+
pass
998+
elif first_full_time_handler[3] < self.time_base_handler[3]: # 符合扩增条件
999+
time_string = '明天' + time_string
1000+
else: # 还需比较 分钟
1001+
1002+
if first_full_time_handler[4] != -1 and self.time_base_handler[4] != -1: # 分钟时间均存在
1003+
if first_full_time_handler[4] > self.time_base_handler[4]: # 在分钟字段不扩增
1004+
pass
1005+
elif first_full_time_handler[4] < self.time_base_handler[4]: # 符合扩增条件
1006+
time_string = '明天' + time_string
1007+
else:
1008+
# 暂时不考虑 秒钟 ,此时直接将时间扩增至第二天
1009+
time_string = '明天' + time_string
1010+
else:
1011+
time_string = '明天' + time_string
1012+
else:
1013+
# 时间基未指明,因此直接跨到第二天
1014+
time_string = '明天' + time_string
9811015
else:
9821016
pass
9831017

@@ -1082,7 +1116,8 @@ def parse_time_span_point(self, time_string):
10821116
# 检查 handler,确定是否按 ret_future 未来时间解析
10831117
if self.ret_future:
10841118

1085-
future_time_string = self._adjust_underlying_future_time(time_string)
1119+
future_time_string = self._adjust_underlying_future_time(
1120+
time_string, first_full_time_handler, second_full_time_handler)
10861121
first_full_time_handler, second_full_time_handler, time_type, blur_time = self.parse_time_point(
10871122
future_time_string, self.time_base_handler)
10881123

test/test_time_parser.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,9 @@ def test_time_parser(self):
10401040
['妇女节', _ts_2,
10411041
{'type': 'time_point', 'definition': 'accurate', 'time': ['2022-03-08 00:00:00', '2022-03-08 23:59:59']}],
10421042
['下午三点', _ts_2,
1043-
{'type': 'time_point', 'definition': 'accurate', 'time': ['2021-09-02 15:00:00', '2021-09-02 15:59:59']}]
1043+
{'type': 'time_point', 'definition': 'accurate', 'time': ['2021-09-02 15:00:00', '2021-09-02 15:59:59']}],
1044+
['下午4点', _ts_2,
1045+
{'type': 'time_point', 'definition': 'accurate', 'time': ['2021-09-01 16:00:00', '2021-09-01 16:59:59']}]
10441046
]
10451047

10461048
for item in time_string_list:

0 commit comments

Comments
 (0)