How to extract time date period information from raw sentences in Python

点点圈 提交于 2019-12-06 06:38:58

This is a typical usecase for the excellent dateparser library. Just read the docs and you should be able to do it.

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

  1. Recognize the propositions and then recognize months and do the extraction.
  2. Recognize '-' and then recognize months and do the extraction.

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!