自然语言处理:日期识别

#日期识别
# 模块:所谓模块就是一个.py文件,用来存放变量,方法的文件
# 包(package): 包是更大的组织单位,用来组织区别管理多个模块文件
# import 模块 [as  别名模块]
# import 包.[N包].模块
# import 导入 最后一个必须是模块,而不能以包结尾
# from 包.[..包]   import 模块
# from 包.模块  import 方法
# from 模块 import 方法
import re
# 第一个datetime是包,第二个datetime是模块
from datetime import datetime,timedelta
from dateutil.parser import parse
import jieba.posseg as psg

UTIL_CN_NUM = {
    '零': 0, '一': 1, '二': 2, '两': 2, '三': 3, '四': 4,
    '五': 5, '六': 6, '七': 7, '八': 8, '九': 9,
    '0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
    '5': 5, '6': 6, '7': 7, '8': 8, '9': 9
}

UTIL_CN_UNIT = {'十': 10, '百': 100, '千': 1000, '万': 10000}

def cn2dig(src):
    if src == "":
        return None
    m = re.match("\d+", src)
    if m:
        return int(m.group(0))
    rsl = 0
    unit = 1
    for item in src[::-1]:
        if item in UTIL_CN_UNIT.keys():
            unit = UTIL_CN_UNIT[item]
        elif item in UTIL_CN_NUM.keys():
            num = UTIL_CN_NUM[item]
            rsl += num * unit
        else:
            return None
    if rsl < unit:
        rsl += unit
    return rsl

def year2dig(year):
    res = ''
    for item in year:
        if item in UTIL_CN_NUM.keys():
            res = res + str(UTIL_CN_NUM[item])
        else:
            res = res + item
    m = re.match("\d+", res)
    if m:
        if len(m.group(0)) == 2:
            return int(datetime.datetime.today().year/100)*100 + int(m.group(0))
        else:
            return int(m.group(0))
    else:
        return None

def parse_datetime(msg):
    if msg is None or len(msg) == 0:
        return None

    try:
        dt = parse(msg, fuzzy=True)
        return dt.strftime('%Y-%m-%d %H:%M:%S')
    except Exception as e:
        m = re.match(
            r"([0-9零一二两三四五六七八九十]+年)?([0-9一二两三四五六七八九十]+月)?([0-9一二两三四五六七八九十]+[号日])?([上中下午晚早]+)?([0-9零一二两三四五六七八九十百]+[点:\.时])?([0-9零一二三四五六七八九十百]+分?)?([0-9零一二三四五六七八九十百]+秒)?",
            msg)
        if m.group(0) is not None:
            res = {
                "year": m.group(1),
                "month": m.group(2),
                "day": m.group(3),
                "hour": m.group(5) if m.group(5) is not None else '00',
                "minute": m.group(6) if m.group(6) is not None else '00',
                "second": m.group(7) if m.group(7) is not None else '00',
            }
            params = {}

            for name in res:
                if res[name] is not None and len(res[name]) != 0:
                    tmp = None
                    if name == 'year':
                        tmp = year2dig(res[name][:-1])
                    else:
                        tmp = cn2dig(res[name][:-1])
                    if tmp is not None:
                        params[name] = int(tmp)
            target_date = datetime.today().replace(**params)
            is_pm = m.group(4)
            if is_pm is not None:
                if is_pm == u'下午' or is_pm == u'晚上' or is_pm =='中午':
                    hour = target_date.time().hour
                    if hour < 12:
                        target_date = target_date.replace(hour=hour + 12)
            return target_date.strftime('%Y-%m-%d %H:%M:%S')
        else:
            return None



def check_time_valid(word):
    m = re.match("\d+$", word)
    if m:
        if len(word) <= 6:
            return None
    word1 = re.sub('[号|日]\d+$', '日', word)
    if word1 != word:
        return check_time_valid(word1)
    else:
        return word1

#时间提取
def time_extract(text):

    time_res = []
    word = ''
    #字典键值对用冒号分割,每个键值对之间用逗号分割,整个字典包括在花括号{}中 
    #dict.get(key, default=None)返回指定键的值,如果值不在字典中返回default值
    keyDate = {'前天':-2,'前日':-2,'昨天':-1,'昨日':-1,'今天': 0,'今日': 0,'明天':1,'明日':1, '后天': 2}
    for k, v in psg.cut(text):
        if k in keyDate:
            if word != '':
                time_res.append(word)
            # datetime.timedelta对象代表两个时间之间的时间差
            word = (datetime.today() + timedelta(days=keyDate.get(k, 0))).strftime('%Y{y}%m{m}%d{d}').format(y='年', m='月', d='日')
        elif word != '':
            if v in ['m', 't']:
                word = word + k
            else:
                time_res.append(word)
                word = ''
        elif v in ['m', 't']:
            word = k
    if word != '':
        time_res.append(word)
    result = list(filter(lambda x: x is not None, [check_time_valid(w) for w in time_res]))
    final_res = [parse_datetime(w) for w in result]

    return [x for x in final_res if x is not None]

