From a6f9f794e813773c9eda6c1b6e4aa474f388ec60 Mon Sep 17 00:00:00 2001 From: Ninglin Du Date: Wed, 13 Nov 2024 17:02:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81x=E5=88=86=20(#249)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 05分 latent = false - 50分 latent = true --- .../duckling/dimension/numeral/Predicates.scala | 8 ++++---- .../duckling/dimension/time/rule/Times.scala | 17 ++++++++++++++--- .../duckling/dimension/time/Examples.scala | 2 ++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/numeral/Predicates.scala b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/numeral/Predicates.scala index 7abd0aad..9e17178f 100644 --- a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/numeral/Predicates.scala +++ b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/numeral/Predicates.scala @@ -46,7 +46,7 @@ object Predicates { } val isIntegerBetween: (Int, Int) => Predicate = (low: Int, high: Int) => { - case Token(Numeral, NumeralData(v, _, _, _, _, _)) => isIntegerBetween(v, low, high) + case Token(Numeral, NumeralData(v, _, _, _, _, precision)) => precision == 0 && isIntegerBetween(v, low, high) } def isIntegerBetween(x: Double, low: Int, high: Int): Boolean = { @@ -58,13 +58,13 @@ object Predicates { } val isNatural: Predicate = { - case Token(Numeral, NumeralData(v, _, _, _, _, _)) => isInteger(v) && v > 0 - case Token(UnitNumber, NumeralData(v, _, _, _, _, _)) => isInteger(v) && v > 0 + case Token(Numeral, NumeralData(v, _, _, _, _, precision)) => precision == 0 && isInteger(v) && v > 0 + case Token(UnitNumber, NumeralData(v, _, _, _, _, precision)) => precision == 0 && isInteger(v) && v > 0 case _ => false } val isInteger: Predicate = { - case Token(_, NumeralData(v, _, _, _, _, _)) => isInteger(v) + case Token(_, NumeralData(v, _, _, _, _, precision)) => precision == 0 && isInteger(v) } def isInteger(d: Double): Boolean = getIntValue(d).nonEmpty diff --git a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/rule/Times.scala b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/rule/Times.scala index fb3ffd4e..71eec987 100644 --- a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/rule/Times.scala +++ b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/rule/Times.scala @@ -16,17 +16,16 @@ package com.xiaomi.duckling.dimension.time.rule -import org.apache.commons.lang3.StringUtils - import scalaz.Scalaz._ import com.xiaomi.duckling.Types._ import com.xiaomi.duckling.dimension.implicits._ import com.xiaomi.duckling.dimension.matcher.Prods.regexMatch import com.xiaomi.duckling.dimension.matcher.{GroupMatch, RegexMatch} -import com.xiaomi.duckling.dimension.numeral.NumeralData +import com.xiaomi.duckling.dimension.numeral.{Numeral, NumeralData} import com.xiaomi.duckling.dimension.numeral.Predicates._ import com.xiaomi.duckling.dimension.numeral.Prods.integerMap +import com.xiaomi.duckling.dimension.numeral.seq.{isDigitLeading0, isDigitOfWidth, DigitSequence, DigitSequenceData} import com.xiaomi.duckling.dimension.time.Prods._ import com.xiaomi.duckling.dimension.time.enums.Hint import com.xiaomi.duckling.dimension.time.form.TimeOfDay @@ -55,6 +54,17 @@ object Times { } ) + val ruleMM = Rule( + name = "mm 分", + pattern = List(or(and(isDimension(DigitSequence), isDigitLeading0, isDigitOfWidth(2)), isIntegerBetween(0, 59)).predicate, "分".regex), + prod = tokens { + case Token(_, DigitSequenceData(seq, _, _)) :: _ => + tt(minute(Integer.parseInt(seq))) + case Token(_, nd: NumeralData) :: _ => + tt(minute(nd.value.toInt).copy(latent = true)) + } + ) + val ruleHhmmssCN_TimeOfDay = Rule( name = "hh时mm分ss秒 (time-of-day)", pattern = List(raw"((?:[01]?\d)|(?:2[0-3]))[点]([0-5]?\d)分(([0-5]?\d)秒)?".regex), @@ -199,6 +209,7 @@ object Times { val rules = List( ruleNow, + ruleMM, ruleHhmmssTimeOfDay, ruleTimeOfDayOClock, ruleIntegerLatentTimeOfDay, diff --git a/duckling-fork-chinese/learning/src/main/scala/com/xiaomi/duckling/dimension/time/Examples.scala b/duckling-fork-chinese/learning/src/main/scala/com/xiaomi/duckling/dimension/time/Examples.scala index cb1fafde..82d7b750 100644 --- a/duckling-fork-chinese/learning/src/main/scala/com/xiaomi/duckling/dimension/time/Examples.scala +++ b/duckling-fork-chinese/learning/src/main/scala/com/xiaomi/duckling/dimension/time/Examples.scala @@ -497,6 +497,8 @@ object Examples extends DimExamples { (datetime(LocalDateTime.of(2013, 2, 22, 8, 0, 0), Hour), List("下周五8点")), (datetime(LocalDateTime.of(2013, 11, 20, 20, 0, 0), Hour), List("11.20 20点")), (datetime(LocalDateTime.of(2013, 12, 24, 0, 0, 0), Day), List("圣诞节的前一天", "圣诞节前一天")), + (datetime(LocalDateTime.of(2013, 2, 12, 5, 5, 0), Minute), List("05分")), + (datetime(LocalDateTime.of(2013, 2, 12, 4, 50, 0), Minute), List("50分")), (datetime(LocalDateTime.of(2013, 2, 12, 11, 3, 0), Minute), List("11点03分", "11点03", "十一点零三分", "十一点零三")), (datetime(LocalDateTime.of(2013, 9, 15, 11, 0, 0), Hour), List("9月1号的14天11小时后", "9月1号的两个星期11小时之后", "9月16号的13个小时以前")), (datetime(LocalDateTime.of(2013, 1, 13, 0, 0, 0), Day), List("2月13号的31天前", "2月13号的一个月以前", "明天的一个月以前", "2月13号31天前", "2月13号前的第31天")),