2016-09-20 23 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 - 答えの2番目の候補は、オプションの 'this | next | last'を許可します。 –

+0

ありがとう、しかし私は正規表現で非常に貧しいです。あなたはそれを使用する方法を教えていただけますか? – user3449212

関連する問題