2016-09-20 36 views
-1

我使用正則表達式NLTK日期和時間提取:正則表達式來提取日期和時間

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 

一切看起來我的權利,但它不標記Wednesday任何線索?我應該改變什麼考慮「星期三」,以及「星期三」

威爾

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

考慮我的情況?

+1

線通過它的線,並計算出如果線產生預期的結果。當你這樣做的時候,如果你不知道如何解決這個問題,你會得到一個實際上最小的[mcve]。 – khelwood

+0

輸入中沒有'(this | next | last)'。 –

回答

3

正則表達式正在尋找((this|next|last) (dmy|weekday|month))

您的輸入不匹配。

一些替代品可能工作:

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

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

謝謝,但我們可以標記'今日'和'日'兩者,如果有任何存在 – user3449212

+0

@ user3449212 - 答案中的第二個建議允許可選的'this | next | last'。 –

+0

謝謝,但我用正則表達式很差。你可以告訴我如何使用它 – user3449212