|
| 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 | + |
| 12 | +module Duckling.Duration.TR.Rules |
| 13 | + ( rules |
| 14 | + ) where |
| 15 | + |
| 16 | +import Control.Monad (join) |
| 17 | +import Data.String |
| 18 | +import Prelude |
| 19 | +import qualified Data.Text as Text |
| 20 | + |
| 21 | +import Duckling.Dimensions.Types |
| 22 | +import Duckling.Duration.Helpers |
| 23 | +import Duckling.Numeral.Helpers (parseInt) |
| 24 | +import Duckling.Numeral.Types (NumeralData (..)) |
| 25 | +import Duckling.Regex.Types |
| 26 | +import Duckling.Types |
| 27 | +import qualified Duckling.Numeral.Types as TNumeral |
| 28 | +import qualified Duckling.TimeGrain.Types as TG |
| 29 | + |
| 30 | +ruleDurationQuarterOfAnHour :: Rule |
| 31 | +ruleDurationQuarterOfAnHour = Rule |
| 32 | + { name = "quarter of an hour" |
| 33 | + , pattern = [ regex "(1/4\\s?sa(at)?|\x00e7eyrek saat)" ] |
| 34 | + , prod = \_ -> Just . Token Duration $ duration TG.Minute 15 |
| 35 | + } |
| 36 | + |
| 37 | +ruleDurationHalfAnHour :: Rule |
| 38 | +ruleDurationHalfAnHour = Rule |
| 39 | + { name = "half an hour" |
| 40 | + , pattern = [regex "(1/2\\s?sa(at)?|yar\305m saat)"] |
| 41 | + , prod = \_ -> Just . Token Duration $ duration TG.Minute 30 |
| 42 | + } |
| 43 | + |
| 44 | +ruleDurationThreeQuartersOfAnHour :: Rule |
| 45 | +ruleDurationThreeQuartersOfAnHour = Rule |
| 46 | + { name = "three-quarters of an hour" |
| 47 | + , pattern = [regex "(3/4\\s?sa(at)?|\x00fc\x00e7e \231eyrek sa(at)?)"] |
| 48 | + , prod = \_ -> Just . Token Duration $ duration TG.Minute 45 |
| 49 | + } |
| 50 | + |
| 51 | +ruleDurationFortnight :: Rule |
| 52 | +ruleDurationFortnight = Rule |
| 53 | + { name = "fortnight" |
| 54 | + , pattern = [regex "iki hafta"] |
| 55 | + , prod = \_ -> Just . Token Duration $ duration TG.Day 14 |
| 56 | + } |
| 57 | + |
| 58 | +ruleNumeralQuotes :: Rule |
| 59 | +ruleNumeralQuotes = Rule |
| 60 | + { name = "<integer> + '\"" |
| 61 | + , pattern = |
| 62 | + [ Predicate isNatural |
| 63 | + , regex "(['\"])" |
| 64 | + ] |
| 65 | + , prod = \tokens -> case tokens of |
| 66 | + (Token Numeral (NumeralData {TNumeral.value = v}): |
| 67 | + Token RegexMatch (GroupMatch (x:_)): |
| 68 | + _) -> case x of |
| 69 | + "'" -> Just . Token Duration . duration TG.Minute $ floor v |
| 70 | + "\"" -> Just . Token Duration . duration TG.Second $ floor v |
| 71 | + _ -> Nothing |
| 72 | + _ -> Nothing |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +ruleDurationDotNumeralHours :: Rule |
| 77 | +ruleDurationDotNumeralHours = Rule |
| 78 | + { name = "number.number hours" |
| 79 | + , pattern = [regex "(\\d+)(\\.|,)(\\d+) *sa(at)?"] |
| 80 | + , prod = \tokens -> case tokens of |
| 81 | + (Token RegexMatch (GroupMatch (h:d:_)):_) -> do |
| 82 | + hh <- parseInt h |
| 83 | + dec <- parseInt d |
| 84 | + let divisor = floor $ (fromIntegral (10 :: Integer) :: Float) ** |
| 85 | + fromIntegral (Text.length d - 1) |
| 86 | + numerator = fromIntegral $ 6 * dec |
| 87 | + Just . Token Duration . duration TG.Minute $ |
| 88 | + 60 * hh + quot numerator divisor |
| 89 | + _ -> Nothing |
| 90 | + } |
| 91 | + |
| 92 | +ruleDurationAndHalfHour :: Rule |
| 93 | +ruleDurationAndHalfHour = Rule |
| 94 | + { name = "<integer> and an half hour" |
| 95 | + , pattern = |
| 96 | + [ Predicate isNatural |
| 97 | + , regex "bu\x00e7euk sa(at)?" |
| 98 | + ] |
| 99 | + , prod = \tokens -> case tokens of |
| 100 | + (Token Numeral (NumeralData {TNumeral.value = v}):_) -> |
| 101 | + Just . Token Duration . duration TG.Minute $ 30 + 60 * floor v |
| 102 | + _ -> Nothing |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | +ruleDurationPrecision :: Rule |
| 107 | +ruleDurationPrecision = Rule |
| 108 | + { name = "<duration> about|exactly" |
| 109 | + , pattern = |
| 110 | + [ regex "(gibi|civar\305nda|yakla\x015f\305k|tam( olarak)?)" |
| 111 | + , dimension Duration |
| 112 | + ] |
| 113 | + , prod = \tokens -> case tokens of |
| 114 | + (_:token:_) -> Just token |
| 115 | + _ -> Nothing |
| 116 | + } |
| 117 | + |
| 118 | +rules :: [Rule] |
| 119 | +rules = |
| 120 | + [ ruleDurationQuarterOfAnHour |
| 121 | + , ruleDurationHalfAnHour |
| 122 | + , ruleDurationThreeQuartersOfAnHour |
| 123 | + , ruleDurationFortnight |
| 124 | + , ruleDurationDotNumeralHours |
| 125 | + , ruleDurationAndHalfHour |
| 126 | + , ruleDurationPrecision |
| 127 | + , ruleNumeralQuotes |
| 128 | + ] |
0 commit comments