ChineseDateTimeNLP


NameChineseDateTimeNLP JSON
Version 1.0.5 PyPI version JSON
download
home_pagehttps://github.com/xiaoyaosharenfan/ChineseDateTimeNLP
Summary将中文时间表达词转为相应的时间字符串,支持时间点,时间段,时间间隔。
upload_time2023-10-16 07:35:45
maintainer
docs_urlNone
authorAlex
requires_python
licenseMIT Licence
keywords nlp time nlp date nlp
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ChineseDateTimeNLP

[![PyPI](https://img.shields.io/pypi/v/ChineseDateTimeNLP.svg)](https://pypi.python.org/pypi/ChineseDateTimeNLP)
![Python Version](https://img.shields.io/badge/python-3.7+-blue.svg)

[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://pypi.org/project/isort/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)

[![Downloads](https://pepy.tech/badge/chinesedatetimenlp)](https://pepy.tech/project/chinesedatetimenlp)
[![Downloads](https://pepy.tech/badge/chinesedatetimenlp/week)](https://pepy.tech/project/chinesedatetimenlp)

## 简介

这是 Time-NLP 的 Python3 版本。  
fork 自 [Kelab/ChineseTimeNLP](https://github.com/Kelab/ChineseTimeNLP)  

相关链接:

- 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 ChineseDateTimeNLP import pattern` 导入。

```py
TimeNormalizer(isPreferFuture=True, pattern=None):
```

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

## 安装使用

安装:

```bash
pip install ChineseDateTimeNLP
```

使用:

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

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

```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('====')

```

结果:

```sh
目标字符串:  星期天晚上
基础时间 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"]" |

## 声明

1. 增加了"礼拜xx"的识别
2. 修复了"2023/10/09"识别错误的问题
3. 修复了"2023-10-09"被识别为时间区间的问题
4. 增加了"10/09"的识别
5. 修复了"10-09"在系统时间是"2023-10-09"的时候,被推测为未来日期"2024-10-09"的问题
6. 增加了"1009"的识别
7. 优化对"下个月明天"这类相对日期的识别

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/xiaoyaosharenfan/ChineseDateTimeNLP",
    "name": "ChineseDateTimeNLP",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "nlp,time nlp,date nlp",
    "author": "Alex",
    "author_email": "fanbinthu@163.com",
    "download_url": "https://files.pythonhosted.org/packages/5c/17/145054aa35939f7b4aa65910f11330cf5903763f50531d1181ae76613c64/ChineseDateTimeNLP-1.0.5.tar.gz",
    "platform": null,
    "description": "# ChineseDateTimeNLP\n\n[![PyPI](https://img.shields.io/pypi/v/ChineseDateTimeNLP.svg)](https://pypi.python.org/pypi/ChineseDateTimeNLP)\n![Python Version](https://img.shields.io/badge/python-3.7+-blue.svg)\n\n[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://pypi.org/project/isort/)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)\n\n[![Downloads](https://pepy.tech/badge/chinesedatetimenlp)](https://pepy.tech/project/chinesedatetimenlp)\n[![Downloads](https://pepy.tech/badge/chinesedatetimenlp/week)](https://pepy.tech/project/chinesedatetimenlp)\n\n## \u7b80\u4ecb\n\n\u8fd9\u662f Time-NLP \u7684 Python3 \u7248\u672c\u3002  \nfork \u81ea [Kelab/ChineseTimeNLP](https://github.com/Kelab/ChineseTimeNLP)  \n\n\u76f8\u5173\u94fe\u63a5\uff1a\n\n- Python2 \u7248\u672c <https://github.com/ryanInf/Time-NLPY/tree/Python2%E7%89%88%E6%9C%AC>\n- Python3 \u7248\u672c <https://github.com/ryanInf/Time-NLPY>\n- Java \u7248\u672c <https://github.com/shinyke/Time-NLP>\n- PHP \u7248\u672c <https://github.com/crazywhalecc/Time-NLP-PHP>\n\n## \u914d\u7f6e\n\n\u53ef\u4ee5\u4f20\u5165\u81ea\u5b9a\u4e49\u7684 pattern\uff0c\u9ed8\u8ba4 pattern \u4e5f\u53ef\u4ee5\u901a\u8fc7 `from ChineseDateTimeNLP import pattern` \u5bfc\u5165\u3002\n\n```py\nTimeNormalizer(isPreferFuture=True, pattern=None):\n```\n\n\u5bf9\u4e8e\u4e0b\u5348\u4e24\u70b9\u3001\u665a\u4e0a\u5341\u70b9\u8fd9\u6837\u7684\u8bcd\u6c47\uff0c\u5728\u4e0d\u7279\u522b\u6307\u660e\u7684\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u8fd4\u56de\u660e\u5929\u7684\u65f6\u95f4\u70b9\u3002\n\n## \u5b89\u88c5\u4f7f\u7528\n\n\u5b89\u88c5\uff1a\n\n```bash\npip install ChineseDateTimeNLP\n```\n\n\u4f7f\u7528:\n\n```py\nfrom ChineseDateTimeNLP        import TimeNormalizer\ntn = TimeNormalizer()\nres = tn.parse(target=u\"\u4e09\u5929\u540e\")  # target \u4e3a\u5f85\u5206\u6790\u8bed\u53e5\uff0cbaseTime \u4e3a\u57fa\u51c6\u65f6\u95f4\u9ed8\u8ba4\u662f\u5f53\u524d\u65f6\u95f4\nprint(res)\n```\n## \u529f\u80fd\u8bf4\u660e\n\n\u7528\u4e8e\u53e5\u5b50\u4e2d\u65f6\u95f4\u8bcd\u7684\u62bd\u53d6\u548c\u8f6c\u6362  \n\u8be6\u60c5\u8bf7\u89c1 `Test.py`\n\n```py\ntn = TimeNormalizer(isPreferFuture=False)\n\nres = tn.parse(target=u'\u661f\u671f\u5929\u665a\u4e0a')  # target\u4e3a\u5f85\u5206\u6790\u8bed\u53e5\uff0cbaseTime\u4e3a\u57fa\u51c6\u65f6\u95f4\u9ed8\u8ba4\u662f\u5f53\u524d\u65f6\u95f4\nprint(res)\nprint('====')\n\nres = tn.parse(target=u'\u665a\u4e0a8\u70b9\u5230\u4e0a\u534810\u70b9\u4e4b\u95f4')  # target\u4e3a\u5f85\u5206\u6790\u8bed\u53e5\uff0cbaseTime\u4e3a\u57fa\u51c6\u65f6\u95f4\u9ed8\u8ba4\u662f\u5f53\u524d\u65f6\u95f4\nprint(res)\nprint('====')\n\nres = tn.parse(\n    target=u'2013\u5e74\u4e8c\u6708\u4e8c\u5341\u516b\u65e5\u4e0b\u5348\u56db\u70b9\u4e09\u5341\u5206\u4e8c\u5341\u4e5d\u79d2',\n    baseTime='2013-02-28 16:30:29')  # target\u4e3a\u5f85\u5206\u6790\u8bed\u53e5\uff0cbaseTime\u4e3a\u57fa\u51c6\u65f6\u95f4\u9ed8\u8ba4\u662f\u5f53\u524d\u65f6\u95f4\nprint(res)\nprint('====')\n\nres = tn.parse(\n    target=u'\u6211\u9700\u8981\u5927\u698233\u59292\u5206\u949f\u56db\u79d2',\n    baseTime='2013-02-28 16:30:29')  # target\u4e3a\u5f85\u5206\u6790\u8bed\u53e5\uff0cbaseTime\u4e3a\u57fa\u51c6\u65f6\u95f4\u9ed8\u8ba4\u662f\u5f53\u524d\u65f6\u95f4\nprint(res)\nprint('====')\n\nres = tn.parse(target=u'\u4eca\u5e74\u513f\u7ae5\u8282\u665a\u4e0a\u4e5d\u70b9\u4e00\u523b')  # target\u4e3a\u5f85\u5206\u6790\u8bed\u53e5\uff0cbaseTime\u4e3a\u57fa\u51c6\u65f6\u95f4\u9ed8\u8ba4\u662f\u5f53\u524d\u65f6\u95f4\nprint(res)\nprint('====')\n\nres = tn.parse(target=u'\u4e09\u65e5')  # target\u4e3a\u5f85\u5206\u6790\u8bed\u53e5\uff0cbaseTime\u4e3a\u57fa\u51c6\u65f6\u95f4\u9ed8\u8ba4\u662f\u5f53\u524d\u65f6\u95f4\nprint(res)\nprint('====')\n\nres = tn.parse(target=u'7\u70b94')  # target\u4e3a\u5f85\u5206\u6790\u8bed\u53e5\uff0cbaseTime\u4e3a\u57fa\u51c6\u65f6\u95f4\u9ed8\u8ba4\u662f\u5f53\u524d\u65f6\u95f4\nprint(res)\nprint('====')\n\nres = tn.parse(target=u'\u4eca\u5e74\u6625\u5206')\nprint(res)\nprint('====')\n\nres = tn.parse(target=u'7000\u4e07')\nprint(res)\nprint('====')\n\nres = tn.parse(target=u'7\u767e')\nprint(res)\nprint('====')\n\nres = tn.parse(target=u'7\u5343')\nprint(res)\nprint('====')\n\n```\n\n\u7ed3\u679c\uff1a\n\n```sh\n\u76ee\u6807\u5b57\u7b26\u4e32:  \u661f\u671f\u5929\u665a\u4e0a\n\u57fa\u7840\u65f6\u95f4 2019-7-28-15-47-27\ntemp ['\u661f\u671f7\u665a\u4e0a']\n{\"type\": \"timestamp\", \"timestamp\": \"2019-07-28 20:00:00\"}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  \u665a\u4e0a8\u70b9\u5230\u4e0a\u534810\u70b9\u4e4b\u95f4\n\u57fa\u7840\u65f6\u95f4 2019-7-28-15-47-27\ntemp ['\u665a\u4e0a8\u70b9', '\u4e0a\u534810\u70b9']\n{\"type\": \"timespan\", \"timespan\": [\"2019-07-28 20:00:00\", \"2019-07-28 10:00:00\"]}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  2013\u5e74\u4e8c\u6708\u4e8c\u5341\u516b\u65e5\u4e0b\u5348\u56db\u70b9\u4e09\u5341\u5206\u4e8c\u5341\u4e5d\u79d2\n\u57fa\u7840\u65f6\u95f4 2013-2-28-16-30-29\ntemp ['2013\u5e742\u670828\u65e5\u4e0b\u53484\u70b930\u520629\u79d2']\n{\"type\": \"timestamp\", \"timestamp\": \"2013-02-28 16:30:29\"}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  \u6211\u9700\u8981\u5927\u698233\u59292\u5206\u949f\u56db\u79d2\n\u57fa\u7840\u65f6\u95f4 2013-2-28-16-30-29\ntemp ['33\u59292\u5206\u949f4\u79d2']\ntimedelta:  33 days, 0:02:04\n{\"type\": \"timedelta\", \"timedelta\": {\"year\": 0, \"month\": 1, \"day\": 3, \"hour\": 0, \"minute\": 2, \"second\": 4}}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  \u4eca\u5e74\u513f\u7ae5\u8282\u665a\u4e0a\u4e5d\u70b9\u4e00\u523b\n\u57fa\u7840\u65f6\u95f4 2019-7-28-15-47-27\ntemp ['\u4eca\u5e74\u513f\u7ae5\u8282\u665a\u4e0a9\u70b91\u523b']\n{\"type\": \"timestamp\", \"timestamp\": \"2019-06-01 21:15:00\"}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  \u4e09\u65e5\n\u57fa\u7840\u65f6\u95f4 2019-7-28-15-47-27\ntemp ['3\u65e5']\n{\"type\": \"timestamp\", \"timestamp\": \"2019-07-03 00:00:00\"}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  7\u70b94\n\u57fa\u7840\u65f6\u95f4 2019-7-28-15-47-27\ntemp ['7\u70b94']\n{\"type\": \"timestamp\", \"timestamp\": \"2019-07-28 07:04:00\"}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  \u4eca\u5e74\u6625\u5206\n\u57fa\u7840\u65f6\u95f4 2019-7-28-15-47-27\ntemp ['\u4eca\u5e74\u6625\u5206']\n{\"type\": \"timestamp\", \"timestamp\": \"2019-03-21 00:00:00\"}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  7000\u4e07\n\u57fa\u7840\u65f6\u95f4 2019-7-28-15-47-27\ntemp ['70000000']\n{\"type\": \"error\", \"error\": \"no time pattern could be extracted.\"}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  7\u767e\n\u57fa\u7840\u65f6\u95f4 2019-7-28-15-47-27\ntemp []\n{\"type\": \"error\", \"error\": \"no time pattern could be extracted.\"}\n====\n\u76ee\u6807\u5b57\u7b26\u4e32:  7\u5343\n\u57fa\u7840\u65f6\u95f4 2019-7-28-15-47-27\ntemp []\n{\"type\": \"error\", \"error\": \"no time pattern could be extracted.\"}\n====\n```\n\n## \u4f7f\u7528\u65b9\u5f0f \n\n\u89c1 `Test.py`\n\n## TODO\n\n| \u95ee\u9898                  | \u73b0\u5728\u7248\u672c                                       | \u6b63\u786e                                            |\n| --------------------- | ---------------------------------------------- | ----------------------------------------------- |\n| \u665a\u4e0a8\u70b9\u5230\u4e0a\u534810\u70b9\u4e4b\u95f4 | [\"2018-03-16 20:00:00\", \"2018-03-16 22:00:00\"] | [\"2018-03-16 20:00:00\", \"2018-03-17 10:00:00\"]\" |\n\n## \u58f0\u660e\n\n1. \u589e\u52a0\u4e86\"\u793c\u62dcxx\"\u7684\u8bc6\u522b\n2. \u4fee\u590d\u4e86\"2023/10/09\"\u8bc6\u522b\u9519\u8bef\u7684\u95ee\u9898\n3. \u4fee\u590d\u4e86\"2023-10-09\"\u88ab\u8bc6\u522b\u4e3a\u65f6\u95f4\u533a\u95f4\u7684\u95ee\u9898\n4. \u589e\u52a0\u4e86\"10/09\"\u7684\u8bc6\u522b\n5. \u4fee\u590d\u4e86\"10-09\"\u5728\u7cfb\u7edf\u65f6\u95f4\u662f\"2023-10-09\"\u7684\u65f6\u5019\uff0c\u88ab\u63a8\u6d4b\u4e3a\u672a\u6765\u65e5\u671f\"2024-10-09\"\u7684\u95ee\u9898\n6. \u589e\u52a0\u4e86\"1009\"\u7684\u8bc6\u522b\n7. \u4f18\u5316\u5bf9\"\u4e0b\u4e2a\u6708\u660e\u5929\"\u8fd9\u7c7b\u76f8\u5bf9\u65e5\u671f\u7684\u8bc6\u522b\n",
    "bugtrack_url": null,
    "license": "MIT Licence",
    "summary": "\u5c06\u4e2d\u6587\u65f6\u95f4\u8868\u8fbe\u8bcd\u8f6c\u4e3a\u76f8\u5e94\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u652f\u6301\u65f6\u95f4\u70b9\uff0c\u65f6\u95f4\u6bb5\uff0c\u65f6\u95f4\u95f4\u9694\u3002",
    "version": "1.0.5",
    "project_urls": {
        "Homepage": "https://github.com/xiaoyaosharenfan/ChineseDateTimeNLP"
    },
    "split_keywords": [
        "nlp",
        "time nlp",
        "date nlp"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a88218953cafbf06dc9be6ee6eade821ba86c7de953087ece17a4c3ca6c46565",
                "md5": "35b12dd095a9fe5b0e27b3a9068a74ae",
                "sha256": "155a113edf1ebf7a410ca8144008b955ca1aef834db8482caf5d7e5729f11cbd"
            },
            "downloads": -1,
            "filename": "ChineseDateTimeNLP-1.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "35b12dd095a9fe5b0e27b3a9068a74ae",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 27154,
            "upload_time": "2023-10-16T07:35:43",
            "upload_time_iso_8601": "2023-10-16T07:35:43.837966Z",
            "url": "https://files.pythonhosted.org/packages/a8/82/18953cafbf06dc9be6ee6eade821ba86c7de953087ece17a4c3ca6c46565/ChineseDateTimeNLP-1.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5c17145054aa35939f7b4aa65910f11330cf5903763f50531d1181ae76613c64",
                "md5": "a390938d4092a0f2c7c5947f2d0689c1",
                "sha256": "10e359aa168e69a805f99a0446746487ed6e8417b79f10cdf7f7e80fb1e461c0"
            },
            "downloads": -1,
            "filename": "ChineseDateTimeNLP-1.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "a390938d4092a0f2c7c5947f2d0689c1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 23788,
            "upload_time": "2023-10-16T07:35:45",
            "upload_time_iso_8601": "2023-10-16T07:35:45.987684Z",
            "url": "https://files.pythonhosted.org/packages/5c/17/145054aa35939f7b4aa65910f11330cf5903763f50531d1181ae76613c64/ChineseDateTimeNLP-1.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-16 07:35:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "xiaoyaosharenfan",
    "github_project": "ChineseDateTimeNLP",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "chinesedatetimenlp"
}
        
Elapsed time: 0.12970s