user3449212 user3449212 - 2 months ago 14
Python Question

REgex to extract date and time

I am using nltk regex for date and time extraction:

text = 'LEts have quick meeting on Wednesday at 9am'
week_day = "(monday|tuesday|wednesday|thursday|friday|saturday|sunday)"
month = "(january|february|march|april|may|june|july|august|september| \
october|november|december)"
dmy = "(year|day|week|month)"
exp2 = "(this|next|last)"
regxp2 = "(" + exp2 + " (" + dmy + "|" + week_day + "|" + month + "))"
reg2 = re.compile(regxp2, re.IGNORECASE)
found = reg2.findall(text)
found = [a[0] for a in found if len(a) > 1]
for timex in found:
timex_found.append(timex)

print timex_found


Everything looks right to me, but it does not tag
Wednesday
any clue? What change should I make to consider "wednesday" as well "this wednesday"

Will

regxp2 = "((this|next|last)? (" + dmy + "| " + week_day + "| " + month+ "))"


consider my case?

Answer

The regex is looking for ((this|next|last) (dmy|weekday|month)).

Your input doesn't have a match.

Some alternatives that may work:

((this|next|last|on) (dmy|weekday|month))

((this|next|last)? (dmy|weekday|month))