Skip to main content

将中文时间表达词转为相应的时间字符串,支持时间点,时间段,时间间隔。

Project description

cntime-nlp

License

简介

fork 自 Kelab/ChineseTimeNLP.

配置

可以传入自定义的 pattern,默认 pattern 也可以通过 from ChineseTimeNLP import pattern 导入。

TimeNormalizer(isPreferFuture=True, pattern=None):

对于下午两点、晚上十点这样的词汇,在不特别指明的情况下,默认返回明天的时间点。

安装使用

安装:

pip install cntime-nlp

使用:

from ChineseTimeNLP import TimeNormalizer
tn = TimeNormalizer()
res = tn.parse(target=u"三天后")  # target 为待分析语句,baseTime 为基准时间默认是当前时间
print(res)

功能说明

用于句子中时间词的抽取和转换
详情请见 Test.py

tn = TimeNormalizer(isPreferFuture=False)

res = tn.parse(target=u'星期天晚上')  # target为待分析语句,baseTime为基准时间默认是当前时间
print(res)
print('====')

res = tn.parse(target=u'晚上8点到上午10点之间')  # target为待分析语句,baseTime为基准时间默认是当前时间
print(res)
print('====')

res = tn.parse(
    target=u'2013年二月二十八日下午四点三十分二十九秒',
    baseTime='2013-02-28 16:30:29')  # target为待分析语句,baseTime为基准时间默认是当前时间
print(res)
print('====')

res = tn.parse(
    target=u'我需要大概33天2分钟四秒',
    baseTime='2013-02-28 16:30:29')  # target为待分析语句,baseTime为基准时间默认是当前时间
print(res)
print('====')

res = tn.parse(target=u'今年儿童节晚上九点一刻')  # target为待分析语句,baseTime为基准时间默认是当前时间
print(res)
print('====')

res = tn.parse(target=u'三日')  # target为待分析语句,baseTime为基准时间默认是当前时间
print(res)
print('====')

res = tn.parse(target=u'7点4')  # target为待分析语句,baseTime为基准时间默认是当前时间
print(res)
print('====')

res = tn.parse(target=u'今年春分')
print(res)
print('====')

res = tn.parse(target=u'7000万')
print(res)
print('====')

res = tn.parse(target=u'7百')
print(res)
print('====')

res = tn.parse(target=u'7千')
print(res)
print('====')

结果:

目标字符串:  星期天晚上
基础时间 2019-7-28-15-47-27
temp ['星期7晚上']
{"type": "timestamp", "timestamp": "2019-07-28 20:00:00"}
====
目标字符串:  晚上8点到上午10点之间
基础时间 2019-7-28-15-47-27
temp ['晚上8点', '上午10点']
{"type": "timespan", "timespan": ["2019-07-28 20:00:00", "2019-07-28 10:00:00"]}
====
目标字符串:  2013年二月二十八日下午四点三十分二十九秒
基础时间 2013-2-28-16-30-29
temp ['2013年2月28日下午4点30分29秒']
{"type": "timestamp", "timestamp": "2013-02-28 16:30:29"}
====
目标字符串:  我需要大概33天2分钟四秒
基础时间 2013-2-28-16-30-29
temp ['33天2分钟4秒']
timedelta:  33 days, 0:02:04
{"type": "timedelta", "timedelta": {"year": 0, "month": 1, "day": 3, "hour": 0, "minute": 2, "second": 4}}
====
目标字符串:  今年儿童节晚上九点一刻
基础时间 2019-7-28-15-47-27
temp ['今年儿童节晚上9点1刻']
{"type": "timestamp", "timestamp": "2019-06-01 21:15:00"}
====
目标字符串:  三日
基础时间 2019-7-28-15-47-27
temp ['3日']
{"type": "timestamp", "timestamp": "2019-07-03 00:00:00"}
====
目标字符串:  7点4
基础时间 2019-7-28-15-47-27
temp ['7点4']
{"type": "timestamp", "timestamp": "2019-07-28 07:04:00"}
====
目标字符串:  今年春分
基础时间 2019-7-28-15-47-27
temp ['今年春分']
{"type": "timestamp", "timestamp": "2019-03-21 00:00:00"}
====
目标字符串:  7000万
基础时间 2019-7-28-15-47-27
temp ['70000000']
{"type": "error", "error": "no time pattern could be extracted."}
====
目标字符串:  7百
基础时间 2019-7-28-15-47-27
temp []
{"type": "error", "error": "no time pattern could be extracted."}
====
目标字符串:  7千
基础时间 2019-7-28-15-47-27
temp []
{"type": "error", "error": "no time pattern could be extracted."}
====

使用方式

Test.py

TODO

问题 现在版本 正确
晚上8点到上午10点之间 ["2018-03-16 20:00:00", "2018-03-16 22:00:00"] ["2018-03-16 20:00:00", "2018-03-17 10:00:00"]"

声明

为了适合自己的编程习惯,删除了代码中部分文件的头部注释信息,信息格式如下,特此声明:

# -*- coding: utf-8 -*-
# @Time    : xxxxxxxx
# @Author  : zhm
# @File    : xxxxx
# @Software: PyCharm
# @Changed : tianyuningmou

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cntime-nlp-0.1.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

cntime_nlp-0.1-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

Details for the file cntime-nlp-0.1.tar.gz.

File metadata

  • Download URL: cntime-nlp-0.1.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.8

File hashes

Hashes for cntime-nlp-0.1.tar.gz
Algorithm Hash digest
SHA256 48f1de97da98c8efe6067ce1edaa59c0921aa31306eaaf6093597f42e78defab
MD5 c9b10c3b9d39e0833ec4e12e9fbfede4
BLAKE2b-256 a0feaa1b54e2304af332d15c46f6213144974e18e3903879ec5cc3b5f9f4a86d

See more details on using hashes here.

Provenance

File details

Details for the file cntime_nlp-0.1-py3-none-any.whl.

File metadata

  • Download URL: cntime_nlp-0.1-py3-none-any.whl
  • Upload date:
  • Size: 25.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.8

File hashes

Hashes for cntime_nlp-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3840b3a987c31364d95765aa3333362689b9ce39fc72cfb4c46a2ddf1f7afa63
MD5 2516819b6501a51a6173991576cb8a2a
BLAKE2b-256 51798cbb1134f253089d3791b63ae59fce1c77cc8ff2c7ccfcad560d4fcd2524

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page