Advertisement
banovski

Word reorderer (speaking)

Aug 31st, 2024 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.84 KB | Source Code | 0 0
  1. -- The app reads a set of text lines from stdin, reorders the words in
  2. -- them and sends the result to stdout. It is intended to create
  3. -- exercises for language training purposes, where a student has to
  4. -- reorder the words to restore the original sentence. Each set of
  5. -- words is merely sorted alphabetically; if the words were in
  6. -- alphabetical order originally, the set is reversed. The app can
  7. -- handle dialogue lines i.e. ones that start with an em dash or a
  8. -- plus sign.
  9.  
  10. import Data.List (intercalate, sortOn)
  11. import Data.Char (toLower)
  12.  
  13. toValidChars :: String -> String
  14. toValidChars = filter toValidChar
  15.   where
  16.     toValidChar = flip elem validChars
  17.  
  18. -- Below is a list of characters that are not removed from the initial
  19. -- text. They are: word characters: letters (including ones with
  20. -- diacritics), the apostrophe, the right single quotation mark
  21. -- (sometimes used instead of the apostrophe), the hyphen; delimiters:
  22. -- the space and the newline; the plus sign and the em dash (used to
  23. -- introduce dialogue lines).
  24.  
  25. validChars :: String
  26. validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzóşăţ'’- +—\n"
  27.  
  28. toListOfStringsLists :: String -> [[String]]
  29. toListOfStringsLists string = map words $ lines string
  30.  
  31. enhancedSorting :: [String] -> [String]
  32. enhancedSorting list
  33.   | sorter list == list = (reverse . sorter) list
  34.   | otherwise = sorter list
  35.   where
  36.     sorter = sortOn (map toLower)
  37.  
  38. tailSorting :: [String] -> [String]
  39. tailSorting [] = []
  40. tailSorting (x:xs) = x : enhancedSorting xs
  41.  
  42. checkPlusAndEmDash :: String -> Bool
  43. checkPlusAndEmDash x = x `elem` ["+", "—"]
  44.  
  45. selectiveSorting :: [String] -> [String]
  46. selectiveSorting [] = []
  47. selectiveSorting list
  48.   | checkPlusAndEmDash listHead = tailSorting list
  49.   | otherwise = enhancedSorting list
  50.   where
  51.     listHead = head list
  52.  
  53. selectiveIntercalation :: [String] -> String
  54. selectiveIntercalation [] = []
  55. selectiveIntercalation list@(x:xs)
  56.   | checkPlusAndEmDash x = x ++ " " ++ intercalate ", " xs
  57.   | otherwise = intercalate ", " list
  58.  
  59. singleWordReplacement :: [String] -> [String]
  60. singleWordReplacement [] = []
  61. singleWordReplacement list
  62.   | checkPlusAndEmDash listHead && length list == 2 = listHead : ["…"]
  63.   | length list == 1 = ["…"]
  64.   | otherwise = list
  65.   where
  66.     listHead = head list
  67.  
  68. innerDashesRemoval :: [String] -> [String]
  69. innerDashesRemoval [] = []
  70. innerDashesRemoval (x:xs) = x : (xs >>= removeDash)
  71.   where
  72.     removeDash string
  73.       | string == "—" = []
  74.       | otherwise = [string]
  75.  
  76. replacementRemovalSorting :: [String] -> [String]
  77. replacementRemovalSorting =
  78.   innerDashesRemoval . singleWordReplacement . selectiveSorting
  79.  
  80. main :: IO ()
  81. main = do
  82.   input <- getContents
  83.   mapM_
  84.     ((putStrLn . selectiveIntercalation) . replacementRemovalSorting)
  85.     (toListOfStringsLists $ toValidChars input)
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement