|
| 1 | +-- | |
| 2 | +-- Module : Main |
| 3 | +-- Copyright : (c) OleksandrZhabenko 2020 |
| 4 | +-- License : MIT |
| 5 | +-- Stability : Experimental |
| 6 | + |
| 7 | +-- |
| 8 | +-- Analyzes a poetic text in Ukrainian, for every line prints statistic data and |
| 9 | +-- then for the whole poem prints the hypothesis evaluation information. |
| 10 | +-- |
| 11 | +-- To enable parallel computations (potentially, they can speed up the work), please, run the @propertiesText@ executable with |
| 12 | +-- @+RTS -threaded -RTS@ command line options with possibly @-N@ option inside. |
| 13 | +-- |
| 14 | + |
| 15 | +{-# OPTIONS_GHC -threaded -rtsopts #-} |
| 16 | + |
| 17 | +{-# LANGUAGE BangPatterns, FlexibleContexts #-} |
| 18 | + |
| 19 | +module Main where |
| 20 | + |
| 21 | + |
| 22 | +import Data.SubG hiding (takeWhile,dropWhile) |
| 23 | +import System.IO |
| 24 | +import Control.Concurrent |
| 25 | +import Control.Exception |
| 26 | +import Control.Parallel.Strategies |
| 27 | +import Data.Maybe (fromMaybe) |
| 28 | +import Data.List (sort) |
| 29 | +import Text.Read (readMaybe) |
| 30 | +import qualified Data.Vector as VB |
| 31 | +import Melodics.ByteString.Ukrainian |
| 32 | +import System.Environment |
| 33 | +import Languages.Phonetic.Ukrainian.PrepareText |
| 34 | +import Numeric (showFFloat) |
| 35 | +import Languages.UniquenessPeriods.Vector.Filters |
| 36 | +import Data.Char (isAlpha) |
| 37 | +import Data.Statistics.RulesIntervalsPlus |
| 38 | +import Data.MinMax.Preconditions |
| 39 | +import Phonetic.Languages.Lists.Ukrainian.PropertiesSyllablesG2 |
| 40 | +import Phonetic.Languages.Simplified.StrictVG |
| 41 | +import Phonetic.Languages.Permutations |
| 42 | +import Phonetic.Languages.Simplified.DataG |
| 43 | +import Phonetic.Languages.Simplified.Lists.Ukrainian.FuncRep2RelatedG2 |
| 44 | +import Languages.UniquenessPeriods.Vector.Constraints.Encoded |
| 45 | +import Phonetic.Languages.Simplified.Lists.SimpleConstraints |
| 46 | + |
| 47 | +main :: IO () |
| 48 | +main = do |
| 49 | + args000 <- getArgs |
| 50 | + let !args00 = filter (/= "++B") args000 |
| 51 | + !lstW = any (== "++B") args000 |
| 52 | + !args0 = takeWhile (/= "+M") args00 `mappend` drop 1 (dropWhile (/= "-M") args00) |
| 53 | + !multiples = drop 1 . dropWhile (/= "+M") . takeWhile (/= "-M") $ args00 -- Arguments for multiple metrices mode |
| 54 | + !args = filter (\xs -> all (/= ':') xs && all (/= '@') xs) args0 |
| 55 | + !coeffs = readCF . concat . take 1 $ args -- The first command line argument. If not sure, just enter \"1_\". |
| 56 | + !lInes = filter (any (== ':')) args0 |
| 57 | + !numbersJustPrint = filter (== "@n") args0 |
| 58 | + if isPair coeffs then do |
| 59 | + let !file = concat . drop 1 . take 2 $ args -- The second command line argument except those ones that are RTS arguments |
| 60 | + if null numbersJustPrint then do |
| 61 | + let !gzS = concat . take 1 . drop 2 $ args -- The third command line argument that controls the choice of the number of intervals |
| 62 | + !printLine = fromMaybe 0 (readMaybe (concat . take 1 . drop 3 $ args)::(Maybe Int)) -- The fourth command line argument except those ones that are RTS arguments. Set to 1 if you would like to print the current line within the information |
| 63 | + !toOneLine = fromMaybe 0 (readMaybe (concat . take 1 . drop 4 $ args)::(Maybe Int)) -- The fifth command line argument except those ones that are RTS arguments. Set to 1 if you would like to convert the text into one single line before applying to it the processment (it can be more conceptually consistent in such a case) |
| 64 | + !choice = concat . drop 5 . take 6 $ args -- The sixth command line argument that controls what properties are used. |
| 65 | + generalProc lstW multiples lInes coeffs file gzS printLine toOneLine choice |
| 66 | + else do |
| 67 | + contents <- readFile file |
| 68 | + fLinesIO contents |
| 69 | + else do |
| 70 | + let !file = concat . take 1 $ args |
| 71 | + if null numbersJustPrint then do |
| 72 | + let !gzS = concat . take 1 . drop 1 $ args |
| 73 | + !printLine = fromMaybe 0 (readMaybe (concat . take 1 . drop 2 $ args)::(Maybe Int)) |
| 74 | + !toOneLine = fromMaybe 0 (readMaybe (concat . take 1 . drop 3 $ args)::(Maybe Int)) |
| 75 | + !choice = concat . drop 4 . take 5 $ args |
| 76 | + generalProc lstW multiples lInes coeffs file gzS printLine toOneLine choice |
| 77 | + else do |
| 78 | + contents <- readFile file |
| 79 | + fLinesIO contents |
| 80 | + |
| 81 | +generalProc :: Bool -> [String] -> [String] -> Coeffs2 -> FilePath -> String -> Int -> Int -> String -> IO () |
| 82 | +generalProc lstW multiples2 lInes coeffs file gzS printLine toOneLine choice |
| 83 | + | null lInes = do |
| 84 | + contents <- readFile file |
| 85 | + let !flines = fLines toOneLine contents |
| 86 | + getData3 lstW coeffs (getIntervalsNS lstW gzS flines) printLine choice multiples2 flines |
| 87 | + | otherwise = do |
| 88 | + contents <- readFile file |
| 89 | + let !flines = fLines toOneLine . unlines . linesFromArgsG lInes . fLines 0 $ contents |
| 90 | + getData3 lstW coeffs (getIntervalsNS lstW gzS flines) printLine choice multiples2 flines |
| 91 | + |
| 92 | +linesFromArgs1 :: Int -> String -> [String] -> [String] |
| 93 | +linesFromArgs1 n xs yss = |
| 94 | + let (!ys,!zs) = (\(x,z) -> (x, drop 1 z)) . break (== ':') $ xs |
| 95 | + !ts = sort . map (min n . abs) $ [fromMaybe 1 (readMaybe ys::Maybe Int), fromMaybe n (readMaybe zs::Maybe Int)] in |
| 96 | + drop (head ts - 1) . take (last ts) $ yss |
| 97 | + |
| 98 | +linesFromArgsG :: [String] -> [String] -> [String] |
| 99 | +linesFromArgsG xss yss = let n = length yss in concatMap (\ts -> linesFromArgs1 n ts yss) xss |
| 100 | + |
| 101 | +getData3 :: Bool -> Coeffs2 -> Int -> Int -> String -> [String] -> [String] -> IO () |
| 102 | +getData3 lstW coeffs gz printLine choice multiples3 zss = let !permsV4 = genPermutationsVL in putStrLn (replicate (length multiples3 + 1) '\t' `mappend` show gz) >> mapM_ (process1Line lstW coeffs gz printLine choice multiples3 permsV4) zss |
| 103 | + |
| 104 | +process1Line :: Bool -> Coeffs2 -> Int -> Int -> String -> [String] -> VB.Vector [VB.Vector Int] -> String -> IO () |
| 105 | +process1Line lstW coeffs gz printLine choice multiples4 !permsV50 v |
| 106 | + | null multiples4 = bracket (do { |
| 107 | + myThread <- forkIO (do |
| 108 | + let !v2 = words v |
| 109 | + !l2 = length v2 - 2 |
| 110 | + if l2 >= (if lstW then 1 else 0) then do |
| 111 | + let !permsV5 = decodeConstraint1 (fromMaybe (E 1) . readMaybeECG (l2 + 1) . showB (l2 + 2) $ lstW) . |
| 112 | + VB.unsafeIndex permsV50 $ l2 |
| 113 | + ((!minE,!maxE),!data2) = runEval (parTuple2 rpar rpar (minMax11C . map (toTransPropertiesF' (chooseMax id coeffs choice )) . |
| 114 | + uniquenessVariants2GNBL ' ' id id id permsV5 $ v2, toTransPropertiesF' (chooseMax id coeffs choice) . unwords . subG " 01-" $ v)) |
| 115 | + (!wordsN,!intervalN) = (l2 + 2, intervalNRealFrac minE maxE gz data2) |
| 116 | + !ratio = if maxE == 0.0 then 0.0 else 2.0 * data2 / (minE + maxE) |
| 117 | + hPutStr stdout . showFFloat (precChoice choice) minE $ "\t" |
| 118 | + hPutStr stdout . showFFloat (precChoice choice) data2 $ "\t" |
| 119 | + hPutStr stdout . showFFloat (precChoice choice) maxE $ "\t" |
| 120 | + hPutStr stdout . showFFloat (Just 4) (data2 / minE) $ "\t" |
| 121 | + hPutStr stdout . showFFloat (Just 4) (maxE / minE) $ "\t" |
| 122 | + hPutStr stdout . showFFloat (Just 4) (maxE / data2) $ "\t" |
| 123 | + hPutStr stdout . showFFloat (Just 8) ratio $ "\t" |
| 124 | + hPutStr stdout ('\t':show (wordsN::Int)) |
| 125 | + hPutStr stdout ('\t':show (intervalN::Int)) |
| 126 | + hPutStrLn stdout (if printLine == 1 then '\t':v else "") |
| 127 | + else putStrLn (replicate (length multiples4) '\t' ++ if printLine == 1 then '\t':v else "")) |
| 128 | + ; return myThread }) (killThread) (\_ -> putStr "") |
| 129 | + | otherwise = bracket (do { |
| 130 | + myThread <- forkIO (do |
| 131 | + let !v2 = words v |
| 132 | + !l2 = length v2 - 2 |
| 133 | + if l2 >= (if lstW then 1 else 0) then do |
| 134 | + let !permsV5 = decodeConstraint1 (fromMaybe (E 1) . readMaybeECG (l2 + 1) . showB (l2 + 2) $ lstW) . |
| 135 | + VB.unsafeIndex permsV50 $ l2 |
| 136 | + rs = parMap rpar (\choiceMMs -> (minMax11C . |
| 137 | + map (toTransPropertiesF' (chooseMax id coeffs choiceMMs)) . |
| 138 | + uniquenessVariants2GNBL ' ' id id id permsV5 $ v2, |
| 139 | + toTransPropertiesF' (chooseMax id coeffs choiceMMs) . unwords . subG " 01-" $ v,gz)) multiples4 |
| 140 | + (!wordsN,!intervalNs) = (l2 + 2, map (\((!x,!y),!z,!t) -> intervalNRealFrac x y t z) rs) |
| 141 | + in do |
| 142 | + hPutStr stdout (show (wordsN::Int)) |
| 143 | + mapM_ (\i -> hPutStr stdout ('\t':show (i::Int))) intervalNs |
| 144 | + hPutStrLn stdout (if printLine == 1 then '\t':v else "") |
| 145 | + else putStrLn (replicate (length multiples4) '\t' ++ if printLine == 1 then '\t':v else "")) |
| 146 | + ; return myThread }) (killThread) (\_ -> putStr "") |
| 147 | + |
| 148 | +fLines :: Int -> String -> [String] |
| 149 | +fLines !toOneLine ys = |
| 150 | + let preText = filter (any (\x -> isUkrainianL x && isAlpha x)) . prepareText . (\z -> if toOneLine == 1 then unwords . words $ z else z) $ ys |
| 151 | + wss = map (length . subG " 01-") preText |
| 152 | + g (t:ts) (r:rs) = if r > 7 then filter (`notElem` "01-") t:g ts rs else t:g ts rs |
| 153 | + g _ _ = [] |
| 154 | + in g preText wss |
| 155 | + |
| 156 | +fLinesIO :: String -> IO () |
| 157 | +fLinesIO ys = |
| 158 | + let preText = filter (any (\x -> isUkrainianL x && isAlpha x)) . prepareText $ ys |
| 159 | + wss = map (length . subG " 01-") preText |
| 160 | + g (t:ts) (r:rs) = if r > 7 then filter (`notElem` "01-") t:g ts rs else t:g ts rs |
| 161 | + g _ _ = [] |
| 162 | + in VB.mapM_ putStrLn . VB.map (\(i,x) -> show (i + 1) ++ "\t" ++ x) . VB.indexed . VB.fromList . g preText $ wss |
0 commit comments