Skip to content

Commit

Permalink
form修复 明天的上午 (XiaoMi#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
du00cs authored Aug 12, 2024
1 parent 6216e46 commit 617c943
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ trait Rules extends DimRules {
)

private def intersectToken(options: Options, td1: TimeData, td2: TimeData): Option[Token] = {
println(td1, td2)
// 破除(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)出现
Expand Down Expand Up @@ -221,8 +222,7 @@ trait Rules extends DimRules {
name = "intersect: <x> 的 <y>",
// "一日"单独是latent,但是可以参与组合
pattern = List(isNotLatent.predicate, "".regex, or(or(isNotLatent, isLatent0oClockOfDay), isADayOfMonth).predicate),
prod = {
case (options, (t1@Token(Time, td1: TimeData)) :: _ :: (t2@Token(Time, td2: TimeData)) :: _)
prod = {case (options, (t1@Token(Time, td1: TimeData)) :: _ :: (t2@Token(Time, td2: TimeData)) :: _)
if td1.timeGrain > td2.timeGrain ||
// 上午的8-9点
td1.timeGrain == td2.timeGrain && td1.timeGrain == Hour && isAPartOfDay(t1) && !isAPartOfDay(t2) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,37 @@ trait Rules extends DimRules {
}
})

def intersectProd(td1: TimeData, td2: TimeData) : Option[Token] = {
// 破除(y-m)-d和y-(m-d)均构造出来的问题
if (td1.hint == YearMonth && td2.hint == 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 isAlreadySet = td2.timePred match {
case tdp: TimeDatePredicate =>
td1.timeGrain match {
case Year => tdp.year.nonEmpty
case Month => tdp.month.nonEmpty
case Day => tdp.dayOfMonth.nonEmpty
case _ => true
}
case _ => false
}
if (isAlreadySet) None
else {
val hint =
if (td1.timeGrain == Year && td2.hint == Hint.MonthOnly) YearMonth
else NoHint
for (td <- intersect(td1, td2)) yield {
Token(Date, td.copy(hint = hint))
}
}
}
}

val ruleIntersect =
Rule(
name = "dates: intersect",
Expand All @@ -347,34 +378,7 @@ trait Rules extends DimRules {
prod = tokens {
case Token(Date, td1: TimeData) :: Token(Date, td2: TimeData) :: _
if td1.timeGrain > td2.timeGrain =>
// 破除(y-m)-d和y-(m-d)均构造出来的问题
if (td1.hint == YearMonth && td2.hint == 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 isAlreadySet = td2.timePred match {
case tdp: TimeDatePredicate =>
td1.timeGrain match {
case Year => tdp.year.nonEmpty
case Month => tdp.month.nonEmpty
case Day => tdp.dayOfMonth.nonEmpty
case _ => true
}
case _ => false
}
if (isAlreadySet) None
else {
val hint =
if (td1.timeGrain == Year && td2.hint == Hint.MonthOnly) YearMonth
else NoHint
for (td <- intersect(td1, td2)) yield {
Token(Date, td.copy(hint = hint))
}
}
}
intersectProd(td1, td2)
}
)

Expand All @@ -389,15 +393,7 @@ trait Rules extends DimRules {
prod = tokens {
case Token(Date, td1: TimeData) :: _ :: Token(Date, td2: TimeData) :: _
if td1.timeGrain > td2.timeGrain =>
if (td1.timeGrain > Day && td2.timeGrain < Day) None
else {
val hint =
if (td1.timeGrain == Year && td2.hint == Hint.MonthOnly) YearMonth
else NoHint
for (td <- intersect(td1, td2)) yield {
Token(Date, td.copy(hint = hint))
}
}
intersectProd(td1, td2)
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ object Times {
}
)

val ruleDimTimePartOfDay1 = Rule(
name = "<dim time> 的 <part-of-day>",
pattern = List(isADayOfMonth.predicate, "".regex, and(isAPartOfDay, isNotLatent).predicate),
prod = tokens {
case Token(Time, td1: TimeData) :: _ :: Token(Time, td2: TimeData) :: _ =>
for (td <- intersect(td1, td2)) yield {
tt(td.copy(form = td2.form, hint = Hint.PartOfDayAtLast))
}
}
)

val rulePartOfDayDimTime = Rule(
name = "<part-of-day> <dim time>",
pattern = List(isAPartOfDay.predicate, isNotLatent.predicate),
Expand Down Expand Up @@ -192,6 +203,7 @@ object Times {
ruleTimeOfDayOClock,
ruleIntegerLatentTimeOfDay,
ruleDimTimePartOfDay,
ruleDimTimePartOfDay1,
ruleHhmmssCN_TimeOfDay,
// ruleRelativeMinutesAfterPastIntegerOClockOfDay,
ruleRelativeMinutesAfterPastIntegerHourOfDay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class FormTest extends UnitSpec {
describe("Form") {

val cases = Table[String, Option[Form]](("query", "form")
, ("周五的上午", PartOfDay("上午"))
, ("周五上午", PartOfDay("上午"))
, ("每个月五号的早上", PartOfDay("早上"))
, ("23号8点", TimeOfDay(8, true))
, ("23号上午8点", TimeOfDay(8, false))
Expand Down

0 comments on commit 617c943

Please sign in to comment.