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.Char (toUpper) |
import Data.Char (toUpper) |
||
⚫ | |||
import Data.Function (on) |
import Data.Function (on) |
||
toKey :: Char -> Maybe Char |
toKey :: Char -> Maybe Char |
||
toKey ch |
toKey ch |
||
| ch < 'A' = Nothing |
|||
| ch < 'D' = Just '2' |
|||
| ch < 'G' = Just '3' |
|||
| ch < 'J' = Just '4' |
|||
| ch < 'M' = Just '5' |
|||
| ch < 'P' = Just '6' |
|||
| ch < 'T' = Just '7' |
|||
| ch < 'W' = Just '8' |
|||
| ch <= 'Z' = Just '9' |
|||
| otherwise = Nothing |
|||
toKeyString :: String -> Maybe String |
toKeyString :: String -> Maybe String |
||
toKeyString st |
toKeyString st |
||
| any isNothing mch = Nothing |
|||
⚫ | |||
in if any isNothing mch then Nothing |
|||
where |
|||
⚫ | |||
mch = map (toKey . toUpper) st |
|||
showTextonym :: [(String,String)] -> |
showTextonym :: [(String, String)] -> String |
||
showTextonym ts = |
showTextonym ts = |
||
fst (head ts) ++ |
|||
" => " ++ |
|||
putStrLn $ keyCode ++ " => " ++ concat [w ++ " " | (_,w) <- ts ] |
|||
concat |
|||
⚫ | |||
| (_, w) <- ts ] |
|||
main :: IO() |
main :: IO () |
||
main = do |
main = do |
||
let src = "unixdict.txt" |
|||
contents <- readFile src |
|||
⚫ | |||
keyedList = |
|||
⚫ | |||
[ (key, word) |
|||
keyedList = [(key, word) | (Just key, word) <- filter (isJust.fst) $ zip (map toKeyString wordList) wordList] |
|||
| (Just key, word) <- |
|||
⚫ | |||
filter (isJust . fst) $ zip (map toKeyString wordList) wordList ] |
|||
⚫ | |||
textonymList = filter ((> 1) . length) groupedList |
|||
⚫ | |||
mapM_ putStrLn $ |
|||
⚫ | |||
[ "There are " ++ |
|||
⚫ | |||
show (length keyedList) ++ |
|||
⚫ | |||
" words in " ++ |
|||
⚫ | |||
⚫ | |||
, "They require " ++ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
, "" |
|||
, "Top 5 in ambiguity:" |
|||
] ++ |
|||
⚫ | |||
⚫ | |||
fmap |
|||
showTextonym |
|||
⚫ | |||
{{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. |