如何在Python中从原始句子中提取时间日期段信息 [英] How to extract time date period information from raw sentences in Python

查看:155
本文介绍了如何在Python中从原始句子中提取时间日期段信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入:

  1. 从2016年3月27日至2016年12月31日有效的票务和旅行
  2. 有效期票必须在16年2月18日之前签发
  3. 有效期票必须在2016年2月29日之前签发
  4. 立即出行日期-2016年2月10日2016年2月22日-2016年5月12日
  5. 票务有效期为2016年1月31日之前

(注意:此输入已通过某些Python代码进行了预处理,因此使用某些Python包将更易于处理.)

(Note: The input has been preprocessed to this stage by some Python codes so that it will be easier to process using some Python packages.)

预期输出:

  1. 从2016-03-27到2016-12-31
  2. 在2016-02-18之前
  3. 在2016-02-29之前
  4. 现在-2016-02-10 2016-02-22-2016-05-12
  5. 在2016-01-31之前

我尝试过dateutil.但是它只能提取一个日期,对吗?即使在这种情况下,介词和日期的提取也是一个问题.

I have tried dateutil. However it can only extract one date, right? Even for this situation, extraction of both preposition and date is also a problem.

我还查看了dateparser和datefinder.看来他们俩都使用dateutil.

I also looked at dateparser and datefinder. It seems they both use dateutil.

日期可以是YYYY-MM-DD,DDMMYYYY等,只要格式相同即可.

Dates can be YYYY-MM-DD, DDMMYYYY, etc., as long as in the same format.

输出不必与上述输出相同,只要它能反映出准确的信息即可.

Output doesn't have to be identical to the above one, as long as it reflects accurate information.

最后,感谢您的宝贵时间和想法.我也会继续尝试.

Finally, thanks for your time and thoughts. I will also keep trying.

推荐答案

经过几天的研究,我提出了以下解决提取问题的方法.

After a few days of research, I come up with the following approaches which solve the extraction problem.

  1. 认识这些命题,然后认识数月并进行提取.
  2. 识别-",然后识别月份并进行提取.

部分代码如下所示. (摘录中需要上下文相关性)

Part the codes are shown below. (An excerpt which need dependencies in context)

new_w = new_s.split()
for j in range(len(new_w)):
    if new_w[j] in prepositions and (new_w[j+1].isdecimal() or new_w[j+1].lower() in months):
        # Process case like "Starting from Mar27, 2016 to Dec31, 2016"
        if j+7 in range(len(new_w)) and new_w[j+4] in prepositions:
            if new_w[j+5].isdecimal() or new_w[j+5].lower() in months:
                u = ' '.join(new_w[j:j+8])
                print(label_class[i] + ': ' + u)
                break
        # Process case like "Ticket must be issued on/before 29FEB, 2016"
        elif new_w[j-1] in prepositions:
            u = ' '.join(new_w[j-1:j+4])
            print(label_class[i] + ': ' + u)
            break
        # Process case like "Ticketing valid until 18FEB16"
        else:
            u = ' '.join(new_w[j:j+4])
            print(label_class[i] + ': ' + u)
            break
    # Process case like "TICKETING PERIOD:      NOW - FEB 02, 2016"
    # Process case like "TRAVELING DATES:      NOW - FEB 10,2016    FEB 22,2016 - MAY 12,2016"
    if new_w[j] in ['-'] and (new_w[j+1].lower() in months or new_w[j+2].lower() in months):
        if new_w[j-1].lower() == 'now':
            u = released_date + ' - ' + ' '.join(new_w[j+1:j+4])
            print(label_class[i] + ': ' + u)
        elif new_w[j-3].lower() in months or new_w[j-2].lower() in months:
            u = ' '.join(new_w[j-3:j+4])
            print(label_class[i] + ': ' + u)

这篇关于如何在Python中从原始句子中提取时间日期段信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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