Skip to content

Commit

Permalink
支持x分
Browse files Browse the repository at this point in the history
- 05分 latent = false
- 50分 latent = true
  • Loading branch information
du00cs committed Nov 13, 2024
1 parent 65dceca commit 2056de2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -199,6 +209,7 @@ object Times {

val rules = List(
ruleNow,
ruleMM,
ruleHhmmssTimeOfDay,
ruleTimeOfDayOClock,
ruleIntegerLatentTimeOfDay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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天")),
Expand Down

0 comments on commit 2056de2

Please sign in to comment.