Search code examples
pythonstringdatedatetimepython-datetime

extract 'date-related' string from a sentence-python


I am new to python, and want to find all 'date-related' words in a sentence, such as date, Monday, Tuesday, last week, next week, tomorrow, yesterday, today, etc.

For example:
input: 'Yesterday I went shopping'
return: 'Yesterday'
input: 'I will start working on Tuesday'
return: 'Tuesday'
input: 'My birthday is 1998-12-12'
return: '1998-12-12'

I find that python package 'datefinder' can find these words, but it will automatically change these words to standard datetime. However, I only want to extract these words, is there any other method or package that can do this?

Thanks for your help!


Solution

  • This is how I would do the logic for it, as far as getting the numbers from a string that contains digits as well I'm not sure, I would create and input that would specifically ask for digits then as I did firstSentence.lower() I would then do firstSentence = int(firstSentence) to ensure only ints passed

    firstSentence = raw_input('Tell me something: ')
    firstSentence = firstSentence.lower()
    if 'yesterday' in firstSentence:
        #now pass a function that returns date/time
        pass
    elif 'tuesday' in firstSentence:
        #now pass a function that returns date/time
        pass
    else:
        print 'No day found'