|
| 1 | +-- Copyright (c) 2016-present, Facebook, Inc. |
| 2 | +-- All rights reserved. |
| 3 | +-- |
| 4 | +-- This source code is licensed under the BSD-style license found in the |
| 5 | +-- LICENSE file in the root directory of this source tree. An additional grant |
| 6 | +-- of patent rights can be found in the PATENTS file in the same directory. |
| 7 | + |
| 8 | + |
| 9 | +{-# LANGUAGE GADTs #-} |
| 10 | +{-# LANGUAGE OverloadedStrings #-} |
| 11 | +{-# LANGUAGE NoRebindableSyntax #-} |
| 12 | + |
| 13 | +module Duckling.Numeral.HU.Rules |
| 14 | + ( rules ) where |
| 15 | + |
| 16 | +import Data.HashMap.Strict (HashMap) |
| 17 | +import Data.Maybe |
| 18 | +import Data.String |
| 19 | +import Data.Text (Text) |
| 20 | +import Prelude |
| 21 | +import qualified Data.HashMap.Strict as HashMap |
| 22 | +import qualified Data.Text as Text |
| 23 | + |
| 24 | +import Duckling.Dimensions.Types |
| 25 | +import Duckling.Numeral.Helpers |
| 26 | +import Duckling.Numeral.Types (NumeralData (..)) |
| 27 | +import Duckling.Regex.Types |
| 28 | +import Duckling.Types |
| 29 | +import qualified Duckling.Numeral.Types as TNumeral |
| 30 | + |
| 31 | +ruleIntegerNumeric :: Rule |
| 32 | +ruleIntegerNumeric = Rule |
| 33 | + { name = "integer (numeric)" |
| 34 | + , pattern = |
| 35 | + [ regex "(\\d{1,18})" |
| 36 | + ] |
| 37 | + , prod = \tokens -> case tokens of |
| 38 | + (Token RegexMatch (GroupMatch (match:_)): |
| 39 | + _) -> do |
| 40 | + v <- parseInt match |
| 41 | + integer $ toInteger v |
| 42 | + _ -> Nothing |
| 43 | + } |
| 44 | + |
| 45 | +ruleNumeralMap :: HashMap Text Integer |
| 46 | +ruleNumeralMap = HashMap.fromList |
| 47 | + [ ( "nulla", 0 ) |
| 48 | + , ( "z\x00E9r\x00F3", 0 ) |
| 49 | + , ( "egy", 1 ) |
| 50 | + , ( "kett\x0151", 2 ) |
| 51 | + , ( "h\x00E1rom", 3 ) |
| 52 | + , ( "n\x00E9gy", 4 ) |
| 53 | + , ( "\x00F6t", 5) |
| 54 | + , ( "hat", 6) |
| 55 | + , ( "h\x00E9t", 7) |
| 56 | + , ( "nyolc", 8) |
| 57 | + , ( "kilenc", 9) |
| 58 | + , ( "t\x00EDz", 10) |
| 59 | + ] |
| 60 | + |
| 61 | +ruleNumeral :: Rule |
| 62 | +ruleNumeral = Rule |
| 63 | + { name = "number (0..10)" |
| 64 | + , pattern = |
| 65 | + [ regex "(nulla|z\x00E9r\x00F3|egy|kett\x0151|h\x00E1rom|n\x00E9gy|\x00F6t|hat|h\x00E9t|nyolc|kilenc|t\x00EDz)" |
| 66 | + ] |
| 67 | + , prod = \tokens -> case tokens of |
| 68 | + (Token RegexMatch (GroupMatch (match:_)):_) -> |
| 69 | + HashMap.lookup (Text.toLower match) ruleNumeralMap >>= integer |
| 70 | + _ -> Nothing |
| 71 | + } |
| 72 | + |
| 73 | +elevenToNineteenMap :: HashMap Text Integer |
| 74 | +elevenToNineteenMap = HashMap.fromList |
| 75 | + [ ( "tizenegy", 11 ) |
| 76 | + , ( "tizenkett\x0151", 12 ) |
| 77 | + , ( "tizenh\x00E1rom", 13 ) |
| 78 | + , ( "tizenn\x00E9gy", 14 ) |
| 79 | + , ( "tizen\x00F6t", 15 ) |
| 80 | + , ( "tizenhat", 16 ) |
| 81 | + , ( "tizenh\x00E9t", 17 ) |
| 82 | + , ( "tizennyolc", 18 ) |
| 83 | + , ( "tizenkilenc", 19 ) |
| 84 | + ] |
| 85 | + |
| 86 | +ruleElevenToNineteen :: Rule |
| 87 | +ruleElevenToNineteen = Rule |
| 88 | + { name = "number (11..19)" |
| 89 | + , pattern = |
| 90 | + [ regex "(tizenegy|tizenkett\x0151|tizenh\x00E1rom|tizenn\x00E9gy|tizen\x00F6t|tizenhat|tizenh\x00E9t|tizennyolc|tizenkilenc)" |
| 91 | + ] |
| 92 | + , prod = \tokens -> case tokens of |
| 93 | + (Token RegexMatch (GroupMatch (match:_)):_) -> |
| 94 | + HashMap.lookup (Text.toLower match) elevenToNineteenMap >>= integer |
| 95 | + _ -> Nothing |
| 96 | + } |
| 97 | + |
| 98 | +twentyoneToTwentynineMap :: HashMap Text Integer |
| 99 | +twentyoneToTwentynineMap = HashMap.fromList |
| 100 | + [ ( "huszonegy", 21 ) |
| 101 | + , ( "huszonkett\x0151", 22 ) |
| 102 | + , ( "huszonh\x00E1rom", 23 ) |
| 103 | + , ( "huszonn\x00E9gy", 24 ) |
| 104 | + , ( "huszon\x00F6t", 25 ) |
| 105 | + , ( "huszonhat", 26 ) |
| 106 | + , ( "huszonh\x00E9t", 27 ) |
| 107 | + , ( "huszonnyolc", 28 ) |
| 108 | + , ( "huszonkilenc", 29 ) |
| 109 | + ] |
| 110 | + |
| 111 | +ruleTwentyoneToTwentynine :: Rule |
| 112 | +ruleTwentyoneToTwentynine = Rule |
| 113 | + { name = "number (21..29)" |
| 114 | + , pattern = |
| 115 | + [ regex "(huszonegy|huszonkett\x0151|huszonh\x00E1rom|huszonn\x00E9gy|huszon\x00F6t|huszonhat|huszonh\x00E9t|huszonnyolc|huszonkilenc)" |
| 116 | + ] |
| 117 | + , prod = \tokens -> case tokens of |
| 118 | + (Token RegexMatch (GroupMatch (match:_)):_) -> |
| 119 | + HashMap.lookup (Text.toLower match) twentyoneToTwentynineMap >>= integer |
| 120 | + _ -> Nothing |
| 121 | + } |
| 122 | + |
| 123 | +dozensMap :: HashMap Text Integer |
| 124 | +dozensMap = HashMap.fromList |
| 125 | + [ ( "h\x00FAsz", 20 ) |
| 126 | + , ( "harminc", 30 ) |
| 127 | + , ( "negyven", 40 ) |
| 128 | + , ( "\x00F6tven", 50 ) |
| 129 | + , ( "hatvan", 60 ) |
| 130 | + , ( "hetven", 70 ) |
| 131 | + , ( "nyolcvan", 80 ) |
| 132 | + , ( "kilencven", 90 ) |
| 133 | + ] |
| 134 | + |
| 135 | +ruleTens :: Rule |
| 136 | +ruleTens = Rule |
| 137 | + { name = "integer (20,30..90)" |
| 138 | + , pattern = |
| 139 | + [ regex "(h\x00FAsz|harminc|negyven|\x00f6tven|hatvan|hetven|nyolcvan|kilencven)" |
| 140 | + ] |
| 141 | + , prod = \tokens -> case tokens of |
| 142 | + (Token RegexMatch (GroupMatch (match:_)):_) -> |
| 143 | + HashMap.lookup (Text.toLower match) dozensMap >>= integer |
| 144 | + _ -> Nothing |
| 145 | + } |
| 146 | + |
| 147 | +ruleCompositeTens :: Rule |
| 148 | +ruleCompositeTens = Rule |
| 149 | + { name = "integer ([3-9][1-9])" |
| 150 | + , pattern = |
| 151 | + [ regex "(harminc|negyven|\x00F6tven|hatvan|hetven|nyolcvan|kilencven)(egy|kett\x0151|h\x00E1rom|n\x00E9gy|\x00F6t|hat|h\x00E9t|nyolc|kilenc)" |
| 152 | + ] |
| 153 | + , prod = \tokens -> case tokens of |
| 154 | + (Token RegexMatch (GroupMatch (m1:m2:_)):_) -> do |
| 155 | + v1 <- HashMap.lookup (Text.toLower m1) dozensMap |
| 156 | + v2 <- HashMap.lookup (Text.toLower m2) ruleNumeralMap |
| 157 | + integer $ v1 + v2 |
| 158 | + _ -> Nothing |
| 159 | + } |
| 160 | + |
| 161 | +rules :: [Rule] |
| 162 | +rules = |
| 163 | + [ ruleIntegerNumeric |
| 164 | + , ruleNumeral |
| 165 | + , ruleElevenToNineteen |
| 166 | + , ruleTwentyoneToTwentynine |
| 167 | + , ruleTens |
| 168 | + , ruleCompositeTens |
| 169 | + ] |
0 commit comments