Numeral/JA: HashMap lookups for large regexes

Summary: Replaced pattern matching with Hashmap. Also, removed ruleInteger17 and moved its regex to ruleInteger.

Reviewed By: patapizza

Differential Revision: D5812629

fbshipit-source-id: f0c1a06
This commit is contained in:
Ian Stewart-Binks 2017-10-06 10:18:56 -07:00 committed by Facebook Github Bot
parent fd77036a72
commit 2b566eeac0

View File

@ -12,7 +12,10 @@
module Duckling.Numeral.JA.Rules
( rules ) where
import qualified Data.HashMap.Strict as HashMap
import qualified Data.Text as Text
import Data.HashMap.Strict (HashMap)
import Data.Text (Text)
import Prelude
import Data.String
@ -44,30 +47,6 @@ ruleNumeralsPrefixWithNegativeOrMinus = Rule
_ -> Nothing
}
ruleInteger17 :: Rule
ruleInteger17 = Rule
{ name = "integer (0..10)"
, pattern =
[ regex "(ゼロ|零|一|二|三|四|五|六|七|八|九|十)"
]
, prod = \tokens -> case tokens of
(Token RegexMatch (GroupMatch (match:_)):_) -> case match of
"ゼロ" -> integer 0
"" -> integer 0
"" -> integer 1
"" -> integer 2
"" -> integer 3
"" -> integer 4
"" -> integer 5
"" -> integer 6
"" -> integer 7
"" -> integer 8
"" -> integer 9
"" -> integer 10
_ -> Nothing
_ -> Nothing
}
ruleIntegerNumeric :: Rule
ruleIntegerNumeric = Rule
{ name = "integer (numeric)"
@ -270,27 +249,31 @@ ruleInteger9 = Rule
, prod = \_ -> integer 1000
}
integerMap :: HashMap Text Integer
integerMap = HashMap.fromList
[ ("", 0)
, ("ゼロ", 0)
, ("", 1)
, ("", 2)
, ("", 3)
, ("", 4)
, ("", 5)
, ("", 6)
, ("", 7)
, ("", 8)
, ("", 9)
, ("", 10)
]
ruleInteger :: Rule
ruleInteger = Rule
{ name = "integer (0..10)"
, pattern =
[ regex "ゼロ|零|一|二|三|四|五|六|七|八|九|十"
[ regex "(ゼロ|零|一|二|三|四|五|六|七|八|九|十)"
]
, prod = \tokens -> case tokens of
(Token RegexMatch (GroupMatch (match:_)):_) -> case match of
"" -> integer 0
"ゼロ" -> integer 0
"" -> integer 1
"" -> integer 2
"" -> integer 3
"" -> integer 4
"" -> integer 5
"" -> integer 6
"" -> integer 7
"" -> integer 8
"" -> integer 9
"" -> integer 10
_ -> Nothing
(Token RegexMatch (GroupMatch (match:_)):_) ->
HashMap.lookup (Text.toLower match) integerMap >>= integer
_ -> Nothing
}
@ -356,7 +339,6 @@ rules =
, ruleInteger14
, ruleInteger15
, ruleInteger16
, ruleInteger17
, ruleInteger2
, ruleInteger3
, ruleInteger4