mirror of
https://github.com/facebook/duckling.git
synced 2024-09-17 16:17:13 +03:00
Add Quantity/ZH
Summary: Fixes #181. Closes https://github.com/facebook/duckling/pull/182 Reviewed By: patapizza Differential Revision: D7891288 Pulled By: haoxuany fbshipit-source-id: faa505dbf149c728545a92579cb2e92a89cd5cb8
This commit is contained in:
parent
ccab5533c7
commit
c84166d708
@ -18,6 +18,7 @@ allDimensions =
|
||||
, This Duration
|
||||
, This Numeral
|
||||
, This Ordinal
|
||||
, This Quantity
|
||||
, This Temperature
|
||||
, This Time
|
||||
]
|
||||
|
64
Duckling/Quantity/ZH/Corpus.hs
Normal file
64
Duckling/Quantity/ZH/Corpus.hs
Normal file
@ -0,0 +1,64 @@
|
||||
-- Copyright (c) 2016-present, Facebook, Inc.
|
||||
-- All rights reserved.
|
||||
--
|
||||
-- This source code is licensed under the BSD-style license found in the
|
||||
-- LICENSE file in the root directory of this source tree. An additional grant
|
||||
-- of patent rights can be found in the PATENTS file in the same directory.
|
||||
|
||||
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Duckling.Quantity.ZH.Corpus
|
||||
( corpus
|
||||
) where
|
||||
|
||||
import Data.String
|
||||
import Prelude
|
||||
|
||||
import Duckling.Locale
|
||||
import Duckling.Quantity.Types
|
||||
import Duckling.Resolve
|
||||
import Duckling.Testing.Types
|
||||
|
||||
corpus :: Corpus
|
||||
corpus = (testContext {locale = makeLocale ZH Nothing},
|
||||
testOptions, allExamples)
|
||||
|
||||
allExamples :: [Example]
|
||||
allExamples = concat
|
||||
[ examples (simple Gram 2 Nothing)
|
||||
[ "2克"
|
||||
, "二克"
|
||||
, "两克"
|
||||
, "2g"
|
||||
, "2.0g"
|
||||
]
|
||||
, examples (simple Gram 1500 Nothing)
|
||||
[ "3斤"
|
||||
, "三斤"
|
||||
, "1.5公斤"
|
||||
, "1.5千克"
|
||||
, "1.5kg"
|
||||
, "1500g"
|
||||
]
|
||||
, examples (simple Gram 0.035 Nothing)
|
||||
[ "35毫克"
|
||||
, "三十五毫克"
|
||||
, "35mg"
|
||||
]
|
||||
, examples (simple Gram 1750 Nothing)
|
||||
[ "三斤半"
|
||||
, "3斤半"
|
||||
, "3斤5两"
|
||||
, "1.75千克"
|
||||
]
|
||||
, examples (simple Gram 475 Nothing)
|
||||
[ "9两半"
|
||||
, "九兩半"
|
||||
]
|
||||
, examples (simple Gram 3400 Nothing)
|
||||
[ "六斤八两"
|
||||
, "六斤8兩"
|
||||
, "3.4公斤"
|
||||
]
|
||||
]
|
122
Duckling/Quantity/ZH/Rules.hs
Normal file
122
Duckling/Quantity/ZH/Rules.hs
Normal file
@ -0,0 +1,122 @@
|
||||
-- Copyright (c) 2016-present, Facebook, Inc.
|
||||
-- All rights reserved.
|
||||
--
|
||||
-- This source code is licensed under the BSD-style license found in the
|
||||
-- LICENSE file in the root directory of this source tree. An additional grant
|
||||
-- of patent rights can be found in the PATENTS file in the same directory.
|
||||
|
||||
|
||||
{-# LANGUAGE GADTs #-}
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Duckling.Quantity.ZH.Rules
|
||||
( rules
|
||||
) where
|
||||
|
||||
import Data.HashMap.Strict (HashMap)
|
||||
import Data.String
|
||||
import Data.Text (Text)
|
||||
import Prelude
|
||||
import qualified Data.HashMap.Strict as HashMap
|
||||
import qualified Data.Text as Text
|
||||
|
||||
import Duckling.Dimensions.Types
|
||||
import Duckling.Numeral.Helpers
|
||||
import Duckling.Quantity.Helpers
|
||||
import Duckling.Regex.Types
|
||||
import Duckling.Types
|
||||
import qualified Duckling.Numeral.Types as TNumeral
|
||||
import qualified Duckling.Quantity.Types as TQuantity
|
||||
|
||||
quantities :: [(Text, String, TQuantity.Unit)]
|
||||
quantities =
|
||||
[ ("<quantity> grams", "(千克|公斤|斤|两|兩|克|毫克|kg|g|mg)", TQuantity.Gram)
|
||||
]
|
||||
|
||||
opsMap :: HashMap Text (Double -> Double)
|
||||
opsMap = HashMap.fromList
|
||||
[ ( "千克" , (* 1000))
|
||||
, ( "公斤", (* 1000))
|
||||
, ( "kg", (* 1000))
|
||||
, ( "斤", (* 500))
|
||||
, ( "两", (* 50))
|
||||
, ( "兩", (* 50))
|
||||
, ( "毫克", (/ 1000))
|
||||
, ( "mg", (/ 1000))
|
||||
]
|
||||
|
||||
ruleNumeralQuantities :: [Rule]
|
||||
ruleNumeralQuantities = map go quantities
|
||||
where
|
||||
getValue :: Text -> Double -> Double
|
||||
getValue match = HashMap.lookupDefault id (Text.toLower match) opsMap
|
||||
|
||||
go :: (Text, String, TQuantity.Unit) -> Rule
|
||||
go (name, regexPattern, u) = Rule
|
||||
{ name = name
|
||||
, pattern =
|
||||
[ Predicate isPositive
|
||||
, regex regexPattern
|
||||
]
|
||||
, prod = \case
|
||||
(Token Numeral nd:
|
||||
Token RegexMatch (GroupMatch (match:_)):
|
||||
_) -> Just . Token Quantity $ quantity u value
|
||||
where value = getValue match $ TNumeral.value nd
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
ruleCattyTael :: Rule
|
||||
ruleCattyTael = Rule
|
||||
{ name = "<quantity> catty <quantity> tael"
|
||||
, pattern =
|
||||
[ Predicate isPositive
|
||||
, regex "斤"
|
||||
, numberBetween 1 10
|
||||
, regex "两|兩"
|
||||
]
|
||||
, prod = \case
|
||||
(Token Numeral TNumeral.NumeralData{TNumeral.value = x}:
|
||||
Token RegexMatch (GroupMatch _):
|
||||
Token Numeral TNumeral.NumeralData{TNumeral.value = y}:
|
||||
Token RegexMatch (GroupMatch _):
|
||||
_) -> Just . Token Quantity $ quantity TQuantity.Gram (x * 500 + y * 50)
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
ruleCattyHalf :: Rule
|
||||
ruleCattyHalf = Rule
|
||||
{ name = "<quantity> catty half"
|
||||
, pattern =
|
||||
[ Predicate isPositive
|
||||
, regex "斤半"
|
||||
]
|
||||
, prod = \case
|
||||
(Token Numeral TNumeral.NumeralData{TNumeral.value = x}:
|
||||
Token RegexMatch (GroupMatch _):
|
||||
_) -> Just . Token Quantity $ quantity TQuantity.Gram (x * 500 + 250)
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
ruleTaelHalf :: Rule
|
||||
ruleTaelHalf = Rule
|
||||
{ name = "<quantity> tael half"
|
||||
, pattern =
|
||||
[ Predicate isPositive
|
||||
, regex "(两|兩)半"
|
||||
]
|
||||
, prod = \case
|
||||
(Token Numeral TNumeral.NumeralData{TNumeral.value = x}:
|
||||
Token RegexMatch (GroupMatch _):
|
||||
_) -> Just . Token Quantity $ quantity TQuantity.Gram (x * 50 + 25)
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
rules :: [Rule]
|
||||
rules =
|
||||
[ ruleCattyTael
|
||||
, ruleCattyHalf
|
||||
, ruleTaelHalf
|
||||
]
|
||||
++ ruleNumeralQuantities
|
@ -22,6 +22,7 @@ import qualified Duckling.AmountOfMoney.ZH.Rules as AmountOfMoney
|
||||
import qualified Duckling.Distance.ZH.Rules as Distance
|
||||
import qualified Duckling.Numeral.ZH.Rules as Numeral
|
||||
import qualified Duckling.Ordinal.ZH.Rules as Ordinal
|
||||
import qualified Duckling.Quantity.ZH.Rules as Quantity
|
||||
import qualified Duckling.Temperature.ZH.Rules as Temperature
|
||||
import qualified Duckling.Time.ZH.Rules as Time
|
||||
import qualified Duckling.Time.ZH.CN.Rules as TimeCN
|
||||
@ -49,7 +50,7 @@ langRules (This Email) = []
|
||||
langRules (This Numeral) = Numeral.rules
|
||||
langRules (This Ordinal) = Ordinal.rules
|
||||
langRules (This PhoneNumber) = []
|
||||
langRules (This Quantity) = []
|
||||
langRules (This Quantity) = Quantity.rules
|
||||
langRules (This RegexMatch) = []
|
||||
langRules (This Temperature) = Temperature.rules
|
||||
langRules (This Time) = Time.rules
|
||||
|
@ -452,6 +452,8 @@ library
|
||||
, Duckling.Quantity.RO.Rules
|
||||
, Duckling.Quantity.RU.Corpus
|
||||
, Duckling.Quantity.RU.Rules
|
||||
, Duckling.Quantity.ZH.Corpus
|
||||
, Duckling.Quantity.ZH.Rules
|
||||
, Duckling.Quantity.Helpers
|
||||
, Duckling.Quantity.Types
|
||||
|
||||
@ -824,6 +826,7 @@ test-suite duckling-test
|
||||
, Duckling.Quantity.PT.Tests
|
||||
, Duckling.Quantity.RO.Tests
|
||||
, Duckling.Quantity.RU.Tests
|
||||
, Duckling.Quantity.ZH.Tests
|
||||
, Duckling.Quantity.Tests
|
||||
|
||||
-- Temperature
|
||||
|
@ -22,6 +22,7 @@ import qualified Duckling.Quantity.KO.Tests as KO
|
||||
import qualified Duckling.Quantity.PT.Tests as PT
|
||||
import qualified Duckling.Quantity.RO.Tests as RO
|
||||
import qualified Duckling.Quantity.RU.Tests as RU
|
||||
import qualified Duckling.Quantity.ZH.Tests as ZH
|
||||
|
||||
tests :: TestTree
|
||||
tests = testGroup "Quantity Tests"
|
||||
@ -33,4 +34,5 @@ tests = testGroup "Quantity Tests"
|
||||
, PT.tests
|
||||
, RO.tests
|
||||
, RU.tests
|
||||
, ZH.tests
|
||||
]
|
||||
|
23
tests/Duckling/Quantity/ZH/Tests.hs
Normal file
23
tests/Duckling/Quantity/ZH/Tests.hs
Normal file
@ -0,0 +1,23 @@
|
||||
-- Copyright (c) 2016-present, Facebook, Inc.
|
||||
-- All rights reserved.
|
||||
--
|
||||
-- This source code is licensed under the BSD-style license found in the
|
||||
-- LICENSE file in the root directory of this source tree. An additional grant
|
||||
-- of patent rights can be found in the PATENTS file in the same directory.
|
||||
|
||||
|
||||
module Duckling.Quantity.ZH.Tests
|
||||
( tests ) where
|
||||
|
||||
import Data.String
|
||||
import Prelude
|
||||
import Test.Tasty
|
||||
|
||||
import Duckling.Dimensions.Types
|
||||
import Duckling.Quantity.ZH.Corpus
|
||||
import Duckling.Testing.Asserts
|
||||
|
||||
tests :: TestTree
|
||||
tests = testGroup "ZH Tests"
|
||||
[ makeCorpusTest [This Quantity] corpus
|
||||
]
|
Loading…
Reference in New Issue
Block a user