Textonyms: Difference between revisions

Content added Content deleted
m (C code modified to use GLib I/O functions to load the dictionary)
m (→‎{{header|Haskell}}: hlint, hindent – minor tidying)
Line 821: Line 821:
=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.Maybe (isJust, isNothing, fromMaybe)
<lang haskell>import Data.Maybe (isJust, isNothing, fromMaybe)
import Data.List (sortBy, groupBy)
import Data.Char (toUpper)
import Data.Char (toUpper)
import Data.List (sortBy, groupBy)
import Data.Function (on)
import Data.Function (on)


toKey :: Char -> Maybe Char
toKey :: Char -> Maybe Char
toKey ch
toKey ch
| ch < 'A' = Nothing
| ch < 'A' = Nothing
| ch < 'D' = Just '2'
| ch < 'D' = Just '2'
| ch < 'G' = Just '3'
| ch < 'G' = Just '3'
| ch < 'J' = Just '4'
| ch < 'J' = Just '4'
| ch < 'M' = Just '5'
| ch < 'M' = Just '5'
| ch < 'P' = Just '6'
| ch < 'P' = Just '6'
| ch < 'T' = Just '7'
| ch < 'T' = Just '7'
| ch < 'W' = Just '8'
| ch < 'W' = Just '8'
| ch <= 'Z' = Just '9'
| ch <= 'Z' = Just '9'
| otherwise = Nothing
| otherwise = Nothing


toKeyString :: String -> Maybe String
toKeyString :: String -> Maybe String
toKeyString st =
toKeyString st
let mch = map (toKey.toUpper) st
| any isNothing mch = Nothing
| otherwise = Just $ map (fromMaybe '!') mch
in if any isNothing mch then Nothing
where
else Just $ map (fromMaybe '!') mch
mch = map (toKey . toUpper) st


showTextonym :: [(String,String)] -> IO ()
showTextonym :: [(String, String)] -> String
showTextonym ts = do
showTextonym ts =
let keyCode = fst $ head ts
fst (head ts) ++
" => " ++
putStrLn $ keyCode ++ " => " ++ concat [w ++ " " | (_,w) <- ts ]
concat
[ w ++ " "
| (_, w) <- ts ]


main :: IO()
main :: IO ()
main = do
main = do
let src = "unixdict.txt"
let src = "unixdict.txt"
contents <- readFile src
contents <- readFile src
let wordList = lines contents

keyedList =
let wordList = lines contents
[ (key, word)
keyedList = [(key, word) | (Just key, word) <- filter (isJust.fst) $ zip (map toKeyString wordList) wordList]
| (Just key, word) <-
groupedList = groupBy ((==) `on` fst) $ sortBy (compare `on` fst) keyedList
textonymList = filter ((>1) . length) groupedList
filter (isJust . fst) $ zip (map toKeyString wordList) wordList ]
groupedList = groupBy ((==) `on` fst) $ sortBy (compare `on` fst) keyedList

textonymList = filter ((> 1) . length) groupedList
putStrLn $ "There are " ++ show (length keyedList) ++ " words in " ++ src ++ " which can be represented by the digit key mapping."
mapM_ putStrLn $
putStrLn $ "They require " ++ show (length groupedList) ++ " digit combinations to represent them."
[ "There are " ++
putStrLn $ show (length textonymList) ++ " digit combinations represent Textonyms."
show (length keyedList) ++
putStrLn ""
putStrLn "Top 5 in ambiguity:"
" words in " ++
src ++ " which can be represented by the digit key mapping."
mapM_ showTextonym $ take 5 $ sortBy (flip compare `on` length) textonymList
putStrLn ""
, "They require " ++
show (length groupedList) ++ " digit combinations to represent them."
putStrLn "Top 5 in length:"
, show (length textonymList) ++ " digit combinations represent Textonyms."
mapM_ showTextonym $ take 5 $ sortBy (flip compare `on` (length.fst.head)) textonymList </lang>
, ""
, "Top 5 in ambiguity:"
] ++
fmap showTextonym (take 5 $ sortBy (flip compare `on` length) textonymList) ++
["", "Top 5 in length:"] ++
fmap
showTextonym
(take 5 $ sortBy (flip compare `on` (length . fst . head)) textonymList)</lang>
{{out}}
{{out}}
<pre style="font-size:80%">There are 24978 words in unixdict.txt which can be represented by the digit key mapping.
<pre style="font-size:80%">There are 24978 words in unixdict.txt which can be represented by the digit key mapping.