From 21c9b8ed7ac6ff6c4724646ab1511145b1189d0b Mon Sep 17 00:00:00 2001 From: Mohankumar Dhayalan Date: Thu, 25 May 2017 10:40:08 -0700 Subject: [PATCH] HashMap lookups for large regexes Summary: Added Hashmap lookups for Regex for Numeral/ID Reviewed By: patapizza Differential Revision: D5128492 fbshipit-source-id: 5ab928b --- Duckling/Numeral/ID/Rules.hs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Duckling/Numeral/ID/Rules.hs b/Duckling/Numeral/ID/Rules.hs index 10713ea1..b8cb0dbb 100644 --- a/Duckling/Numeral/ID/Rules.hs +++ b/Duckling/Numeral/ID/Rules.hs @@ -12,8 +12,11 @@ module Duckling.Numeral.ID.Rules ( rules ) where +import qualified Data.HashMap.Strict as HashMap +import Data.HashMap.Strict (HashMap) import Data.Maybe import qualified Data.Text as Text +import Data.Text (Text) import Prelude import Data.String @@ -24,6 +27,22 @@ import qualified Duckling.Numeral.Types as TNumeral import Duckling.Regex.Types import Duckling.Types +ruleIntegerMap :: HashMap Text Integer +ruleIntegerMap = HashMap.fromList + [ ( "kosong" , 0 ) + , ( "nol" , 0 ) + , ( "satu" , 1 ) + , ( "dua" , 2 ) + , ( "tiga" , 3 ) + , ( "empat" , 4 ) + , ( "lima" , 5 ) + , ( "enam" , 6 ) + , ( "tujuh" , 7 ) + , ( "delapan" , 8 ) + , ( "sembilan", 9 ) + , ( "sebelas" , 11 ) + ] + ruleTeen :: Rule ruleTeen = Rule { name = "teen" @@ -209,20 +228,8 @@ ruleInteger = Rule [ regex "(kosong|nol|satu|dua|tiga|empat|lima|enam|tujuh|delapan|sembilan|sebelas)" ] , prod = \tokens -> case tokens of - (Token RegexMatch (GroupMatch (match:_)):_) -> case Text.toLower match of - "kosong" -> integer 0 - "nol" -> integer 0 - "satu" -> integer 1 - "dua" -> integer 2 - "tiga" -> integer 3 - "empat" -> integer 4 - "lima" -> integer 5 - "enam" -> integer 6 - "tujuh" -> integer 7 - "delapan" -> integer 8 - "sembilan" -> integer 9 - "sebelas" -> integer 11 - _ -> Nothing + (Token RegexMatch (GroupMatch (match:_)):_) -> + HashMap.lookup (Text.toLower match) ruleIntegerMap >>= integer _ -> Nothing }