From ef11b77fdfb18959787c78659e3ad9c6df25bb52 Mon Sep 17 00:00:00 2001 From: Ninglin Du Date: Wed, 31 Jul 2024 16:32:53 +0800 Subject: [PATCH] =?UTF-8?q?[WIP]=20repeat=20=E5=A2=9E=E5=8A=A0=20=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=8C=BA=E9=97=B4+=E6=97=B6=E9=97=B4=E7=82=B9/?= =?UTF-8?q?=E5=8C=BA=E9=97=B4=20=E7=9A=84=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../duckling/dimension/time/date/Rules.scala | 38 ++++++++++++++++++- .../dimension/time/repeat/Rules.scala | 15 +++++++- .../duckling/task/NaiveBayesConsole.scala | 13 ++++--- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/date/Rules.scala b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/date/Rules.scala index b29ed1e..17638ed 100644 --- a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/date/Rules.scala +++ b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/date/Rules.scala @@ -18,7 +18,9 @@ package com.xiaomi.duckling.dimension.time.date import scalaz.std.string.parseInt import java.time.LocalDate +import java.util.regex.Pattern +import scala.collection.mutable import scala.util.Try import com.xiaomi.duckling.Types._ @@ -33,7 +35,7 @@ import com.xiaomi.duckling.dimension.time.duration.{isADecade, Duration, Duratio import com.xiaomi.duckling.dimension.time.enums.Grain._ import com.xiaomi.duckling.dimension.time.enums.Hint.{NoHint, RecentNominal, YearMonth} import com.xiaomi.duckling.dimension.time.enums.IntervalType.{Closed, Open} -import com.xiaomi.duckling.dimension.time.enums.{Grain, Hint} +import com.xiaomi.duckling.dimension.time.enums.{Grain, Hint, IntervalType} import com.xiaomi.duckling.dimension.time.helper.TimeDataHelpers._ import com.xiaomi.duckling.dimension.time.predicates.{EndOfGrainPredicate, SequencePredicate, TimeDatePredicate, _} import com.xiaomi.duckling.dimension.time.{GrainWrapper, TimeData} @@ -398,4 +400,38 @@ trait Rules extends DimRules { } } ) + + private val WeekX = Pattern.compile("(周|星期)([一二三四五六日天])") + private val WeekDayStr = "12345671234567" + + val ruleWeekXyz = Rule( + name = "周一周二", + pattern = List("((周|星期)[一二三四五六日天]){2,}".regex), + prod = regexMatch {case text :: _ => + val m = WeekX.matcher(text) + var pos = 0 + val days = mutable.MutableList[Int]() + while (m.find(pos)) { + val d = m.group(2) match { + case "一" => 1 + case "二" => 2 + case "三" => 3 + case "四" => 4 + case "五" => 5 + case "六" => 6 + case "日" | "天" => 7 + } + days += d + pos = m.end() + } + WeekDayStr.indexOf(days.mkString("")) match { + case -1 => None + case _ => + val td = TimeData( + TimeIntervalsPredicate(IntervalType.Closed, TimeDatePredicate(dayOfWeek = days.head), TimeDatePredicate(dayOfWeek = days.last), beforeEndOfInterval = true), + timeGrain = Day) + Token(Date, td) + } + } + ) } diff --git a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/repeat/Rules.scala b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/repeat/Rules.scala index d6311ad..7fce4cf 100644 --- a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/repeat/Rules.scala +++ b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/repeat/Rules.scala @@ -157,14 +157,25 @@ trait Rules extends DimRules with LazyLogging { } ) - // 周一到周五的早上八点 + // 周一到周五早上八点 val ruleIntervalTime = Rule( name = "