Skip to content

Commit

Permalink
[DUCK]feat:支持time+ordinal+grain
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsonglei committed Jul 28, 2024
1 parent 88d666f commit df683fb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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 ordinal (digits)",
pattern = List("最后(1|一)个?".regex),
prod = tokens {
case Token(RegexMatch, GroupMatch(s :: _)) :: _ =>
ordinal(-1, ge = s.endsWith(""))
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,53 @@ trait Rules extends DimRules {
}
}
)

/**
* 今年/本月/2024年/5月 + 最后一天/(倒数)?第二天
*/
val ruleNthTimeOfOrdinalGrain = Rule(
name = "nth <time> of <ordinal> grain",
pattern =
List(isDimension(Time).predicate, isDimension(Ordinal).predicate, isDimension(TimeGrain).predicate),
prod = tokens {
case Token(Time, td: TimeData) ::
Token(Ordinal, od: OrdinalData)
:: Token(TimeGrain, GrainData(g, _)) :: _ if td.timeGrain > g && g == Grain.Day && !od.ge =>

val predicate = if (od.value >= 0) {
val ov = if(od.value > 0) od.value.toInt -1 else 0
SequencePredicate(List(td, cycleNth(g, ov)))
} else {
SequencePredicate(List(td, cycleNth(td.timeGrain, 1), cycleNth(g, od.value.toInt)))
}

tt(TimeData(timePred = predicate, timeGrain = td.timeGrain))
}
)

val ruleNthTimeOfOrdinalGrain2 = Rule(
name = "nth <time> of <ordinal> grain2",
pattern = List(
isDimension(Time).predicate,
"".regex,
isDimension(Ordinal).predicate,
isDimension(TimeGrain).predicate
),
prod = tokens {
case Token(Time, td: TimeData) :: _ ::
Token(Ordinal, od: OrdinalData)
:: Token(TimeGrain, GrainData(g, _)) :: _ if td.timeGrain > g && g == Grain.Day && !od.ge =>

val predicate = if (od.value >= 0) {
val ov = if(od.value > 0) od.value.toInt -1 else 0
SequencePredicate(List(td, cycleNth(g, ov)))
} else {
SequencePredicate(List(td, cycleNth(td.timeGrain, 1), cycleNth(g, od.value.toInt)))
}

tt(TimeData(timePred = predicate, timeGrain = td.timeGrain))
}
)

val ruleIntersect =
Rule(
Expand Down Expand Up @@ -584,6 +631,8 @@ trait Rules extends DimRules {
ruleRecentTime,
ruleNthTimeOfTime,
ruleNthTimeOfTime2,
ruleNthTimeOfOrdinalGrain,
ruleNthTimeOfOrdinalGrain2,
ruleIntersect,
ruleIntersect2,
ruleRecentCycle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import com.xiaomi.duckling.dimension.{Dimension, DimExamples}
object Examples extends DimExamples {

override def pairs: List[(Types.ResolvedValue, List[String])] =
List((7, List("第七", "第七个")), (11, List("第十一")), (91, List("第九十一"))).map {
List(
(7, List("第七", "第七个")),
(11, List("第十一")),
(91, List("第九十一")),
(-2, List("倒数第二")),
(-1, List("倒数第一", "最后一个"))).map {
case (expected, texts) => (OrdinalData(expected), texts)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,10 @@ object Examples extends DimExamples {
(h(15), List("今天3点")),
// 8点,不是20点
(datetime(LocalDateTime.of(2013, 2, 11, 8, 0, 0), Hour), List("2013年2月11号8点")),
(datetime(LocalDateTime.of(2013, 2, 14, 8, 0, 0), Hour), List("2月14号8点"))
(datetime(LocalDateTime.of(2013, 2, 14, 8, 0, 0), Hour), List("2月14号8点")),
(datetime(LocalDateTime.of(2013, 2, 2, 0, 0, 0), Day), List("2013年2月2", "2013年2月第二天", "今年2月的第二天", "本月第二天")),
(datetime(LocalDateTime.of(2013, 1, 2, 0, 0, 0), Day), List("2013年的第二天", "今年的第二天", "今年倒数第364天")),
(datetime(LocalDateTime.of(2013, 5, 31, 0, 0, 0), Day), List("5月的最后一天", "5月最后一天", "5月第31天"))
)

override def pairs: List[(ResolvedValue, List[String])] =
Expand Down

0 comments on commit df683fb

Please sign in to comment.