Skip to content

輸入 "过80分后", "过120秒后" 後導致程式拋出異常 #8

Description

@r888800009

提取時間時,傳入的 arrow 的 format 超過範圍無法剖析而拋出異常。

重現問題

輸入超過範圍值,如下面兩種情況皆會發生異常

"过120秒后"

from ChineseTimeNLP import TimeNormalizer
tn = TimeNormalizer()
tn.parse(target="过120秒后")
arrow.parser.ParserMatchError: Failed to match 'YYYY-M-D-H-m-s' when parsing '2021-6-29-18-56-120'.

可以看到秒被填寫成 120 導致異常

"过80分后"

tn.parse(target="过80分后")
ValueError: minute must be in 0..59

預期回應

  1. "过80分后" 轉換成 1時 20分, "过120秒后" 轉成兩分
  2. 或者回傳{'type': 'error',...},讓程式可以排除例外狀態等等

可能的解決方法

如果是前者,可以透過 arrow 的 shift() 處理,後者的話只需要檢查範圍。
前者如下,比較之後可以確實發現增加三分鐘

a  = arw.now()
>>> a.shift(seconds=120, minutes=1)
<Arrow [2021-06-29T22:16:04.830575+08:00]>
>>> a
<Arrow [2021-06-29T22:13:04.830575+08:00]>

backtrace

>>> tn.parse(target="过120秒后")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/normalizer.py", line 41, in parse
    return self.extract()
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/normalizer.py", line 87, in extract
    res.append(TimeUnit(temp[i], self, contextTp))
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/unit.py", line 39, in __init__
    self.time_normalization()
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/unit.py", line 60, in time_normalization
    self.modifyTimeBase()
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/unit.py", line 993, in modifyTimeBase
    self.normalizer.baseTime = grid2arrow(arr)
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/helpers/arrow_helper.py", line 10, in grid2arrow
    return get("-".join(str_list), "YYYY-M-D-H-m-s")
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/arrow/api.py", line 91, in get
    return _factory.get(*args, **kwargs)
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/arrow/factory.py", line 292, in get
    dt = parser.DateTimeParser(locale).parse(
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/arrow/parser.py", line 325, in parse
    raise ParserMatchError(
arrow.parser.ParserMatchError: Failed to match 'YYYY-M-D-H-m-s' when parsing '2021-6-29-19-16-120'.
>>> tn.parse(target="过80分后")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/normalizer.py", line 41, in parse
    return self.extract()
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/normalizer.py", line 87, in extract
    res.append(TimeUnit(temp[i], self, contextTp))
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/unit.py", line 39, in __init__
    self.time_normalization()
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/unit.py", line 60, in time_normalization
    self.modifyTimeBase()
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/unit.py", line 993, in modifyTimeBase
    self.normalizer.baseTime = grid2arrow(arr)
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/ChineseTimeNLP/helpers/arrow_helper.py", line 10, in grid2arrow
    return get("-".join(str_list), "YYYY-M-D-H-m-s")
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/arrow/api.py", line 91, in get
    return _factory.get(*args, **kwargs)
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/arrow/factory.py", line 292, in get
    dt = parser.DateTimeParser(locale).parse(
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/arrow/parser.py", line 346, in parse
    return self._build_datetime(parts)
  File "/{python-path}/envs/python38/lib/python3.8/site-packages/arrow/parser.py", line 698, in _build_datetime
    datetime(
ValueError: minute must be in 0..59

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions