module NLP.Minimorph.English where
import Data.Text ( Text )
import qualified Data.Text as T
import NLP.Minimorph.Util
commas :: Text -> [Text] -> Text
commas _ [] = ""
commas _ [x] = x
commas et xs = T.intercalate ", " (init xs) <+> et <+> last xs
cardinal :: Int -> Text
cardinal n = case n of
1 -> "one"
2 -> "two"
3 -> "three"
_ -> T.pack (show n)
ordinal :: Int -> Text
ordinal n = case n of
1 -> "first"
2 -> "second"
3 -> "third"
n | n < 21 -> n `suf` "th"
| n `rem` 10 == 2 -> n `suf` "nd"
| n `rem` 10 == 3 -> n `suf` "rd"
| otherwise -> n `suf` "th"
where
n `suf` s = T.pack (show n) <> s
defaultNounPlural :: Text -> Text
defaultNounPlural x
| "is" `T.isSuffixOf` x = thesis
| hasSibilantSuffix x = es
| hasCySuffix x = y_ies
| "f" `T.isSuffixOf` x = f_ves
| otherwise = plain
where
plain = x <> "s"
es = x <> "es"
y_ies = T.init x <> "ies"
f_ves = T.init x <> "ves"
thesis = tDropEnd 2 x <> "es"
defaultVerbStuff :: Text -> (Text, Text)
defaultVerbStuff v
| hasSibilantSuffix v = sibilant_o v
| "o" `T.isSuffixOf` v = sibilant_o v
| "e" `T.isSuffixOf` v = e_final v
| hasCySuffix v = y_final v
| otherwise = plain v
where
plain x = (x <> "s" , x <> "ed")
sibilant_o x = (x <> "es" , x <> "ed")
e_final x = (x <> "s" , x <> "d")
y_final x = (T.init x <> "ies", T.init x <> "ied")
indefiniteDet :: Text -> Text
indefiniteDet (T.toLower -> t) =
if useAn then "an" else "a"
where
useAn = t `elem` [ "11", "11th" ] || useAn'
useAn' = case T.uncons t of
Just ('8',_) -> True
Just (h,_) -> isVowel h `butNot` hasSemivowelPrefix t
Nothing -> False
x `butNot` y = x && not y
hasSibilantSuffix :: Text -> Bool
hasSibilantSuffix x = any (`T.isSuffixOf` x) ["x","s","ch","sh"]
hasSemivowelPrefix :: Text -> Bool
hasSemivowelPrefix ls = any (`T.isPrefixOf` ls) ["y","w","eu","ewe"]
hasCySuffix :: Text -> Bool
hasCySuffix (T.unpack . tTakeEnd 2 -> [x, 'y']) = isConsonant x
hasCySuffix _ = False
isVowel :: Char -> Bool
isVowel = (`elem` "aeiouAEIOU")
isConsonant :: Char -> Bool
isConsonant = not . isVowel