module Texts.English
(
bigUp
,relativeTimes
,ellipsize
,ordSuffix
,indefinite
,isVowel
,isConsonant
)
where
import Texts.Types
import Data.Lists
import Data.Monoid
import qualified Data.Text as T
import Data.Time
import Text.Printf
bigUp :: (Integral a) => a -> SText
bigUp = go . fromIntegral where
go :: Integer -> SText
go n | n < 3 = "a couple"
| n < 5 = "several"
| n < 10 = "many"
| n < 80 = "dozens"
| n < 100 = "nearly a hundred"
| n < 200 = "some hundred"
| n < 1000 = "hundreds"
| n < 10000 = "thousands"
| n < 100000 = "tens of thousands"
| n < 200000 = "a hundred thousand"
| n < 500000 = "hundreds of thousands"
| n < 500500 = "half a million"
| n < 600000 = "over half a million"
| n < 1000000 = "nearly a million"
| n < 2000000 = "over a million"
| n < (10^ (9::Int)) = "millions"
| n < 10^ (12::Int) = "billions"
| otherwise = "trillions"
relativeTimes :: UTCTime
-> UTCTime
-> Bool
-> SText
relativeTimes t1 t2 fix = T.pack $ maybe "unknown" format $ find (\(s,_,_) -> abs span'>=s) $ reverse ranges where
minute = 60; hour = minute * 60; day = hour * 24;
week = day * 7; month = day * 30; year = month * 12
format range =
(if fix && span'>0 then "in " else "")
++ case range of
(_,str,0) -> str
(_,str,base) -> printf str (abs $ round (span' / base) :: Integer)
++ (if fix && span'<0 then " ago" else "")
span' = t1 `diffUTCTime` t2
ranges = [(0,"%d seconds",1)
,(minute,"a minute",0)
,(minute*2,"%d minutes",minute)
,(minute*30,"half an hour",0)
,(minute*31,"%d minutes",minute)
,(hour,"an hour",0)
,(hour*2,"%d hours",hour)
,(hour*3,"a few hours",0)
,(hour*4,"%d hours",hour)
,(day,"a day",0)
,(day*2,"%d days",day)
,(week,"a week",0)
,(week*2,"%d weeks",week)
,(month,"a month",0)
,(month*2,"%d months",month)
,(year,"a year",0)
,(year*2,"%d years",year)
]
ellipsize :: Int -> SText -> SText
ellipsize n xs
| T.length xs > n = T.take n $ T.take (max 1 (n1)) xs <> "…"
| otherwise = xs
ordSuffix :: Integral n => n -> SText
ordSuffix n
| tens > 3 && tens < 21 = "th"
| otherwise = case n `mod` 10 of
1 -> "st"; 2 -> "nd"; 3 -> "rd"; _ -> "th"
where tens = n `mod` 100
indefinite :: SText -> SText
indefinite word
| T.all isVowel (T.take 1 word) = "an"
| otherwise = "a"
isVowel :: Char -> Bool
isVowel = flip elem "aeiou"
isConsonant :: Char -> Bool
isConsonant = flip elem "bcsfghjklmnpqrstvwxyz"