diff --git a/Duckling/Dimensions/HI.hs b/Duckling/Dimensions/HI.hs index 938fef7c..c18a6ec9 100644 --- a/Duckling/Dimensions/HI.hs +++ b/Duckling/Dimensions/HI.hs @@ -1,4 +1,4 @@ ---Copyright (c) 2016-present, Facebook, Inc. +-- Copyright (c) 2016-present, Facebook, Inc. -- All rights reserved. -- -- This source code is licensed under the BSD-style license found in the @@ -15,4 +15,5 @@ import Duckling.Dimensions.Types allDimensions :: [Some Dimension] allDimensions = [ This Numeral + , This Ordinal ] diff --git a/Duckling/Ordinal/HI/Corpus.hs b/Duckling/Ordinal/HI/Corpus.hs new file mode 100644 index 00000000..642c34f7 --- /dev/null +++ b/Duckling/Ordinal/HI/Corpus.hs @@ -0,0 +1,106 @@ +-- 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.Ordinal.HI.Corpus + ( corpus ) where + +import Data.String +import Prelude + +import Duckling.Locale +import Duckling.Ordinal.Types +import Duckling.Resolve +import Duckling.Testing.Types + +corpus :: Corpus +corpus = (testContext {locale = makeLocale HI Nothing}, allExamples) + +allExamples :: [Example] +allExamples = concat + [examples (OrdinalData 0) + [ "शून्य" + ] + , examples (OrdinalData 1) + [ "प्रथम" + , "पहला" + , "पहली" + , "पहले" + ] + , examples (OrdinalData 2) + [ "द्वितीय" + , "दूसरा" + , "दूसरी" + , "दूसरे" + ] + , examples (OrdinalData 3) + [ "तृतीय" + , "तीसरा" + , "तीसरी" + , "तीसरे" + ] + , examples (OrdinalData 4) + [ "चौथा" + , "चौथी" + , "चौथे" + ] + , examples (OrdinalData 5) + [ "पाँचवा" + , "पाँचवी" + , "पाँचवे" + , "5वा" + , "5वी" + , "5वे" + ] + , examples (OrdinalData 6) + [ "छठा" + , "छठी" + , "छठे" + ] + , examples (OrdinalData 10) + [ "दसवा" + , "दसवी" + , "दसवे" + , "10वा" + , "10वी" + , "10वे" + ] + , examples (OrdinalData 18) + [ "अठारहवा" + , "अठारहवी" + , "अठारहवे" + , "18वा" + , "18वी" + , "18वे" + ] + , examples (OrdinalData 25) + [ "पच्चीसवा" + , "पच्चीसवी" + , "पच्चीसवे" + , "25वा" + , "25वी" + , "25वे" + ] + , examples (OrdinalData 50) + [ "पचासवा" + , "पचासवी" + , "पचासवे" + , "50वा" + , "50वी" + , "50वे" + ] + , examples (OrdinalData 90) + [ "नब्बेवा" + , "नब्बेवी" + , "नब्बेवे" + , "90वा" + , "90वी" + , "90वे" + ] + ] diff --git a/Duckling/Ordinal/HI/Rules.hs b/Duckling/Ordinal/HI/Rules.hs new file mode 100644 index 00000000..28bb348b --- /dev/null +++ b/Duckling/Ordinal/HI/Rules.hs @@ -0,0 +1,123 @@ +-- 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 OverloadedStrings #-} +{-# LANGUAGE NoRebindableSyntax #-} + +module Duckling.Ordinal.HI.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 (parseInt) +import Duckling.Ordinal.Helpers +import Duckling.Regex.Types +import Duckling.Types + +ordinalsMap :: HashMap Text Int +ordinalsMap = HashMap.fromList + [ ( "शून्य" , 0 ) + , ( "प्रथम" , 1 ) + , ( "पहला" , 1 ) + , ( "पहली" , 1 ) + , ( "पहले" , 1 ) + , ( "द्वितीय" , 2 ) + , ( "दूसरा" , 2 ) + , ( "दूसरी" , 2 ) + , ( "दूसरे" , 2 ) + , ( "तृतीय" , 3 ) + , ( "तीसरा" , 3 ) + , ( "तीसरी" , 3 ) + , ( "तीसरे" , 3 ) + , ( "चौथा" , 4 ) + , ( "चौथी" , 4 ) + , ( "चौथे" , 4 ) + , ( "छठा" , 6 ) + , ( "छठी" , 6 ) + , ( "छठे" , 6 ) + ] + +cardinalsMap :: HashMap Text Int +cardinalsMap = HashMap.fromList + [ ( "पाँच", 5 ) + , ( "सात", 7 ) + , ( "आठ", 8 ) + , ( "नौ" , 9 ) + , ( "दस", 10 ) + , ( "ग्यारह", 11 ) + , ( "बारह", 12 ) + , ( "तेरह", 13 ) + , ( "चौदह", 14 ) + , ( "पन्द्रह", 15 ) + , ( "सोलह", 16 ) + , ( "सत्रह", 17 ) + , ( "अठारह", 18 ) + , ( "उन्नीस", 19 ) + , ( "बीस", 20 ) + , ( "इक्कीस", 21 ) + , ( "बाईस", 22 ) + , ( "तेईस", 23 ) + , ( "चौबीस", 24 ) + , ( "पच्चीस", 25 ) + , ( "छब्बीस", 26 ) + , ( "सत्ताईस", 27 ) + , ( "अट्ठाईस", 28 ) + , ( "उनतीस", 29 ) + , ( "तीस", 30 ) + , ( "चालीस", 40 ) + , ( "पचास", 50 ) + , ( "साठ", 60 ) + , ( "सत्तर", 70 ) + , ( "अस्सी", 80 ) + , ( "नब्बे", 90 ) + ] + +ruleOrdinals :: Rule +ruleOrdinals = Rule + { name = "ordinals (first..fourth, sixth)" + , pattern = [regex "(शून्य|प्रथम|पहला|पहली|पहले|द्वितीय|दूसरा|दूसरी|दूसरे|तृतीय|तीसरा|तीसरी|तीसरे|चौथा|चौथी|चौथे|छठा|छठी|छठे)"] + , prod = \tokens -> case tokens of + (Token RegexMatch (GroupMatch (match:_)):_) -> + ordinal <$> HashMap.lookup (Text.toLower match) ordinalsMap + _ -> Nothing + } + +ruleOtherOrdinals :: Rule +ruleOtherOrdinals = Rule + { name = "ordinals (fifth, seventh ...)" + , pattern = [regex "(पाँच|सात|आठ|नौ|दस|ग्यारह|बारह|तेरह|चौदह|पन्द्रह|सोलह|सत्रह|अठारह|उन्नीस|बीस|इक्कीस|बाईस|तेईस|चौबीस|पच्चीस|छब्बीस|सत्ताईस|अट्ठाईस|उनतीस|तीस|चालीस|पचास|साठ|सत्तर|अस्सी|नब्बे)(वा|वी|वे)"] + , prod = \tokens -> case tokens of + (Token RegexMatch (GroupMatch (match:_)):_) -> + ordinal <$> HashMap.lookup (Text.toLower match) cardinalsMap + _ -> Nothing + } + + +ruleOrdinalDigits :: Rule +ruleOrdinalDigits = Rule + { name = "ordinal (digits)" + , pattern = [regex "0*(\\d+) ?(वा|वी|वे)"] + , prod = \tokens -> case tokens of + (Token RegexMatch (GroupMatch (match:_)):_) -> ordinal <$> parseInt match + _ -> Nothing + } + +rules :: [Rule] +rules = + [ ruleOrdinals + , ruleOtherOrdinals + , ruleOrdinalDigits + ] diff --git a/Duckling/Rules/HI.hs b/Duckling/Rules/HI.hs index d99e69ef..37446267 100644 --- a/Duckling/Rules/HI.hs +++ b/Duckling/Rules/HI.hs @@ -18,6 +18,7 @@ import Duckling.Dimensions.Types import Duckling.Locale import Duckling.Types import qualified Duckling.Numeral.HI.Rules as Numeral +import qualified Duckling.Ordinal.HI.Rules as Ordinal defaultRules :: Some Dimension -> [Rule] defaultRules = langRules @@ -31,7 +32,7 @@ langRules (This Distance) = [] langRules (This Duration) = [] langRules (This Numeral) = Numeral.rules langRules (This Email) = [] -langRules (This Ordinal) = [] +langRules (This Ordinal) = Ordinal.rules langRules (This PhoneNumber) = [] langRules (This Quantity) = [] langRules (This RegexMatch) = [] diff --git a/duckling.cabal b/duckling.cabal index 2426c242..011b5bb4 100644 --- a/duckling.cabal +++ b/duckling.cabal @@ -363,6 +363,8 @@ library , Duckling.Ordinal.GA.Rules , Duckling.Ordinal.HE.Corpus , Duckling.Ordinal.HE.Rules + , Duckling.Ordinal.HI.Corpus + , Duckling.Ordinal.HI.Rules , Duckling.Ordinal.HR.Corpus , Duckling.Ordinal.HR.Rules , Duckling.Ordinal.HU.Corpus @@ -752,6 +754,7 @@ test-suite duckling-test , Duckling.Ordinal.FR.Tests , Duckling.Ordinal.GA.Tests , Duckling.Ordinal.HE.Tests + , Duckling.Ordinal.HI.Tests , Duckling.Ordinal.HR.Tests , Duckling.Ordinal.HU.Tests , Duckling.Ordinal.ID.Tests diff --git a/tests/Duckling/Ordinal/HI/Tests.hs b/tests/Duckling/Ordinal/HI/Tests.hs new file mode 100644 index 00000000..6a50dcee --- /dev/null +++ b/tests/Duckling/Ordinal/HI/Tests.hs @@ -0,0 +1,24 @@ +-- 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.Ordinal.HI.Tests + ( tests + ) where + +import Data.String +import Prelude +import Test.Tasty + +import Duckling.Dimensions.Types +import Duckling.Ordinal.HI.Corpus +import Duckling.Testing.Asserts + +tests :: TestTree +tests = testGroup "HI Tests" + [ makeCorpusTest [This Ordinal] corpus + ] diff --git a/tests/Duckling/Ordinal/Tests.hs b/tests/Duckling/Ordinal/Tests.hs index 09c90ead..3cc17222 100644 --- a/tests/Duckling/Ordinal/Tests.hs +++ b/tests/Duckling/Ordinal/Tests.hs @@ -22,6 +22,7 @@ import qualified Duckling.Ordinal.ET.Tests as ET import qualified Duckling.Ordinal.FR.Tests as FR import qualified Duckling.Ordinal.GA.Tests as GA import qualified Duckling.Ordinal.HE.Tests as HE +import qualified Duckling.Ordinal.HI.Tests as HI import qualified Duckling.Ordinal.HR.Tests as HR import qualified Duckling.Ordinal.HU.Tests as HU import qualified Duckling.Ordinal.ID.Tests as ID @@ -52,6 +53,7 @@ tests = testGroup "Ordinal Tests" , FR.tests , GA.tests , HE.tests + , HI.tests , HR.tests , HU.tests , ID.tests