From cf9790be525d92485bab4877a15c4b7157217a28 Mon Sep 17 00:00:00 2001 From: zhangsonglei Date: Sun, 28 Jul 2024 22:21:14 +0800 Subject: [PATCH] =?UTF-8?q?[DUCK]feat:=E6=94=AF=E6=8C=81time+ordinal+grain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../duckling/dimension/ordinal/Rules.scala | 18 +++++++ .../duckling/dimension/time/Rules.scala | 49 +++++++++++++++++++ .../duckling/dimension/ordinal/Examples.scala | 7 ++- .../duckling/dimension/time/Examples.scala | 5 +- 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/ordinal/Rules.scala b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/ordinal/Rules.scala index 2e679815..bba107b3 100644 --- a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/ordinal/Rules.scala +++ b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/ordinal/Rules.scala @@ -19,6 +19,7 @@ package com.xiaomi.duckling.dimension.ordinal import com.xiaomi.duckling.Types._ import com.xiaomi.duckling.dimension.DimRules import com.xiaomi.duckling.dimension.implicits._ +import com.xiaomi.duckling.dimension.matcher.{GroupMatch, RegexMatch} import com.xiaomi.duckling.dimension.numeral.Predicates.isNumeralDimension import com.xiaomi.duckling.dimension.numeral.{Numeral, NumeralData} @@ -37,4 +38,21 @@ trait Rules extends DimRules { case Token(Ordinal, OrdinalData(value, _)) :: _ => ordinal(value, ge = true) }) + + val ruleReverseOrdinalDigits = Rule( + name = "reverse ordinal (digits)", + pattern = List("倒数第".regex, isNumeralDimension.predicate), + prod = tokens { + case _ :: Token(Numeral, NumeralData(v, _, _, _, _, _)) :: _ => ordinal(-math.floor(v).toLong) + } + ) + + val ruleLastOrdinalDigits = Rule( + name = "reverse last ordinal (digits)", + pattern = List("最后(1|一)个?".regex), + prod = tokens { + case Token(RegexMatch, GroupMatch(s :: _)) :: _ => + ordinal(-1, ge = s.endsWith("个")) + } + ) } diff --git a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/Rules.scala b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/Rules.scala index 92d3e250..a1b62edc 100644 --- a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/Rules.scala +++ b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/Rules.scala @@ -122,6 +122,53 @@ trait Rules extends DimRules { } } ) + + /** + * 今年/本月/2024年/5月 + 最后一天/(倒数)?第二天 + */ + val ruleNthTimeOfOrdinalGrain = Rule( + name = "nth