ChineseTimeNLP
简介
这是 Time-NLP 的 Python3 版本。
fork 自 zhanzecheng/Time_NLP
相关链接:
- Python2 版本 https://github.com/ryanInf/Time-NLPY/tree/Python2%E7%89%88%E6%9C%AC
- Python3 版本 https://github.com/ryanInf/Time-NLPY
- Java 版本 https://github.com/shinyke/Time-NLP
- PHP 版本 https://github.com/crazywhalecc/Time-NLP-PHP
配置
可以传入自定义的 pattern,默认 pattern 也可以通过 from ChineseTimeNLP import pattern 导入。
TimeNormalizer(isPreferFuture=True, pattern=None):
对于下午两点、晚上十点这样的词汇,在不特别指明的情况下,默认返回明天的时间点。
安装使用
安装:
pip install ChineseTimeNLP
使用:
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
Metadata
Release files for ChineseTimeNLP 3.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| ChineseTimeNLP-3.0.1.tar.gz | 22.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ChineseTimeNLP-3.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 48.1 kB
Release files / ChineseTimeNLP-3.0.1.tar.gz
| Download URL | ChineseTimeNLP-3.0.1.tar.gz |
|---|---|
| Size | 22.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eba0b81320be781dc5eac6b21b70d3c1e875dcbdd9cf28af312495dd131677fe
|
|
BLAKE2b-256 checksum How to use checksums |
32f137a54124455b6f7ecdf7360da8c0df548112286f3ea62a857a2d255beff1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
|
Release files / ChineseTimeNLP-3.0.1-py3-none-any.whl
| Download URL | ChineseTimeNLP-3.0.1-py3-none-any.whl |
|---|---|
| Size | 25.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9a4aada2ad7c81688ebc1893fb87c6e88ed9a101d3fed6bb98839f248e35ca07
|
|
BLAKE2b-256 checksum How to use checksums |
dd24ea91b30f783af879f26640c62fd8d03a526e7fd03085a5f28d38ed970397
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
|
Security for ChineseTimeNLP 3.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.