Advertisement
banovski

Word combinations splitter GTK

Sep 15th, 2024 (edited)
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.52 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 -> mapM_ (putStrLn . spacesToNewlines) $ lines $ spaceBeforeBracket input
  18.     1 -> mapM_ processItem $ lines $ spaceBeforeBracket input
  19.     2 -> void (die "Aborted!")
  20.     _ -> return ()
  21.  
  22. spaceBeforeBracket :: String -> String
  23. spaceBeforeBracket [] = []
  24. spaceBeforeBracket [x] = [x]
  25. spaceBeforeBracket (x:y:ys)
  26.   | x == ' ' && y == '(' = ' ' : '(' : spaceBeforeBracket ys
  27.   | otherwise = x : y : spaceBeforeBracket ys
  28.  
  29. runDialog :: String -> String -> String -> IO Int
  30. runDialog dialogMessage buttonOne buttonTwo = do
  31.   _ <- initGUI
  32.   dialog <- dialogNew
  33.   set
  34.     dialog
  35.     [ windowTitle := "Word combinations splitter"
  36.     , windowDefaultWidth := 300
  37.     , containerBorderWidth := 8
  38.     ]
  39.  
  40.   label <- labelNew (Nothing :: Maybe String)
  41.   labelSetMarkup label dialogMessage
  42.   miscSetAlignment label 0.05 0
  43.   upperPart <- dialogGetUpper dialog
  44.   set upperPart [containerChild := label]
  45.  
  46.   button1 <- dialogAddButton dialog buttonOne (ResponseUser 0)
  47.   button2 <- dialogAddButton dialog buttonTwo (ResponseUser 1)
  48.   button3 <- dialogAddButton dialog "Abort" (ResponseUser 2)
  49.  
  50.   widgetShowAll dialog
  51.  
  52.   dialogExitStatus <- dialogRun dialog
  53.   _ <- onClicked button1 mainQuit
  54.   _ <- onClicked button2 mainQuit
  55.   _ <- onClicked button3 mainQuit
  56.   return $ (\(ResponseUser x) -> x) dialogExitStatus
  57.  
  58. spacesToNewlines :: String -> String
  59. spacesToNewlines = map spaceToNewline
  60.   where
  61.     spaceToNewline x
  62.       | x `elem` " \t" = '\n'
  63.       | x == ' ' = ' '
  64.       | otherwise = x
  65.  
  66. processItem :: String -> IO ()
  67. processItem [] = return ()
  68. processItem string
  69.   | ' ' `elem` string = do
  70.     processedDialogResponse <-
  71.       runDialog ("Check: <b>" ++ string ++ "</b>") "Split" "Skip"
  72.     case processedDialogResponse of
  73.       0 -> putStrLn $ spacesToNewlines string
  74.       1 -> putStrLn string
  75.       2 -> void (die "Aborted!")
  76.       _ -> return ()
  77.   | otherwise = putStrLn string
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement