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

feat: 扩充距离表达方式 #210

Merged
merged 1 commit into from
Jan 30, 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 @@ -30,13 +30,13 @@ case object Quantity extends Dimension with Rules {
override val dimDependents: List[Dimension] = List(Numeral)
}

case class QuantityData(v: Double, unit: String, dim: String, isLatent: Boolean = false)
case class QuantityData(v: Double, unit: String, dim: String, isLatent: Boolean = false, originValue: Option[Double] = None, originUnit: Option[String] = None)
extends Resolvable
with ResolvedValue {
override def resolve(context: Context, options: Options): Option[(ResolvedValue, Boolean)] =
(QuantityValue(v, unit, dim), isLatent)
(QuantityValue(v, unit, dim, originValue, originUnit), isLatent)
}

case class QuantityValue(v: Double, unit: String, dim: String) extends ResolvedValue {
case class QuantityValue(v: Double, unit: String, dim: String, originValue: Option[Double] = None, originUnit: Option[String] = None) extends ResolvedValue {
override def schema: Option[String] = if ("温度".equalsIgnoreCase(dim)) Some(s"$v$unit") else Some(s"$v")
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,59 @@ trait Rules extends DimRules {

val dim = "Distance"

private val scalar = Map(
private val scalar: Map[String, Double] = Map(
"千米" -> 1000,
"公里" -> 1000,
"km" -> 1000,
"米" -> 1,
"m" -> 1
"m" -> 1,
"里地" -> 500,
"里" -> 500,
"英里" -> 1609.31,
"海里" -> 1853,
"分米" -> 0.1,
"dm" -> 0.1,
"厘米" -> 0.01,
"cm" -> 0.01,
"毫米" -> 0.001,
"mm" -> 0.001,
"微米" -> 1.0E-6,
"µm" -> 1.0E-6,
"纳米" -> 1.0E-9,
"nm" -> 1.0E-9,
"皮米" -> 1.0E-12,
"pm" -> 1.0E-12,
"丈" -> 3.33,
"尺" -> 0.333,
"英尺" -> 0.304794,
"寸" -> 0.0333,
"英寸" -> 0.025399,
"码" -> 0.914383,
"光年" -> 9.46E+15
)

def unit(s: String): String = s match {
case "千米" | "km" => "千米"
case "米" | "m" => "米"
case "里地" | "里" => "里"
case "英里" | "英里" => "英里"
case "海里" | "海里" => "海里"
case "分米" | "dm" => "分米"
case "厘米" | "cm" => "厘米"
case "毫米" | "mm" => "毫米"
case "微米" | "µm" => "微米"
case "纳米" | "nm" => "纳米"
case "皮米" | "pm" => "皮米"
case _ => s
}

val numberKm = Rule(
name = "<number> km",
pattern = List(isDimension(Numeral).predicate, "(?i)千米|米|公里|km|m".regex),
pattern = List(isDimension(Numeral).predicate, "(?i)千米|公里|km|里地|英里|海里|分米|dm|厘米|cm|毫米|mm|微米|µm|纳米|nm|皮米|pm|英尺|英寸|光年|丈|码|寸|尺|米|m|里".regex),
prod = tokens {
case Token(_, NumeralData(value, _, _, _, _, _)) :: Token(_, GroupMatch(s :: _)) :: _ =>
scalar.get(s.toLowerCase) match {
case Some(t) => Token(Distance, QuantityData(value * t, "米", dim))
case Some(t) => Token(Distance, QuantityData(value * t, "米", dim, originValue = Some(value), originUnit = Some(unit(s.toLowerCase))))
case None => throw new IllegalArgumentException(s"unknown scalar found: $s")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.xiaomi.duckling.dimension.numeral.fraction.{Fraction, FractionData}
import com.xiaomi.duckling.dimension.numeral.{DoubleSideIntervalValue, Numeral, NumeralValue, OpenIntervalValue}
import com.xiaomi.duckling.dimension.ordinal.{Ordinal, OrdinalData}
import com.xiaomi.duckling.dimension.quantity.QuantityValue
import com.xiaomi.duckling.dimension.quantity.distance.Distance
import com.xiaomi.duckling.dimension.quantity.velocity.Velocity
import com.xiaomi.duckling.dimension.rating.Rating
import com.xiaomi.duckling.dimension.season.Season
import com.xiaomi.duckling.dimension.temperature.Temperature
Expand All @@ -45,7 +47,7 @@ class ApiTest extends UnitSpec {

it("should date") {
val options = testOptions.copy(targets = Set(Numeral, Time, Duration, Date), full = false)
println(Api.analyze("1分9秒", testContext, options))
println(Api.analyze("一年前的今天", testContext, options))
}

it("should age") {
Expand Down Expand Up @@ -200,6 +202,21 @@ class ApiTest extends UnitSpec {
}
}
}

it("should distance") {
val options = testOptions.copy(targets = Set(Distance), full = false)
val queries = List("三十公里", "十海里", "一光年", "三毫米", "三英寸")
queries.foreach{
query =>
Api.analyze(query, testContext, options).foreach{
answer =>
answer.token.value match {
case data: QuantityValue => println(data)
case _ => println("error")
}
}
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class QuantityTest extends UnitSpec {
options.copy(targets = Set(Quantity, Numeral), full = false)
)
answers(0).token.value match {
case QuantityValue(1.0, "千米", _) => true shouldBe true
case QuantityValue(1.0, "千米", _, _, _) => true shouldBe true
case _ => true shouldBe (false)
}
answers(1).token.value match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TemperatureTest extends UnitSpec {
describe("TemperatureTest") {
it("simple") {
temperatureAnalyze("2十一°F").token.value match {
case QuantityValue(v, unit, dim) =>
case QuantityValue(v, unit, dim, _, _) =>
(v, unit, dim) shouldBe ((21.0, "F", "温度"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,25 @@ import com.xiaomi.duckling.dimension.quantity.QuantityValue

object Examples extends DimExamples {
override def pairs: List[(Types.ResolvedValue, List[String])] = List(
(QuantityValue(3000, "米", "Distance"), List("3千米", "三公里", "3km")),
(QuantityValue(3000, "米", "Distance"), List("3000米", "3000m"))
(QuantityValue(3000, "米", "Distance", originValue = Some(3), originUnit = Some("千米")), List("3千米", "3km", "3Km", "3KM")),
(QuantityValue(3000, "米", "Distance", originValue = Some(3), originUnit = Some("公里")), List("三公里")),
(QuantityValue(3000, "米", "Distance", originValue = Some(3000), originUnit = Some("米")), List("3000米", "3000m", "3000M")),
(QuantityValue(1000, "米", "Distance", originValue = Some(2), originUnit = Some("里")), List("二里地", "2里")),
(QuantityValue(16093.1, "米", "Distance", originValue = Some(10), originUnit = Some("英里")), List("十英里", "10英里")),
(QuantityValue(5559, "米", "Distance", originValue = Some(3), originUnit = Some("海里")), List("三海里", "3海里")),
(QuantityValue(0.1, "米", "Distance", originValue = Some(1), originUnit = Some("分米")), List("一分米", "1dm", "1DM")),
(QuantityValue(0.1, "米", "Distance", originValue = Some(10), originUnit = Some("厘米")), List("十厘米", "10cm", "10CM")),
(QuantityValue(0.003, "米", "Distance", originValue = Some(3), originUnit = Some("毫米")), List("三毫米", "3mm", "3MM")),
(QuantityValue(3.0E-6, "米", "Distance", originValue = Some(3), originUnit = Some("微米")), List("3微米", "3µm", "3µM")),
(QuantityValue(3.0E-9, "米", "Distance", originValue = Some(3), originUnit = Some("纳米")), List("3纳米", "3nm", "3NM")),
(QuantityValue(1.0E-11, "米", "Distance", originValue = Some(10), originUnit = Some("皮米")), List("十皮米", "10pm", "10PM")),
(QuantityValue(6.66, "米", "Distance", originValue = Some(2), originUnit = Some("丈")), List("两丈")),
(QuantityValue(0.999, "米", "Distance", originValue = Some(3), originUnit = Some("尺")), List("三尺")),
(QuantityValue(0.304794, "米", "Distance", originValue = Some(1), originUnit = Some("英尺")), List("一英尺")),
(QuantityValue(0.0999, "米", "Distance", originValue = Some(3), originUnit = Some("寸")), List("三寸")),
(QuantityValue(0.025399, "米", "Distance", originValue = Some(1), originUnit = Some("英寸")), List("1英寸")),
(QuantityValue(0.914383, "米", "Distance", originValue = Some(1), originUnit = Some("码")), List("1码")),
(QuantityValue(9.46E+16, "米", "Distance", originValue = Some(10), originUnit = Some("光年")), List("10光年"))
)

override val dimension: Dimension = Distance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ object Examples extends DimExamples {
(ymd(2013, 10, 1, holiday = "国庆节"), List("下一个国庆节")),
(ymd(2014, 1, 1, holiday = "元旦节"), List("下一个元旦节")),
(ymd(2013, 8, 15, holiday = "中秋节", calendar = Lunar(false)), List("下一个中秋节")),
(ymd(2012, 2, 12), List("一年前的今天")),
// (ymd(2012, 2, 12), List("一年前的今天")),
(ymd(2022, 10, 1, direction = IntervalDirection.Before), List("2022年10月1号之前")),
(ymd(2022, 10, 1, direction = IntervalDirection.After), List("2022年10月1号之后")),
(h(12), List("今天12点")),
Expand Down
Loading