banovski

Word combinations splitter GTK

Sep 15th, 2024 (edited)
1,509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.83 KB | Source Code | 0 0
  1. -- The app reads a column of text from stdin. Some lines in the column
  2. -- may contain single words, some may contain word combinations. The
  3. -- app splits word combinations into separate words or leaves them as
  4. -- they are. It performs splitting either automatically or
  5. -- interactively, depending on the mode selected by the user with the
  6. -- help of a dialog window displayed at the launch.
  7.  
  8. import Graphics.UI.Gtk
  9. import Control.Monad (void)
  10. import System.Exit (die)
  11.  
  12. main :: IO ()
  13. main = do
  14.   input <- getContents
  15.   initialDialogResponse <- runDialog "Choose a mode: " "Split all" "Check all"
  16.   case initialDialogResponse of
  17.     0 ->
  18.       mapM_ (putStrLn . underscoresToSpaces . spacesToNewlines) $
  19.       lines $ spaceBeforeBracket input
  20.     1 -> mapM_ processItem $ lines $ spaceBeforeBracket input
  21.     2 -> void (die "Aborted!")
  22.     _ -> return ()
  23.  
  24. spaceBeforeBracket :: String -> String
  25. spaceBeforeBracket [] = []
  26. spaceBeforeBracket [x] = [x]
  27. spaceBeforeBracket (x:y:ys)
  28.   | x == ' ' && y == '(' = '_' : '(' : spaceBeforeBracket ys
  29.   | otherwise = x : spaceBeforeBracket (y : ys)
  30.  
  31. runDialog :: String -> String -> String -> IO Int
  32. runDialog dialogMessage buttonOne buttonTwo = do
  33.   _ <- initGUI
  34.   dialog <- dialogNew
  35.   set
  36.     dialog
  37.     [ windowTitle := "Word combinations splitter"
  38.     , windowDefaultWidth := 300
  39.     , containerBorderWidth := 8
  40.     ]
  41.  
  42.   label <- labelNew (Nothing :: Maybe String)
  43.   labelSetMarkup label dialogMessage
  44.   miscSetAlignment label 0.05 0
  45.   upperPart <- dialogGetUpper dialog
  46.   set upperPart [containerChild := label]
  47.  
  48.   button1 <- dialogAddButton dialog buttonOne (ResponseUser 0)
  49.   button2 <- dialogAddButton dialog buttonTwo (ResponseUser 1)
  50.   button3 <- dialogAddButton dialog "Abort" (ResponseUser 2)
  51.  
  52.   widgetShowAll dialog
  53.  
  54.   dialogExitStatus <- dialogRun dialog
  55.   _ <- onClicked button1 mainQuit
  56.   _ <- onClicked button2 mainQuit
  57.   _ <- onClicked button3 mainQuit
  58.   return $ (\(ResponseUser x) -> x) dialogExitStatus
  59.  
  60. spacesToNewlines :: String -> String
  61. spacesToNewlines = map spaceToNewline
  62.   where
  63.     spaceToNewline x
  64.       | x `elem` " \t" = '\n'
  65.       | otherwise = x
  66.  
  67. underscoresToSpaces :: String -> String
  68. underscoresToSpaces = map underscoreToSpace
  69.   where
  70.     underscoreToSpace x
  71.       | x == '_' = ' '
  72.       | otherwise = x
  73.  
  74. processItem :: String -> IO ()
  75. processItem [] = return ()
  76. processItem string
  77.   | ' ' `elem` string || '\t' `elem` string = do
  78.     processedDialogResponse <-
  79.       runDialog
  80.         ("Check: <b>" ++ underscoresToSpaces string ++ "</b>")
  81.         "Split"
  82.         "Skip"
  83.     case processedDialogResponse of
  84.       0 -> putStrLn $ underscoresToSpaces $ spacesToNewlines string
  85.       1 -> putStrLn $ underscoresToSpaces string
  86.       2 -> void (die "Aborted!")
  87.       _ -> return ()
  88.   | otherwise = putStrLn $ underscoresToSpaces string
  89.  
Advertisement
Add Comment
Please, Sign In to add comment