Textonyms: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Simpler with flipped tuples.) |
|||
Line 909: | Line 909: | ||
digitEncoded :: [String] -> [(String, String)] |
digitEncoded :: [String] -> [(String, String)] |
||
digitEncoded = mapMaybe |
digitEncoded = mapMaybe ((>>=) . traverse toKey <*> curry Just) |
||
where |
|||
⚫ | |||
codeGrouped :: [(String, String)] -> [[(String, String)]] |
|||
codeGrouped = groupBy (on (==) snd) . sortOn snd |
|||
textonymSubset :: [[(String, String)]] -> [[(String, String)]] |
|||
textonymSubset = filter ((1 <) . length) |
|||
toKey :: Char -> Maybe Char |
|||
⚫ | |||
charDict :: M.Map Char Char |
charDict :: M.Map Char Char |
||
Line 931: | Line 924: | ||
showTextonym :: Int -> [(String, String)] -> String |
showTextonym :: Int -> [(String, String)] -> String |
||
showTextonym w ts = |
showTextonym w ts = |
||
concat [ |
concat [rjust w ' ' (snd (head ts)), " -> ", unwords $ fmap fst ts] |
||
where |
|||
⚫ | |||
justifyRight :: Int -> Char -> String -> String |
|||
⚫ | |||
firstColWidth :: [[(String, String)]] -> Int |
|||
firstColWidth = maximum . map (length . snd . head) |
|||
--------------------------- TEST --------------------------- |
|||
main :: IO () |
main :: IO () |
||
main = do |
main = do |
||
Line 944: | Line 934: | ||
s <- readFile fp |
s <- readFile fp |
||
let encodings = digitEncoded $ lines s |
let encodings = digitEncoded $ lines s |
||
codeGroups = |
codeGroups = groupBy (on (==) snd) . sortOn snd $ encodings |
||
textonyms = |
textonyms = filter ((1 <) . length) codeGroups |
||
ambiguous = take 5 $ sortBy (flip (comparing length)) textonyms |
ambiguous = take 5 $ sortBy (flip (comparing length)) textonyms |
||
longer = take 5 $ sortBy (flip (comparing (length . snd . head))) textonyms |
longer = take 5 $ sortBy (flip (comparing (length . snd . head))) textonyms |
||
[wa, wl] = |
[wa, wl] = (maximum . map (length . snd . head)) <$> [ambiguous, longer] |
||
mapM_ putStrLn $ |
mapM_ putStrLn $ |
||
[ "There are " ++ |
[ "There are " ++ |