Distance - introduce interval rules

Reviewed By: haoxuany

Differential Revision: D26256269

Pulled By: chessai

fbshipit-source-id: 0c3ca267158fd5189fef5540d5bbb903b0dd00b4
This commit is contained in:
kcnhk1@gmail.com 2021-02-04 11:54:53 -08:00 committed by Facebook GitHub Bot
parent 722cc838ff
commit 9a6aeb9b51
2 changed files with 99 additions and 2 deletions

View File

@ -78,4 +78,20 @@ allExamples = concat
, "3.9米"
, "三米九"
]
, examples (between Metre (1.6, 3.9))
[ "1.6m-3.9m"
, "1.6~3.9米"
, "一點六至三點九公尺"
, "米六到三米九"
]
, examples (under Mile 3.5)
[ "最多三點五英里"
, "3.5英裏以下"
]
, examples (above Inch 5)
[ "至少5\""
, "最少五吋"
, "五英吋以上"
, "起碼五英寸"
]
]

View File

@ -17,8 +17,10 @@ import Prelude
import Duckling.Dimensions.Types
import Duckling.Distance.Helpers
import Duckling.Distance.Types (DistanceData(..))
import Duckling.Numeral.Helpers
import Duckling.Numeral.Types
import Duckling.Regex.Types
import Duckling.Types
import qualified Duckling.Distance.Types as TDistance
import qualified Duckling.Numeral.Types as TNumeral
@ -70,7 +72,7 @@ ruleDistFeetAndDistInch = Rule
[ dimension Distance
, regex "'|f(oo|ee)?ts?|英尺|呎"
, dimension Distance
, regex "''|inch(es)?|英寸|英吋|吋"
, regex "''|\"|inch(es)?|英寸|英吋|吋"
]
, prod = \case
(Token Distance dd:_) ->
@ -83,7 +85,7 @@ ruleDistInch = Rule
{ name = "<dist> inch"
, pattern =
[ dimension Distance
, regex "''|inch(es)?|英寸|英吋|吋"
, regex "''|\"|inch(es)?|英寸|英吋|吋"
]
, prod = \case
(Token Distance dd:_) ->
@ -145,6 +147,79 @@ ruleDistMetersAnd = Rule
_ -> Nothing
}
ruleIntervalNumeralDash :: Rule
ruleIntervalNumeralDash = Rule
{ name = "<numeral> - <dist>"
, pattern =
[ Predicate isPositive
, regex "-|~|到|至"
, Predicate isSimpleDistance
]
, prod = \case
(Token Numeral NumeralData{TNumeral.value = from}:
_:
Token Distance DistanceData{TDistance.value = Just to,
TDistance.unit = Just u}:_) | from < to ->
Just $ Token Distance $ withInterval (from, to) $ unitOnly u
_ -> Nothing
}
ruleIntervalDash :: Rule
ruleIntervalDash = Rule
{ name = "<dist> - <dist>"
, pattern =
[ Predicate isSimpleDistance
, regex "-|~|到|至"
, Predicate isSimpleDistance
]
, prod = \case
(Token Distance DistanceData{TDistance.value = Just from,
TDistance.unit = Just u1}:
_:
Token Distance DistanceData{TDistance.value = Just to, TDistance.unit = Just u2}:
_) | from < to && u1 == u2 ->
Just $ Token Distance $ withInterval (from, to) $ unitOnly u1
_ -> Nothing
}
ruleIntervalBound :: Rule
ruleIntervalBound = Rule
{ name = "under/less/lower/no more than <amount-of-money> (最多|至少|最少)"
, pattern =
[ regex "(最多|至少|最少|起碼)"
, Predicate isSimpleDistance
]
, prod = \case
(Token RegexMatch (GroupMatch (match:_)):
Token Distance DistanceData{TDistance.value = Just to,
TDistance.unit = Just u}:
_) -> case match of
"最多" -> Just $ Token Distance $ withMax to $ unitOnly u
"最少" -> Just $ Token Distance $ withMin to $ unitOnly u
"至少" -> Just $ Token Distance $ withMin to $ unitOnly u
"起碼" -> Just $ Token Distance $ withMin to $ unitOnly u
_ -> Nothing
_ -> Nothing
}
ruleIntervalBound2 :: Rule
ruleIntervalBound2 = Rule
{ name = "under/less/lower/no more than <amount-of-money> (以下|以上)"
, pattern =
[ Predicate isSimpleDistance
, regex "(以下|以上)"
]
, prod = \case
(Token Distance DistanceData{TDistance.value = Just to,
TDistance.unit = Just u}:
Token RegexMatch (GroupMatch (match:_)):
_) -> case match of
"以下" -> Just $ Token Distance $ withMax to $ unitOnly u
"以上" -> Just $ Token Distance $ withMin to $ unitOnly u
_ -> Nothing
_ -> Nothing
}
rules :: [Rule]
rules =
[ ruleDistCentimeters
@ -156,4 +231,10 @@ rules =
, ruleDistMiles
, ruleDistOneMeterAnd
, ruleDistMetersAnd
, ruleDistOneMeterAnd
, ruleDistMetersAnd
, ruleIntervalNumeralDash
, ruleIntervalDash
, ruleIntervalBound
, ruleIntervalBound2
]