module Text.Sonnex (sonnex, sonnexPhrase) where
import Data.Char
isVowel :: Char -> Bool
isVowel = (`elem` "aâàäeéèêëiîïoôöuùûüyœ")
isConson :: Char -> Bool
isConson = (`elem` "bcçdfghjklmnpqrstvwxyz")
sonx :: String -> String -> String
sonx "" s = s
sonx ('\'':cs) s = sonx cs s
sonx "a" s = s ++ "a"
sonx "aient" s = s ++ "é"
sonx "ain" s = s ++ "1"
sonx ('a':'i':'n':v:cs) s
| isVowel v = sonx (v:cs) (s ++ "é")
| otherwise = sonx (v:cs) (s ++ "1")
sonx "ais" s = s ++ "é"
sonx ('a':'i':'s':v:cs) s
| isVowel v = sonx (v:cs) (s ++ "éz")
| otherwise = sonx (v:cs) (s ++ "és")
sonx "ail" s = s ++ "ai"
sonx ('a':'i':'l':'l':cs) s = sonx cs (s ++ "ai")
sonx ('a':'i':cs) s = sonx cs (s ++ "é")
sonx ('a':'m':'m':cs) s = sonx cs (s ++ "am")
sonx ('a':'m':c:cs) s
| isVowel c = sonx (c:cs) (s ++ "am")
| otherwise = sonx (c:cs) (s ++ "2")
sonx "an" s = s ++ "2"
sonx ('a':'n':'n':cs) s = sonx cs (s ++ "an")
sonx ('a':'n':c:cs) s
| isVowel c = sonx (c:cs) (s ++ "an")
| otherwise = sonx (c:cs) (s ++ "2")
sonx ('a':'s':'s':cs) s = sonx cs (s ++ "as")
sonx ('a':'s':c:cs) s
| isConson c = sonx (c:cs) (s ++ "as")
| otherwise = sonx (c:cs) (s ++ "az")
sonx ('a':'u':cs) s = sonx cs (s ++ "o")
sonx "ay" s = s ++ "é"
sonx "ays" s = s ++ "é"
sonx ('à':cs) s = sonx cs (s ++ "a")
sonx ('â':cs) s = sonx cs (s ++ "a")
sonx "b" s = s
sonx ('b':'b':cs) s = sonx cs (s ++ "b")
sonx "c" s = s
sonx ('c':'a':cs) s = sonx ('a':cs) (s ++ "k")
sonx ('c':'c':cs) s = sonx cs (s ++ "ks")
sonx ('c':'e':cs) s = sonx ('e':cs) (s ++ "s")
sonx ('c':'\'':cs) s = sonx cs (s ++ "s")
sonx ('c':'h':'a':'o':cs) s = sonx ('a':'o':cs) (s ++ "k")
sonx ('c':'h':'l':cs) s = sonx cs (s ++ "kl")
sonx ('c':'h':'o':'e':cs) s = sonx ('o':'e':cs) (s ++ "k")
sonx ('c':'h':'œ':cs) s = sonx ('o':'e':cs) (s ++ "k")
sonx ('c':'h':'r':cs) s = sonx cs (s ++ "kr")
sonx ('c':'h':cs) s = sonx cs (s ++ "C")
sonx ('c':'i':cs) s = sonx ('i':cs) (s ++ "s")
sonx ('c':'k':cs) s = sonx cs (s ++ "k")
sonx ('c':'o':'e':'u':cs) s = sonx ('o':'e':'u':cs) (s ++ "k")
sonx ('c':'o':'m':'p':'t':cs) s = sonx cs (s ++ "k3t")
sonx ('c':'œ':'u':cs) s = sonx ('œ':'u':cs) (s ++ "k")
sonx ('c':'o':cs) s = sonx ('o':cs) (s ++ "k")
sonx ('c':'u':'e':'i':cs) s = sonx ('i':cs) (s ++ "ke")
sonx ('c':'u':cs) s = sonx ('u':cs) (s ++ "k")
sonx ('c':'y':cs) s = sonx ('y':cs) (s ++ "s")
sonx ('c':cs) s = sonx cs (s ++ "k")
sonx ('ç':cs) s = sonx cs (s ++ "s")
sonx "d" s = s
sonx "ds" s = s
sonx ('d':'d':cs) s = sonx cs (s ++ "d")
sonx "e" s = s
sonx "ec" s = s ++ "éc"
sonx "ef" s = s ++ "éf"
sonx "eaux" s = s ++ "o"
sonx ('e':'a':'n':'n':cs) s = sonx cs (s ++ "an")
sonx ('e':'a':'n':cs) s = sonx cs (s ++ "2")
sonx ('e':'a':'u':cs) s = sonx cs (s ++ "o")
sonx ('e':'f':'f':cs) s = sonx cs (s ++ "éf")
sonx "ein" s = s ++ "1"
sonx ('e':'i':'n':c:cs) s
| isVowel c = sonx (c:cs) (s ++ "én")
| otherwise = sonx (c:cs) (s ++ "1")
sonx ('e':'i':cs) s = sonx cs (s ++ "é")
sonx ('e':'l':'l':cs) s = sonx cs (s ++ "él")
sonx ('e':'l':c:cs) s
| isConson c = sonx ('l':c:cs) (s ++ "é")
| otherwise = sonx ('l':c:cs) (s ++ "e")
sonx ('e':'m':'m':cs) s = sonx cs (s ++ "ém")
sonx ('e':'m':'p':cs) s = sonx cs (s ++ "2")
sonx ('e':'n':'n':cs) s = sonx cs (s ++ "én")
sonx "en" s = s ++ "2"
sonx ('e':'n':c:cs) s
| isVowel c = sonx (c:cs) (s ++ "en")
| otherwise = sonx (c:cs) (s ++ "2")
sonx "er" s = s ++ "é"
sonx "ert" s = s ++ "ér"
sonx ('e':'r':'r':cs) s = sonx cs (s ++ "ér")
sonx ('e':'r':'f':cs) s = sonx ('f':cs) (s ++ "ér")
sonx "es" s = s
sonx ('e':'s':'c':'h':cs) s = sonx cs (s ++ "éC")
sonx ('e':'s':'h':cs) s = sonx ('h':cs) (s ++ "é")
sonx ('e':'s':'n':cs) s = sonx ('n':cs) (s ++ "é")
sonx ('e':'s':'s':cs) s = sonx cs (s ++ "és")
sonx ('e':'s':c:cs) s
| isConson c = sonx (c:cs) (s ++ "és")
| otherwise = sonx (c:cs) (s ++ "ez")
sonx ('é':'s':c:cs) s
| isConson c = sonx cs (s ++ "és")
| otherwise = sonx (c:cs) (s ++ "éz")
sonx ('e':'t':'t':cs) s = sonx cs (s ++ "ét")
sonx "et" s = s ++ "é"
sonx ('e':'t':cs) s = sonx cs (s ++ "et")
sonx ('e':'u':'n':c:cs) s
| isVowel c = sonx (c:cs) (s ++ "en")
| otherwise = sonx (c:cs) (s ++ "1")
sonx "eux" s = s ++ "e"
sonx ('e':'u':'x':'i':cs) s = sonx ('i':cs) (s ++ "ez")
sonx ('e':'u':cs) s = sonx cs (s ++ "e")
sonx "ex" s = s ++ "éks"
sonx ('e':'y':c:cs) s
| isConson c = sonx (c:cs) (s ++ "é")
| otherwise = sonx ('y':c:cs) (s ++ "é")
sonx "ez" s = s ++ "é"
sonx ('è':cs) s = sonx cs (s ++ "é")
sonx ('ê':cs) s = sonx cs (s ++ "é")
sonx ('ë':'l':cs) s = sonx ('l':cs) (s ++ "é")
sonx ('f':'f':cs) s = sonx cs (s ++ "f")
sonx "g" s = s
sonx ('g':'e':cs) s = sonx ('e':cs) (s ++ "j")
sonx ('g':'é':cs) s = sonx ('é':cs) (s ++ "j")
sonx ('g':'i':cs) s = sonx ('i':cs) (s ++ "j")
sonx ('g':'n':cs) s = sonx cs (s ++ "n")
sonx ('g':'y':cs) s = sonx ('y':cs) (s ++ "j")
sonx ('g':'u':'ë':cs) s = sonx cs (s ++ "gu")
sonx ('g':'u':cs) s = sonx cs (s ++ "g")
sonx ('g':'g':cs) s = sonx cs (s ++ "g")
sonx ('h':cs) s = sonx cs s
sonx "ic" s = s ++ "ik"
sonx "ics" s = s ++ "ik"
sonx ('i':'e':'n':'n':cs) s = sonx cs (s ++ "ién")
sonx ('i':'e':'n':cs) s = sonx cs (s ++ "i1")
sonx ('i':'n':'n':cs) s = sonx cs (s ++ "in")
sonx ('i':'n':'e':cs) s = sonx cs (s ++ "in")
sonx "in" s = s ++ "1"
sonx ('i':'n':c:cs) s
| isVowel c = sonx (c:cs) (s ++ "in")
| otherwise = sonx (c:cs) (s ++ "1")
sonx ('i':'s':c:cs) s
| isConson c = sonx (c:cs) (s ++ "is")
| otherwise = sonx (c:cs) (s ++ "iz")
sonx ('i':'x':'i':cs) s = sonx ('i':cs) (s ++ "iz")
sonx ('i':'l':'l':cs) s = sonx cs (s ++ "i")
sonx ('i':cs) s = sonx cs (s ++ "i")
sonx ('ï':cs) s = sonx cs (s ++ "i")
sonx ('l':'l':cs) s = sonx cs (s ++ "l")
sonx ('m':'m':cs) s = sonx cs (s ++ "m")
sonx ('n':'n':cs) s = sonx cs (s ++ "n")
sonx ('o':'c':'c':cs) s = sonx cs (s ++ "ok")
sonx ('o':'e':'u':cs) s = sonx cs (s ++ "e")
sonx ('œ':'u':cs) s = sonx cs (s ++ "e")
sonx ('œ':cs) s = sonx cs (s ++ "e")
sonx "oient" s = s ++ "Ua"
sonx ('o':'i':'n':cs) s = sonx cs (s ++ "U1")
sonx ('o':'i':cs) s = sonx cs (s ++ "Ua")
sonx ('o':'m':'m':cs) s = sonx cs (s ++ "om")
sonx ('o':'m':c:cs) s
| isVowel c = sonx (c:cs) (s ++ "om")
| otherwise = sonx (c:cs) (s ++ "3")
sonx ('o':'n':'n':cs) s = sonx cs (s ++ "on")
sonx ('o':'n':cs) s = sonx cs (s ++ "3")
sonx ('o':'s':c:cs) s
| isConson c = sonx cs (s ++ "os")
| otherwise = sonx (c:cs) (s ++ "oz")
sonx ('o':'u':cs) s = sonx cs (s ++ "U")
sonx ('o':'ù':cs) s = sonx cs (s ++ "U")
sonx ('o':'û':cs) s = sonx cs (s ++ "U")
sonx ('ô':cs) s = sonx cs (s ++ "o")
sonx ('ö':cs) s = sonx cs (s ++ "o")
sonx "p" s = s
sonx ('p':'h':cs) s = sonx cs (s ++ "f")
sonx ('p':'p':cs) s = sonx cs (s ++ "p")
sonx ('p':'a':'y':'s':cs) s = sonx ('i':'s':cs) (s ++ "pé")
sonx ('q':'u':'r':cs) s = sonx ('r':cs) (s ++ "ku")
sonx ('q':'u':cs) s = sonx cs (s ++ "k")
sonx ('q':cs) s = sonx cs (s ++ "k")
sonx ('r':'r':cs) s = sonx cs (s ++ "r")
sonx "s" s = s
sonx ('s':'s':cs) s = sonx cs (s ++ "s")
sonx ('s':'c':'i':cs) s = sonx ('i':cs) (s ++ "s")
sonx "t" s = s
sonx ('t':'t':cs) s = sonx cs (s ++ "t")
sonx "un" s = s ++ "1"
sonx ('û':cs) s = sonx cs (s ++ "u")
sonx ('w':cs) s = sonx cs (s ++ "v")
sonx "x" s = s
sonx ('x':'c':cs) s = sonx cs (s ++ "ks")
sonx ('x':c:cs) s
| isVowel c = sonx (c:cs) (s ++ "kz")
| otherwise = sonx (c:cs) (s ++ "ks")
sonx ('y':cs) s = sonx cs (s ++ "i")
sonx ('z':'z':cs) s = sonx cs (s ++ "z")
sonx (c:cs) s = sonx cs (s ++ [c])
sonnex :: String -> String
sonnex word = sonx (map toLower word) ""
sonnexPhrase :: String -> String
sonnexPhrase phrase = unwords $ map sonnex (words phrase)