Skip to content

Commit

Permalink
[DUCK]temperature支持25度半 (#185)
Browse files Browse the repository at this point in the history
Co-authored-by: zhangsonglei <zhangsonglei@xiaomi.com>
  • Loading branch information
zhangsonglei and zhangsonglei committed Jun 4, 2023
1 parent fde7c1e commit 66a304a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
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

0 comments on commit 66a304a

Please sign in to comment.