应用测试

print(datetime.today())

text_1 = '我昨日来的' 
print(text_1, time_extract(text_1), sep=':')

text0 = '我今天到的' 
print(text0, time_extract(text0), sep=':')

text1 = '我要住到明天下午三点' 
print(text1, time_extract(text1), sep=':')

text2 = '预定28号的房间' 
print(text2, time_extract(text2), sep=':')

text3 = '我要从26号下午4点住到11月2号' 
print(text3, time_extract(text3), sep=':')

text4 = '我要预订今天到30日的房间' 
print(text4, time_extract(text4), sep=':')

text5 = '前日到明日' 
print(text5, time_extract(text5), sep=':')

执行结果

2019-10-28 09:58:23.966139
我昨日来的:['2019-10-27 00:00:00']
我今天到的:['2019-10-28 00:00:00']
我要住到明天下午三点:['2019-10-29 00:00:00']
预定28号的房间:['2019-10-28 00:00:00']
我要从26号下午4点住到11月2号:['2019-10-26 16:00:00', '2019-11-02 00:00:00']
我要预订今天到30日的房间:['2019-10-28 00:00:00', '2019-10-30 00:00:00']
前日到明日:['2019-10-26 00:00:00', '2019-10-29 00:00:00']
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Time-NLP 中文语句中的时间语义识别 author:shinyke 本工具是由复旦NLP中的时间分析功能修改而来,做了很多细节和功能的优化,具体如下: 泛指时间的支持,如:早上、晚上、中午、傍晚等。 时间未来倾向。 如:在周五输入“周一早上开会”,则识别到下周一早上的时间;在下午17点输入:“9点送牛奶给隔壁的汉子”则识别到第二天上午9点。 多个时间的识别,及多个时间之间上下文关系处理。如:"下月1号下午3点至5点到图书馆还书",识别到开始时间为下月1号下午三点。同时,结束时间也继承上文时间,识别到下月1号下午5点。 可自定义基准时间:指定基准时间为“2016-05-20-09-00-00-00”,则一切分析以此时间为基准。 修复了各种各样的BUG。 简而言之,这是一个输入一句话,能识别出话里的时间的工具。╮(╯▽╰)╭ 示例代码: /**  *   * 测试类  *   * @author kexm  * @version 1.0  * @since 2016年5月4日  *   */ public class TimeAnalyseTest {     @Test     public void test(){         String path = TimeNormalizer.class.getResource("").getPath();         String classPath = path.substring(0, path.indexOf("/com/time"));         System.out.println(classPath "/TimeExp.m");         TimeNormalizer normalizer = new TimeNormalizer(classPath "/TimeExp.m");         normalizer.parse("Hi,all.下周一下午三点开会");// 抽取时间         TimeUnit[] unit = normalizer.getTimeUnit();         System.out.println("Hi,all.下周一下午三点开会");         System.out.println(DateUtil.formatDateDefault(unit[0].getTime())   "-"   unit[0].getIsAllDayTime());          normalizer.parse("早上六点起床");// 注意此处识别到6天在今天已经过去,自动识别为明早六点(未来倾向,可通过开关关闭:new TimeNormalizer(classPath "/TimeExp.m", false))         unit = normalizer.getTimeUnit();         System.out.println("早上六点起床");         System.out.println(DateUtil.formatDateDefault(unit[0].getTime())   "-"   unit[0].getIsAllDayTime());         normalizer.parse("周一开会");// 如果本周已经是周二,识别为下周周一。同理处理各级时间。(未来倾向)         unit = normalizer.getTimeUnit();         System.out.println("周一开会");         System.out.println(DateUtil.formatDateDefault(unit[0].getTime())   "-"   unit[0].getIsAllDayTime());         normalizer.parse("下下周一开会");//对于上/下的识别         unit = normalizer.getTimeUnit();         System.out.println("下下周一开会");         System.out.println(DateUtil.formatDateDefault(unit[0].getTime())   "-"   unit[0].getIsAllDayTime());  

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值