Skip to content

Commit

Permalink
[WIP] repeat 增加 重复区间+时间点/区间 的能力
Browse files Browse the repository at this point in the history
  • Loading branch information
du00cs committed Jul 30, 2024
1 parent 689711b commit 195850e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package com.xiaomi.duckling.dimension.time

import java.time.temporal.ChronoUnit

import com.xiaomi.duckling.Types._
import com.xiaomi.duckling.dimension.implicits._
import com.xiaomi.duckling.dimension.time.enums.Grain
import com.xiaomi.duckling.dimension.time.enums.Grain._
import com.xiaomi.duckling.dimension.time.form.{TimeOfDay, Month => _}
import com.xiaomi.duckling.dimension.time.helper.TimeDataHelpers.hour
import com.xiaomi.duckling.dimension.time.predicates.{TimeDatePredicate, TimePredicate}
import com.xiaomi.duckling.dimension.time.Types.InstantValue

object Helpers {

Expand Down Expand Up @@ -66,4 +69,20 @@ object Helpers {
} else td2
}

def countGrains(start: InstantValue, end: InstantValue): Int = {
val a = start.datetime.toLocalDatetime
val b = end.datetime.toLocalDatetime
val n = start.grain match {
case NoGrain | Second => ChronoUnit.SECONDS.between(a, b)
case Minute => ChronoUnit.MINUTES.between(a, b)
case Hour => ChronoUnit.HOURS.between(a, b)
case Day => ChronoUnit.DAYS.between(a, b)
case Week => ChronoUnit.WEEKS.between(a, b)
case Month => ChronoUnit.MONTHS.between(a, b)
case Quarter => ChronoUnit.MONTHS.between(a, b) / 3
case Year => ChronoUnit.YEARS.between(a, b)
}
n.intValue()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ object predicates {
case _ => false
}

val isInterval: Predicate = {
case Token(Time, td: TimeData) => td.timePred.isInstanceOf[TimeIntervalsPredicate]
case _ => false
}

val isIntervalOfDay: Predicate = {
case Token(Time, td: TimeData) =>
td.form match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.xiaomi.duckling.dimension.time.grain.TimeGrain
import com.xiaomi.duckling.dimension.time.Types._
import com.xiaomi.duckling.dimension.time.enums.Grain
import com.xiaomi.duckling.dimension.time.form.Form
import com.xiaomi.duckling.dimension.time.Helpers.countGrains

case object Repeat extends Dimension with Rules {
override val name: String = "Repeat"
Expand All @@ -35,7 +36,9 @@ case object Repeat extends Dimension with Rules {
case class RepeatData(interval: Option[DurationData] = None,
n: Option[Int] = None,
start: Option[TimeData] = None,
workdayType: Option[WorkdayType] = None)
workdayType: Option[WorkdayType] = None,
repeatGrain: Option[Grain] = None,
repeatNFromInterval: Option[TimeData] = None)
extends Resolvable {

override def resolve(context: Context,
Expand All @@ -48,7 +51,19 @@ case class RepeatData(interval: Option[DurationData] = None,
}
case None => (None, true)
}
if (success) Some(RepeatValue(interval, n, instant, workdayType), false)
val repeatN = repeatNFromInterval match {
case Some(intervalTimeData) =>
intervalTimeData.resolve(context, options) match {
case Some((tv: TimeValue, _)) =>
tv.timeValue match {
case IntervalValue(start, end) => Some(countGrains(start, end))
case _ => None
}
case _ => None
}
case _ => None
}
if (success) Some(RepeatValue(interval, n.orElse(repeatN), instant, repeatGrain = repeatNFromInterval.map(_.timeGrain), workdayType), false)
else None
}
}
Expand All @@ -63,6 +78,7 @@ case class RepeatData(interval: Option[DurationData] = None,
case class RepeatValue(interval: Option[DurationData] = None,
n: Option[Int] = None,
start: Option[(TimeValue, Option[Form])] = None,
repeatGrain: Option[Grain] = None,
workdayType: Option[WorkdayType] = None)
extends ResolvedValue {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import com.xiaomi.duckling.Types._
import com.xiaomi.duckling.dimension.DimRules
import com.xiaomi.duckling.dimension.implicits._
import com.xiaomi.duckling.dimension.matcher.GroupMatch
import com.xiaomi.duckling.dimension.matcher.Prods.{regexMatch, singleRegexMatch}
import com.xiaomi.duckling.dimension.time.{form, Time, TimeData}
import com.xiaomi.duckling.dimension.matcher.Prods.regexMatch
import com.xiaomi.duckling.dimension.time.{form, GrainWrapper, Time, TimeData}
import com.xiaomi.duckling.dimension.time.duration.{Duration, DurationData}
import com.xiaomi.duckling.dimension.time.enums.{Grain, Hint}
import com.xiaomi.duckling.dimension.time.predicates.{isAPartOfDay, isATimeOfDay, isHint, isNotLatent, isTimeDatePredicate, IntersectTimePredicate, TimeDatePredicate, TimeIntervalsPredicate}
import com.xiaomi.duckling.dimension.time.helper.TimeDataHelpers.intersect
import com.xiaomi.duckling.dimension.time.predicates._

trait Rules extends DimRules with LazyLogging {
/**
Expand Down Expand Up @@ -155,4 +156,16 @@ trait Rules extends DimRules with LazyLogging {
workdaysTime(rd, td)
}
)

// 周一到周五的早上八点
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 =>
val oInterval = outer.timePred.asInstanceOf[TimeIntervalsPredicate]
// start
val start = intersect(TimeData(oInterval.p1, timeGrain=outer.timeGrain), inner)
Token(Repeat, RepeatData(start = start, repeatNFromInterval = outer))
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ object Examples extends DimExamples {
implicit def _toTuple(tv: TimeValue) = Option(tv, None: Option[Form])

override def pairs: List[(ResolvedValue, List[String])] = List(
(RepeatValue(start = (datetimeInterval(
new DuckDateTime(LocalDateTime.of(2013, 2, 18, 12, 0, 0)),
new DuckDateTime(LocalDateTime.of(2013, 2, 18, 18, 0, 0)),
Hour), None), repeatGrain = Day, n = 3), List("周一到周三下午")),
(RepeatValue(DurationData(1, Day, schema = "P1D")), List("每天")),
(RepeatValue(DurationData(1, Week, schema = "P1W")), List("每周")),
(RepeatValue(DurationData(15, Minute, schema = "PT15M")), List("每隔15分钟", "隔15分钟")),
Expand Down
2 changes: 1 addition & 1 deletion duckling-fork-chinese/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.8.0
sbt.version=1.10.1

0 comments on commit 195850e

Please sign in to comment.