module Duckling.PhoneNumber.PT.Rules
( rules ) where
import Data.String
import Prelude
import qualified Data.Text as Text
import Duckling.Dimensions.Types
import Duckling.Numeral.Helpers (parseInt)
import Duckling.PhoneNumber.Types (PhoneNumberData(..))
import Duckling.Regex.Types
import Duckling.Types
import qualified Duckling.PhoneNumber.Types as TPhoneNumber
rulePhoneNumber :: Rule
rulePhoneNumber = Rule
{ name = "phone number"
, pattern =
[ regex $
"(?:\\(?\\+(\\d{1,2})\\)?[\\s-\\.]*)?" ++
"((?=[-\\d()\\s\\.]{6,16}(?:\\s*ramal\\.?\\s*(?:\\d{1,20}))?(?:[^\\d]+|$))(?:[\\d(]{1,20}(?:[-)\\s\\.]*\\d{1,20}){0,20}){1,20})" ++
"(?:\\s*ramal\\.?\\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
]