Skip to content

Commit

Permalink
[WIP] repeat 增加 重复区间+时间点/区间 的能力
Browse files Browse the repository at this point in the history
  • Loading branch information
du00cs committed Jul 31, 2024
1 parent 195850e commit ef11b77
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package com.xiaomi.duckling.dimension.time.date

import scalaz.std.string.parseInt
import java.time.LocalDate
import java.util.regex.Pattern

import scala.collection.mutable
import scala.util.Try

import com.xiaomi.duckling.Types._
Expand All @@ -33,7 +35,7 @@ import com.xiaomi.duckling.dimension.time.duration.{isADecade, Duration, Duratio
import com.xiaomi.duckling.dimension.time.enums.Grain._
import com.xiaomi.duckling.dimension.time.enums.Hint.{NoHint, RecentNominal, YearMonth}
import com.xiaomi.duckling.dimension.time.enums.IntervalType.{Closed, Open}
import com.xiaomi.duckling.dimension.time.enums.{Grain, Hint}
import com.xiaomi.duckling.dimension.time.enums.{Grain, Hint, IntervalType}
import com.xiaomi.duckling.dimension.time.helper.TimeDataHelpers._
import com.xiaomi.duckling.dimension.time.predicates.{EndOfGrainPredicate, SequencePredicate, TimeDatePredicate, _}
import com.xiaomi.duckling.dimension.time.{GrainWrapper, TimeData}
Expand Down Expand Up @@ -398,4 +400,38 @@ trait Rules extends DimRules {
}
}
)

private val WeekX = Pattern.compile("(周|星期)([一二三四五六日天])")
private val WeekDayStr = "12345671234567"

val ruleWeekXyz = Rule(
name = "周一周二",
pattern = List("((周|星期)[一二三四五六日天]){2,}".regex),
prod = regexMatch {case text :: _ =>
val m = WeekX.matcher(text)
var pos = 0
val days = mutable.MutableList[Int]()
while (m.find(pos)) {
val d = m.group(2) match {
case "" => 1
case "" => 2
case "" => 3
case "" => 4
case "" => 5
case "" => 6
case "" | "" => 7
}
days += d
pos = m.end()
}
WeekDayStr.indexOf(days.mkString("")) match {
case -1 => None
case _ =>
val td = TimeData(
TimeIntervalsPredicate(IntervalType.Closed, TimeDatePredicate(dayOfWeek = days.head), TimeDatePredicate(dayOfWeek = days.last), beforeEndOfInterval = true),
timeGrain = Day)
Token(Date, td)
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,25 @@ 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 =>
val oInterval = outer.timePred.asInstanceOf[TimeIntervalsPredicate]
// start
val start = intersect(TimeData(oInterval.p1, timeGrain=outer.timeGrain), inner)
val start = intersect(inner, TimeData(oInterval.p1, timeGrain=outer.timeGrain))
Token(Repeat, RepeatData(start = start, repeatNFromInterval = outer))
}
)

// 周一到周五早上的八点
val ruleIntervalTime1 = Rule(
name = "<interval> 的 <time/interval>",
pattern = List(isInterval.predicate, "".regex, 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]
val start = intersect(inner.copy(hint = Hint.NoHint), TimeData(oInterval.p1, timeGrain=outer.timeGrain).copy(hint = Hint.NoHint))
Token(Repeat, RepeatData(start = start, repeatNFromInterval = outer))
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.xiaomi.duckling.task
import java.nio.charset.StandardCharsets
import java.time.ZonedDateTime

import scala.util.control.Breaks.break

import org.fusesource.jansi.Ansi
import org.jline.reader.{EndOfFileException, LineReader, LineReaderBuilder}
import org.jline.reader.impl.completer._
Expand Down Expand Up @@ -143,7 +145,7 @@ object NaiveBayesConsole extends LazyLogging {
} else (false, options)
}

def round(reader: LineReader, options: Options): Options = {
def round(reader: LineReader, options: Options): (Boolean, Options) = {
reader.getTerminal.flush()

val line =
Expand All @@ -152,7 +154,6 @@ object NaiveBayesConsole extends LazyLogging {
} catch {
case ex: EndOfFileException =>
logger.info("bye!")
System.exit(0)
""
}

Expand All @@ -175,11 +176,11 @@ object NaiveBayesConsole extends LazyLogging {
}
}

_options
(line == "", _options)
}

def run(): Unit = {
var options = Options(targets = Set(), withLatent = false)
var options: Options = Options(targets = Set(), withLatent = false)
options.rankOptions.setRanker(Ranker.NaiveBayes)
options.rankOptions.setWinnerOnly(true)
options.rankOptions.setCombinationRank(false)
Expand All @@ -197,7 +198,9 @@ object NaiveBayesConsole extends LazyLogging {

val reader = buildLineReader()
while (true) {
options = round(reader, options)
round(reader, options) match {
case (true, _) => break()
}
}
}

Expand Down

0 comments on commit ef11b77

Please sign in to comment.