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

-- |
-- Module      :  Phonetic.Languages.General.Common
-- Copyright   :  (c) OleksandrZhabenko 2020-2021
-- License     :  MIT
-- Stability   :  Experimental
-- Maintainer  :  olexandr543@yahoo.com
--
-- Is rewritten from the module Phonetic.Languages.Common from the package @phonetic-languages-simplified-examples-common@.
--

module Phonetic.Languages.General.Common (
  fLines
  , fLinesIO
) where

import Data.SubG (subG)
import Data.Phonetic.Languages.PrepareText
import Data.Char (isAlpha)
import Data.Monoid (mappend)

fLines
 :: Concatenations -- ^ Data used to concatenate 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 :: Concatenations
-> String -> String -> String -> Int -> String -> [String]
fLines Concatenations
ysss String
xs String
us String
vs !Int
toOneLine String
ys =
  let preText :: [String]
preText = (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\Char
x -> String -> Char -> Bool
isPLL String
xs Char
x Bool -> Bool -> Bool
&& Char -> Bool
isAlpha Char
x)) ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Concatenations -> String -> String -> [String]
prepareText Concatenations
ysss String
xs (String -> [String]) -> (String -> String) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
         (\String
z -> if Int
toOneLine Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 then [String] -> String
unwords ([String] -> String) -> (String -> [String]) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
z else String
z) (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ String
ys
      wss :: [Int]
wss = (String -> Int) -> [String] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([String] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([String] -> Int) -> (String -> [String]) -> String -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> [String]
forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a), Monoid (t (t a))) =>
t a -> t a -> t (t a)
subG (Char
' 'Char -> String -> String
forall a. a -> [a] -> [a]
:String
us String -> String -> String
forall a. Monoid a => a -> a -> a
`mappend` String
vs)) [String]
preText
        in String -> String -> [String] -> [Int] -> [String]
forall a (t :: * -> *) a.
(Ord a, Num a, Foldable t, Monoid (t a), Eq a) =>
t a -> t a -> [[a]] -> [a] -> [[a]]
helpG2 String
us String
vs [String]
preText [Int]
wss

helpG2 :: t a -> t a -> [[a]] -> [a] -> [[a]]
helpG2 t a
us t a
vs ([a]
t:[[a]]
ts) (a
r:[a]
rs)
 | a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
7 = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
filter (a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` (t a
us t a -> t a -> t a
forall a. Monoid a => a -> a -> a
`mappend` t a
vs)) [a]
t[a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
:t a -> t a -> [[a]] -> [a] -> [[a]]
helpG2 t a
us t a
vs [[a]]
ts [a]
rs
 | Bool
otherwise = [a]
t[a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
:t a -> t a -> [[a]] -> [a] -> [[a]]
helpG2 t a
us t a
vs [[a]]
ts [a]
rs
helpG2 t a
_ t a
_ [[a]]
_ [a]
_ = []

fLinesIO
 :: Concatenations -- ^ Data used to concatenate 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 :: Concatenations -> String -> String -> String -> String -> IO ()
fLinesIO Concatenations
ysss String
xs String
us String
vs String
ys =
  let preText :: [String]
preText = (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\Char
x -> String -> Char -> Bool
isPLL String
xs Char
x Bool -> Bool -> Bool
&& Char -> Bool
isAlpha Char
x)) ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Concatenations -> String -> String -> [String]
prepareText Concatenations
ysss String
xs (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ String
ys
      wss :: [Int]
wss = (String -> Int) -> [String] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([String] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([String] -> Int) -> (String -> [String]) -> String -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> [String]
forall (t :: * -> *) a.
(InsertLeft t a, Monoid (t a), Monoid (t (t a))) =>
t a -> t a -> t (t a)
subG (Char
' 'Char -> String -> String
forall a. a -> [a] -> [a]
:String
us String -> String -> String
forall a. Monoid a => a -> a -> a
`mappend` String
vs)) [String]
preText
        in (String -> IO ()) -> [String] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ String -> IO ()
putStrLn ([String] -> IO ()) -> ([Int] -> [String]) -> [Int] -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, String) -> String) -> [(Int, String)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (\(Int
i,String
x) -> Int -> String
forall a. Show a => a -> String
show (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\t" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
x) ([(Int, String)] -> [String])
-> ([Int] -> [(Int, String)]) -> [Int] -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int, String)] -> [(Int, String)]
forall a. [a] -> [a]
helpG3 ([(Int, String)] -> [(Int, String)])
-> ([Int] -> [(Int, String)]) -> [Int] -> [(Int, String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String] -> [(Int, String)]
forall (t :: * -> *) b. Foldable t => b -> t b -> [(Int, b)]
indexedL String
"" ([String] -> [(Int, String)])
-> ([Int] -> [String]) -> [Int] -> [(Int, String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> [String] -> [Int] -> [String]
forall a (t :: * -> *) a.
(Ord a, Num a, Foldable t, Monoid (t a), Eq a) =>
t a -> t a -> [[a]] -> [a] -> [[a]]
helpG2 String
us String
vs [String]
preText ([Int] -> IO ()) -> [Int] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Int]
wss

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

helpG3 :: [a] -> [a]
helpG3 :: [a] -> [a]
helpG3 [a]
xs
 | [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs = []
 | Bool
otherwise = [a] -> [a]
forall a. [a] -> [a]
init [a]
xs