Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DUCK]feat:支持time+ordinal+grain #229

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 last 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
Loading