From 5b670da48d0b560bba5d376d7a5685403891c464 Mon Sep 17 00:00:00 2001 From: Ninglin Du Date: Tue, 11 Jun 2024 16:57:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E2=80=9C=E8=BF=87=E5=8E=BB?= =?UTF-8?q?=E4=B8=83=E5=A4=A9=E2=80=9D=E8=BF=99=E7=A7=8D=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=9C=A8alwaysInFuture=20=3D=20false=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=E4=B9=9F=E5=8F=8D=E8=BD=AC=E7=9A=84=E4=BA=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaomi/duckling/dimension/time/Time.scala | 3 +- .../dimension/time/TimeDataTest.scala | 54 ++++++++++--------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/Time.scala b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/Time.scala index b0a70fc2..e08974c9 100644 --- a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/Time.scala +++ b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/Time.scala @@ -125,7 +125,8 @@ case class TimeData(timePred: TimePredicate, val refTime = refTimeAdjust(new TimeObject(context.referenceTime, Grain.Second), options.timeOptions) - val reverseTake = hint == Hint.UncertainRecent && !options.timeOptions.recentInFuture || !options.timeOptions.alwaysInFuture + val reverseTake = (hint == Hint.UncertainRecent && !options.timeOptions.recentInFuture + || hint != Hint.Recent && !options.timeOptions.alwaysInFuture) val valueOpt = try { resolveTimeData(refTime, this, reverseTake) diff --git a/duckling-fork-chinese/core/src/test/scala/com/xiaomi/duckling/dimension/time/TimeDataTest.scala b/duckling-fork-chinese/core/src/test/scala/com/xiaomi/duckling/dimension/time/TimeDataTest.scala index 7e947fe4..ada9dc6e 100644 --- a/duckling-fork-chinese/core/src/test/scala/com/xiaomi/duckling/dimension/time/TimeDataTest.scala +++ b/duckling-fork-chinese/core/src/test/scala/com/xiaomi/duckling/dimension/time/TimeDataTest.scala @@ -44,38 +44,40 @@ class TimeDataTest extends UnitSpec with LazyLogging { } } - val testCases = List( - //2013, 2, 12, 4, 30, 0 - ("昨天", 2013, 2, 11), - ("今天", 2013, 2, 12), - ("明天", 2013, 2, 13), - ("一月二号", 2013, 1, 2), - ("二月十一号", 2013, 2, 11), - ("明年一月二号", 2014, 1, 2), - ("今年三月十号", 2013, 3, 10) + val testCases = Table( ("query", "year", "month", "day"), + ("最近三天", 2013, 2, 9) + , ("过去三天", 2013, 2, 9) + , ("昨天", 2013, 2, 11) + , ("今天", 2013, 2, 12) + , ("明天", 2013, 2, 13) + , ("一月二号", 2013, 1, 2) + , ("二月十一号", 2013, 2, 11) + , ("明年一月二号", 2014, 1, 2) + , ("今年三月十号", 2013, 3, 10) ) it("not InFuture eq") { val timeOptions = new TimeOptions() timeOptions.setAlwaysInFuture(false) + timeOptions.setRecentInFuture(false) val options = testOptions.copy(targets = Set(Time), timeOptions = timeOptions) - - testCases.foreach { - case (query, yyyy, mm, dd) => - val answers = analyze(query, testContext, options) - answers.headOption match { - case Some(answer) => - answer.token.value match { - case TimeValue(SimpleValue(InstantValue(dt, _)), _, _, _, _, _) => - (dt.year, dt.month, dt.dayOfMonth) shouldBe(yyyy, mm, dd) - case v => - logger.warn(s"unexpected value: $query => $v") - true shouldBe false - } - case _ => - logger.warn(s"empty result: $query") - true shouldBe false - } + forAll(testCases) { (query, yyyy, mm, dd) => + val answers = analyze(query, testContext, options) + answers.headOption match { + case Some(answer) => + answer.token.value match { + case TimeValue(SimpleValue(InstantValue(dt, _)), _, _, _, _, _) => + (dt.year, dt.month, dt.dayOfMonth) shouldBe(yyyy, mm, dd) + case TimeValue(IntervalValue(InstantValue(dt, _), _), _, _, _, _, _) => + (dt.year, dt.month, dt.dayOfMonth) shouldBe(yyyy, mm, dd) + case v => + logger.warn(s"unexpected value: $query => $v") + true shouldBe false + } + case _ => + logger.warn(s"empty result: $query") + true shouldBe false + } } }