{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
module Duckling.PhoneNumber.Rules
( rules ) where
import qualified Data.Text as Text
import Prelude
import Data.String
import Duckling.Dimensions.Types
import Duckling.Numeral.Helpers (parseInt)
import Duckling.PhoneNumber.Types (PhoneNumberData(..))
import qualified Duckling.PhoneNumber.Types as TPhoneNumber
import Duckling.Regex.Types
import Duckling.Types
rulePhoneNumber :: Rule
rulePhoneNumber = Rule
{ name = "phone number"
, pattern =
[ regex $
"(?:\\(?\\+(\\d{1,2})\\)?[\\s-\\.]*)?" ++
"((?=[-\\d()\\s\\.]{6,16}(?:\\s*e?xt?\\.?\\s*(?:\\d{1,20}))?(?:[^\\d]+|$))(?:[\\d(]{1,20}(?:[-)\\s\\.]*\\d{1,20}){0,20}){1,20})" ++
"(?:\\s*e?xt?\\.?\\s*(\\d{1,20}))?"
]
, prod = \xs -> case xs of
(Token RegexMatch (GroupMatch (code:nums:ext:_)):_) ->
let parseNum x = toInteger <$> parseInt x
mcode = parseNum code
mext = parseNum ext
cleanup = Text.filter (not . isWhitespace)
isWhitespace x = elem x ['.', ' ', '-', '\t', '(', ')']
in Just . Token PhoneNumber $ PhoneNumberData
{ TPhoneNumber.prefix = mcode
, TPhoneNumber.number = cleanup nums
, TPhoneNumber.extension = mext
}
_ -> Nothing
}
rules :: [Rule]
rules = [ rulePhoneNumber ]