是否有用于从自然语言解析日期和时间的 Python 库? [英] Is there any python library for parsing dates and times from a natural language?

查看:20
本文介绍了是否有用于从自然语言解析日期和时间的 Python 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找的是可以将明天早上 6 点"或下周一中午"转换为适当的日期时间对象的内容.

What I'm looking for is something that can translate 'tomorrow at 6am' or 'next moday at noon' to the appropriate datetime objects.

推荐答案

parsedatetime - Python能够解析人类可读"的日期/时间表达式的模块.

#!/usr/bin/env python
from datetime import datetime
import parsedatetime as pdt # $ pip install parsedatetime

cal = pdt.Calendar()
now = datetime.now()
print("now: %s" % now)
for time_string in ["tomorrow at 6am", "next moday at noon", 
                    "2 min ago", "3 weeks ago", "1 month ago"]:
   print("%s:	%s" % (time_string, cal.parseDT(time_string, now)[0]))

输出

now: 2015-10-18 13:55:29.732131
tomorrow at 6am:    2015-10-19 06:00:00
next moday at noon: 2015-10-18 12:00:00
2 min ago:  2015-10-18 13:53:29
3 weeks ago:    2015-09-27 13:55:29
1 month ago:    2015-09-18 13:55:29

这篇关于是否有用于从自然语言解析日期和时间的 Python 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