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] temperature支持25度半 #185

Merged
merged 1 commit into from
Jun 4, 2023
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 @@ -20,6 +20,7 @@ import com.xiaomi.duckling.Types._
import com.xiaomi.duckling.dimension.DimRules
import com.xiaomi.duckling.dimension.implicits._
import com.xiaomi.duckling.dimension.matcher.{GroupMatch, RegexMatch}
import com.xiaomi.duckling.dimension.numeral.Predicates.isInteger
import com.xiaomi.duckling.dimension.numeral.{Numeral, NumeralData}
import com.xiaomi.duckling.dimension.quantity.QuantityData

Expand Down Expand Up @@ -64,4 +65,36 @@ trait Rules extends DimRules {
Token(Temperature, QuantityData(value, unit, "温度"))
}
)

val ruleHalfTemperatureWithNegative = Rule(
name = "negative <number> unit like [31度半, 零下二十度半, 负的华氏31度半]",
pattern = List(
s"(华氏|摄氏)?${negatives.mkString("(", "|", ")?")}(华氏|摄氏)?".regex,
and(isInteger).predicate,
"度半".regex
),
prod = tokens {
case Token(RegexMatch, GroupMatch(prefix :: _)) :: Token(Numeral, NumeralData(value, _, _, _, _, _)
) :: _ :: _ =>
val negative =
if (prefix.nonEmpty && (prefix.contains("零下") || prefix.contains("负的") || prefix
.contains("负"))) -1
else +1
val unit =
if (prefix.contains("华氏")) "F" else "C"

val temperature_value = if (value >= 0) value + 0.5 else value - 0.5
Token(Temperature, QuantityData(negative * temperature_value, unit, "温度"))
}
)

val ruleHalfTemperature = Rule(
name = "<number> unit like [30度半]",
pattern = List(and(isInteger).predicate, "度半".regex),
prod = tokens {
case Token(Numeral, NumeralData(value, _, _, _, _, _)) :: _ :: _ =>
val temperature_value = if (value >= 0) value + 0.5 else value - 0.5
Token(Temperature, QuantityData(temperature_value, "C", "温度"))
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ class TemperatureTest extends UnitSpec {
("零下25.3度", Some("-25.3C")),
("华氏21度", Some("21.0F")),
("华氏零下十五度", Some("-15.0F")),
("华氏22点6度", Some("22.6F"))
("华氏22点6度", Some("22.6F")),
("23度半", Some("23.5C")),
("负22度半", Some("-22.5C")),
("华氏22度半", Some("22.5F")),
("摄氏22度半", Some("22.5C")),
("华氏零下三十一度半", Some("-31.5F")),
("零下摄氏三十一度半", Some("-31.5C"))
)

it("schema eq") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ object Examples extends DimExamples {
(QuantityValue(-25.3, "C", "温度"), List("零下25.3摄氏度", "零下25.3°C", "负的二十五点3度")),
(QuantityValue(21, "F", "温度"), List("华氏21度", "2十一华氏度")),
(QuantityValue(+22.6, "F", "温度"), List("华氏22点6度", "二十二点6华氏度")),
(QuantityValue(-13, "F", "温度"), List("零下华氏十3度", "华氏零下十3度", "负的13华氏度"))
(QuantityValue(-13, "F", "温度"), List("零下华氏十3度", "华氏零下十3度", "负的13华氏度")),
(QuantityValue(22.5, "C", "温度"), List("22度半", "摄氏二十二度半")),
(QuantityValue(-31.5, "F", "温度"), List("零下华氏三十一度半", "华氏零下三十一度半", "华氏-31度半")),
(QuantityValue(-11.5, "C", "温度"), List("负11度半", "摄氏零下11度半", "零下摄氏11度半"))
)

override val dimension: Dimension = Temperature
Expand Down
Loading