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] 修复repeat/time 两个问题 #236

Merged
merged 1 commit into from
Aug 7, 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 @@ -170,6 +170,30 @@ trait Rules extends DimRules {
}
)

private def intersectToken(options: Options, td1: TimeData, td2: TimeData): Option[Token] = {
// 破除(y-m)-d和y-(m-d)均构造出来的问题
if (td1.hint == YearMonth && td2.hint == DayOnly) None
// 固定顺序,避免(y-m)-(d H-M-S) 以及(y)-(m-d H-M-S)出现
else if (td1.timeGrain > Day && td2.timeGrain < Day) None
// 避免多路解析 [2017年三月2号早上][10点半] 和 [2017年三月2号][早上10点半]
// else if (td1.timeGrain == Day && td2.timeGrain == Hour) None
else {
val hint =
if (td1.timePred.isInstanceOf[SequencePredicate]) Sequence
else if (td1.timeGrain == Year && td2.hint == MonthOnly) YearMonth
else if (td2.hint == Hint.PartOfDay || td2.form.nonEmpty) Hint.PartOfDay
else Intersect
// 10号八点,需要去掉AMPM (今天是10号9点时,不应再出20点)
// 今天8点,需要根据当前时间是出8/20点
val _td2 = if (td1.hint == Hint.RecentNominal) td2 else removeAMPM(td2)
val _td1 = FuzzyDayIntervals.enlarge(td1, options.timeOptions.beforeEndOfInterval)
hint match {
case Sequence => sequenceProd(_td1, _td2)
case _ => tt(intersect(_td1, _td2).map(_.copy(hint = hint, form = td2.form)))
}
}
}

val ruleIntersect =
Rule(
name = "intersect",
Expand All @@ -183,26 +207,7 @@ trait Rules extends DimRules {
if td1.timeGrain > td2.timeGrain && !(td1.hint == Hint.Date && td2.hint == Hint.Date) ||
// 上午的8-9点
td1.timeGrain == td2.timeGrain && td1.timeGrain == Hour && isAPartOfDay(t1) && !isAPartOfDay(t2) =>
// 破除(y-m)-d和y-(m-d)均构造出来的问题
if (td1.hint == YearMonth && td2.hint == DayOnly) None
// 固定顺序,避免(y-m)-(d H-M-S) 以及(y)-(m-d H-M-S)出现
else if (td1.timeGrain > Day && td2.timeGrain < Day) None
// 避免多路解析 [2017年三月2号早上][10点半] 和 [2017年三月2号][早上10点半]
// else if (td1.timeGrain == Day && td2.timeGrain == Hour) None
else {
val hint =
if (td1.timePred.isInstanceOf[SequencePredicate]) Sequence
else if (td1.timeGrain == Year && td2.hint == MonthOnly) YearMonth
else Intersect
// 10号八点,需要去掉AMPM (今天是10号9点时,不应再出20点)
// 今天8点,需要根据当前时间是出8/20点
val _td2 = if (td1.hint == Hint.RecentNominal) td2 else removeAMPM(td2)
val _td1 = FuzzyDayIntervals.enlarge(td1, options.timeOptions.beforeEndOfInterval)
hint match {
case Sequence => sequenceProd(_td1, _td2)
case _ => tt(intersect(_td1, _td2).map(_.copy(hint = hint)))
}
}
intersectToken(options, td1, td2)
}
)

Expand All @@ -215,15 +220,7 @@ trait Rules extends DimRules {
if td1.timeGrain > td2.timeGrain ||
// 上午的8-9点
td1.timeGrain == td2.timeGrain && td1.timeGrain == Hour && isAPartOfDay(t1) && !isAPartOfDay(t2) =>
if (td1.timeGrain > Day && td2.timeGrain < Day) None
else {
val hint =
if (td1.timeGrain == Year && td2.hint == MonthOnly) YearMonth
else NoHint
val _td1 = FuzzyDayIntervals.enlarge(td1, options.timeOptions.beforeEndOfInterval)
val td = intersect(_td1, removeAMPM(td2)).map(_.copy(hint = hint))
tt(td)
}
intersectToken(options, td1, td2)
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ trait Rules extends DimRules with LazyLogging {
val ruleIntervalTime = Rule(
name = "<interval> <time/interval>",
pattern = List(isInterval.predicate, isDimension(Time).predicate),
prod = tokens { case Token(Time, outer: TimeData) :: Token(Time, inner: TimeData):: _ if outer.timeGrain > inner.timeGrain =>
prod = tokens { case Token(Time, outer: TimeData) :: Token(Time, inner: TimeData):: _
if outer.timeGrain > inner.timeGrain && (inner.timePred.maxGrain.isEmpty || outer.timeGrain > inner.timePred.maxGrain.get) =>
val oInterval = outer.timePred.asInstanceOf[TimeIntervalsPredicate]
// start
val start = intersect(inner, TimeData(oInterval.p1, timeGrain=outer.timeGrain))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ object Examples extends DimExamples {
),
List("周一早上", "周一早晨", "周一清晨", "礼拜一早上", "礼拜一早晨", "下周一早上")
),
(
localDateTimeInterval(
LocalDateTime.of(2013, 2, 13, 4, 0, 0),
LocalDateTime.of(2013, 2, 13, 12, 0, 0),
Hour,
partOfDay = "早上"
),
List("明天早上", "明天的早上")
),
(ymd(2013, 10, 7), List("十月第一个星期一", "十月的第一个星期一")),
(
localDateTimeInterval(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.xiaomi.duckling.dimension.time.{form, TimeValue}
import com.xiaomi.duckling.dimension.time.duration.DurationData
import com.xiaomi.duckling.dimension.time.enums.Grain._
import com.xiaomi.duckling.dimension.time.Types.DuckDateTime
import com.xiaomi.duckling.dimension.time.form.Form
import com.xiaomi.duckling.dimension.time.form.{Form, PartOfDay, TimeOfDay}
import com.xiaomi.duckling.dimension.time.helper.TimeValueHelpers._
import com.xiaomi.duckling.dimension.time.repeat.WorkdayType.{NonWorkday, Workday}

Expand All @@ -47,13 +47,13 @@ object Examples extends DimExamples {
(RepeatValue(DurationData(15, Minute, schema = "PT15M")), List("每隔15分钟", "隔15分钟")),
(RepeatValue(
DurationData(1, Month),
start = datetimeInterval(
start = Some(datetimeInterval(
new DuckDateTime(LocalDateTime.of(2013, 3, 5, 4, 0, 0)),
new DuckDateTime(LocalDateTime.of(2013, 3, 5, 12, 0, 0)),
Hour)
Hour, partOfDay = "早上"), PartOfDay("早上"))
), List("每个月五号的早上")),
(RepeatValue(DurationData(1, Month), start = ymd(m = 3, d = 5)), List("每个月的五号")),
(RepeatValue(DurationData(1, Month), start = ymdhms(M = 3, d = 2, h = 14, grain = Hour)), List("每月2号下午2点")),
(RepeatValue(DurationData(1, Month), start = Some(ymdhms(M = 3, d = 2, h = 14, grain = Hour), TimeOfDay(14, false))), List("每月2号下午2点")),
(RepeatValue(DurationData(1, Week), start = (ymd(d = 13), Some(form.DayOfWeek))), List("每周三", "每个星期三")),
(RepeatValue(DurationData(1, Day), start = (h(8), Some(form.TimeOfDay(Some(8), false)))), List("每天上午八点", "每个上午八点")),
(RepeatValue(workdayType = NonWorkday), List("非工作日", "节假日")),
Expand Down
Loading