packages feed

phonetic-languages-simplified-generalized-examples-common-0.6.1.0: Phladiprelio/General/CommonE.hs

{-# OPTIONS_GHC -threaded -rtsopts #-}
{-# OPTIONS_HADDOCK show-extensions #-}
{-# LANGUAGE BangPatterns, FlexibleContexts, NoImplicitPrelude #-}

-- |
-- Module      :  Phladiprelio.General.CommonE
-- Copyright   :  (c) Oleksandr Zhabenko 2020-2023
-- License     :  MIT
-- Stability   :  Experimental
-- Maintainer  :  oleksandr.zhabenko@yahoo.com
--
-- Is rewritten from the module Phladiprelio.Ukrainian.CommonE from the package @phonetic-languages-simplified-examples-common@.
--

module Phladiprelio.General.CommonE (
  fLinesN
  , fLines
  , fLinesNIO
  , fLinesIO
) where

import GHC.Base hiding (foldr,length)
import GHC.List hiding (foldr,length)
import System.IO (putStrLn)
import GHC.Num ((+),(-),(*))
import Text.Show (show)
import Data.Foldable (Foldable,foldr,length)
import Phladiprelio.General.PrepareText
import Data.Char (isAlpha)

fLinesN
 :: Int
 -> Concatenations -- ^ Data used to concatenate (prepend) the basic grammar preserving words and word sequences to the next word to
 -- leave the most of the meaning (semantics) of the text available to easy understanding while reading and listening to.
 -> Concatenations -- ^ Data used to concatenate (append) the basic grammar preserving words and word sequences to the next word to
 -- leave the most of the meaning (semantics) of the text available to easy understanding while reading and listening to.
 -> String
 -> String
 -> String
 -> Int
 -> String
 -> [String]
fLinesN n ysss zsss xs us vs !toOneLine ys = filter (any (\x -> isPLL xs x && isAlpha x)) . prepareTextN n ysss zsss xs .
         (\z -> if toOneLine == 1 then unls z else z) $ ys
           -- the 'unls' is taken from the 'Data.List.words' and rewritten to be equal to 'unwords' . 'words'
           where unls s  =  case dropWhile (`elem` (' ':us `mappend` vs)) s of
                             "" -> []
                             s' -> w `mappend` (' ' : unls s'')
                                where (w, s'') = break (`elem` (' ':us `mappend` vs)) s'

fLines
 :: Concatenations -- ^ Data used to concatenate (prepend) the basic grammar preserving words and word sequences to the next word to
 -- leave the most of the meaning (semantics) of the text available to easy understanding while reading and listening to.
 -> Concatenations -- ^ Data used to concatenate (append) the basic grammar preserving words and word sequences to the next word to
 -- leave the most of the meaning (semantics) of the text available to easy understanding while reading and listening to.
 -> String
 -> String
 -> String
 -> Int
 -> String
 -> [String]
fLines = fLinesN 7
{-# INLINE fLines #-}

fLinesNIO
 :: Int
 -> Concatenations -- ^ Data used to concatenate (prepend) the basic grammar preserving words and word sequences to the next word to
 -- leave the most of the meaning (semantics) of the text available to easy understanding while reading and listening to.
 -> Concatenations -- ^ Data used to concatenate (append) the basic grammar preserving words and word sequences to the next word to
 -- leave the most of the meaning (semantics) of the text available to easy understanding while reading and listening to.
 -> String
 -> String
 -> String
 -> String
 -> IO ()
fLinesNIO n ysss zsss xs us vs ys = mapM putStrLn (map (\(i,x) -> show (i + 1) ++ "\t" ++ x) . helpG3 . indexedL "" .
  filter (any (\x -> isPLL xs x && isAlpha x)) . prepareTextN n ysss zsss xs $ ys) >> pure ()

fLinesIO
 :: Concatenations -- ^ Data used to concatenate (prepend) the basic grammar preserving words and word sequences to the next word to
 -- leave the most of the meaning (semantics) of the text available to easy understanding while reading and listening to.
 -> Concatenations -- ^ Data used to concatenate (append) the basic grammar preserving words and word sequences to the next word to
 -- leave the most of the meaning (semantics) of the text available to easy understanding while reading and listening to.
 -> String
 -> String
 -> String
 -> String
 -> IO ()
fLinesIO = fLinesNIO 7
{-# INLINE fLinesIO #-}

-- | Indexes the 'Foldable' structure using consequential 'Int' values.
indexedL :: Foldable t => b -> t b -> [(Int, b)]
indexedL y zs = foldr f v zs
  where !v = [(length zs,y)]
        f x ((j,z):ys) = (j-1,x):(j,z):ys
{-# INLINE indexedL #-}

helpG3 :: [a] -> [a]
helpG3 xs
 | null xs = []
 | otherwise = init xs