Advertisement
banovski

The “weightiest” word in a line

Aug 29th, 2024 (edited)
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.26 KB | Source Code | 0 0
  1. -- The utility reads a set of text lines from stdin, selects the
  2. -- "weightiest" word in each of them and sends it to stdout. The
  3. -- weight of a word is a sum of weights of its letters; the weight of
  4. -- each letter roughly corresponds to its average occurence rate. If
  5. -- applied to this text, the utility outputs the following:
  6.  
  7. -- utility
  8. -- weightiest
  9. -- weights
  10. -- corresponds
  11. -- following
  12.  
  13. import Data.Char (ord)
  14.  
  15. weightsList :: [Int]
  16. weightsList =
  17.   [ 3  -- a
  18.   , 20 -- b
  19.   , 12 -- c
  20.   , 9  -- d
  21.   , 1  -- e
  22.   , 16 -- f
  23.   , 18 -- g
  24.   , 8  -- h
  25.   , 5  -- i
  26.   , 24 -- j
  27.   , 22 -- k
  28.   , 11 -- l
  29.   , 14 -- m
  30.   , 6  -- n
  31.   , 4  -- o
  32.   , 19 -- p
  33.   , 25 -- q
  34.   , 10 -- r
  35.   , 7  -- s
  36.   , 2  -- t
  37.   , 13 -- u
  38.   , 21 -- v
  39.   , 15 -- w
  40.   , 23 -- x
  41.   , 17 -- y
  42.   , 26 -- z
  43.   ]
  44.  
  45. validCharList :: String
  46. validCharList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-'… \n"
  47.  
  48. charToWeight :: Char -> Int
  49. charToWeight char
  50.   | charCode > 96 && charCode < 123 = pickCharWeight forLowerCase
  51.   | charCode > 64 && charCode < 91 = pickCharWeight forUpperCase
  52.   | otherwise = 0
  53.   where
  54.     charCode = ord char
  55.     pickCharWeight offset = weightsList !! (charCode - offset)
  56.     forLowerCase = 97
  57.     forUpperCase = 65
  58.  
  59. wordToWeight :: String -> Int
  60. wordToWeight "" = 0
  61. wordToWeight word = sum $ map charToWeight word
  62.  
  63. weightierWord :: String -> String -> String
  64. weightierWord "" string = string
  65. weightierWord string "" = string
  66. weightierWord string1 string2
  67.   | wordToWeight string1 > wordToWeight string2 = string1
  68.   | otherwise = string2
  69.  
  70. weightiestWordInList :: [String] -> String
  71. weightiestWordInList [] = ""
  72. weightiestWordInList list = foldl1 weightierWord list
  73.  
  74. uniqueAdjacentSpaces :: String -> String
  75. uniqueAdjacentSpaces [] = []
  76. uniqueAdjacentSpaces [char] = [char]
  77. uniqueAdjacentSpaces (char1:char2:chars)
  78.   | char1 == ' ' && char2 == ' ' = uniqueAdjacentSpaces (char2 : chars)
  79.   | otherwise = char1 : uniqueAdjacentSpaces (char2 : chars)
  80.  
  81. filterChars :: String -> String
  82. filterChars = filter filterFunction
  83.   where
  84.     filterFunction = flip elem validCharList
  85.  
  86. main :: IO ()
  87. main = do
  88.   input <- getContents
  89.   mapM_
  90.     ((putStrLn . weightiestWordInList) . words)
  91.     (lines $ (uniqueAdjacentSpaces . filterChars) input)
  92.  
Tags: weight words text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement