{-# OPTIONS_HADDOCK show-extensions #-}

{-# LANGUAGE MultiWayIf #-}

-- |
-- Module      :  Data.Phonetic.Languages.PrepareText
-- Copyright   :  (c) OleksandrZhabenko 2020-2022
-- License     :  MIT
-- Stability   :  Experimental
-- Maintainer  :  olexandr543@yahoo.com
--
-- Helps to order the 7 or less phonetic language words (or their concatenations)
-- to obtain (to some extent) suitable for poetry or music text.
-- Earlier it has been a module DobutokO.Poetry.Ukrainian.PrepareText
-- from the @dobutokO-poetry@ package.
-- In particular, this module can be used to prepare the phonetic language text
-- by applying the most needed grammar to avoid misunderstanding
-- for the produced text. The attention is paid to the prepositions, pronouns, conjunctions
-- and particles that are most commonly connected (or not) in a significant way
-- with the next text.
-- Uses the information from:
-- https://uk.wikipedia.org/wiki/%D0%A1%D0%BF%D0%BE%D0%BB%D1%83%D1%87%D0%BD%D0%B8%D0%BA
-- and
-- https://uk.wikipedia.org/wiki/%D0%A7%D0%B0%D1%81%D1%82%D0%BA%D0%B0_(%D0%BC%D0%BE%D0%B2%D0%BE%D0%B7%D0%BD%D0%B0%D0%B2%D1%81%D1%82%D0%B2%D0%BE)
--
-- Uses arrays instead of vectors.
-- A list of basic (but, probably not complete and needed to be extended as needed) English words (the articles, pronouns,
-- particles, conjunctions etc.) the corresponding phonetic language translations of which are intended to be used as a
-- 'Concatenations' here is written to the file EnglishConcatenated.txt in the source tarball.

module Data.Phonetic.Languages.PrepareText (
  Concatenations
  -- * Basic functions
  , concatWordsFromLeftToRight
  , splitLines
  , splitLinesN
  , isSpC
  , sort2Concat
  -- * The end-user functions
  , prepareText
  , prepareTextN
  , growLinesN
  , prepareGrowTextMN
  , tuneLinesN
  , prepareTuneTextMN
  -- * Used to transform after convertToProperphonetic language from mmsyn6ukr package
  , isPLL
) where

import CaseBi.Arr (getBFstL',getBFst')
import Data.List.InnToOut.Basic (mapI)
import Data.Char (isAlpha,toLower)
import GHC.Arr
import Data.List (sort,sortOn)

-- | The lists in the list are sorted in the descending order by the word counts in the inner 'String's. All the 'String's
-- in each inner list have the same number of words, and if there is no 'String' with some intermediate number of words (e. g. there
-- are 'String's for 4 and 2 words, but there is no one for 3 words 'String's) then such corresponding list is absent (since
-- the 0.9.0.0 version). Probably the maximum number of words can be not more than 4, and the minimum number is
-- not less than 1, but it depends. The 'String's in the inner lists must be (unlike the inner
-- lists themselves) sorted in the ascending order for the data type to work correctly in the functions of the module.
type Concatenations = [[String]]

type ConcatenationsArr = [Array Int (String,Bool)]

defaultConversion :: Concatenations -> ConcatenationsArr
defaultConversion :: Concatenations -> ConcatenationsArr
defaultConversion Concatenations
ysss = ([[Char]] -> Array Int ([Char], Bool))
-> Concatenations -> ConcatenationsArr
forall a b. (a -> b) -> [a] -> [b]
map ([[Char]] -> Array Int ([Char], Bool)
f ([[Char]] -> Array Int ([Char], Bool))
-> ([[Char]] -> [[Char]]) -> [[Char]] -> Array Int ([Char], Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([Char] -> Bool) -> [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)) (Concatenations -> ConcatenationsArr)
-> (Concatenations -> Concatenations)
-> Concatenations
-> ConcatenationsArr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([[Char]] -> Bool) -> Concatenations -> Concatenations
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([[Char]] -> Bool) -> [[Char]] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) (Concatenations -> ConcatenationsArr)
-> Concatenations -> ConcatenationsArr
forall a b. (a -> b) -> a -> b
$ Concatenations
ysss
  where f :: [String] -> Array Int (String,Bool)
        f :: [[Char]] -> Array Int ([Char], Bool)
f [[Char]]
yss = let l :: Int
l = [[Char]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Char]]
yss in (Int, Int) -> [([Char], Bool)] -> Array Int ([Char], Bool)
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0,Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ([([Char], Bool)] -> Array Int ([Char], Bool))
-> ([Bool] -> [([Char], Bool)])
-> [Bool]
-> Array Int ([Char], Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [Bool] -> [([Char], Bool)]
forall a b. [a] -> [b] -> [(a, b)]
zip [[Char]]
yss ([Bool] -> [([Char], Bool)])
-> ([Bool] -> [Bool]) -> [Bool] -> [([Char], Bool)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Bool] -> [Bool]
forall a. [a] -> [a]
cycle ([Bool] -> Array Int ([Char], Bool))
-> [Bool] -> Array Int ([Char], Bool)
forall a b. (a -> b) -> a -> b
$ [Bool
True]

-- | Is used to convert a phonetic language text into list of 'String' each of which is ready to be
-- used by the functions from the other modules in the package.
-- It applies minimal grammar links and connections between the most commonly used phonetic language
-- words that \"should\" be paired and not dealt with separately
-- to avoid the misinterpretation and preserve maximum of the semantics for the
-- \"phonetic\" language on the phonetic language basis.
prepareText
  :: [[String]] -- ^ Is intended to become a valid 'Concatenations'.
  -> [[String]] -- ^ Is intended to become a valid 'Concatenations'.
  -> String -- ^ A sorted 'String' of possible characters in the phonetic language representation.
  -> String
  -> [String]
prepareText :: Concatenations -> Concatenations -> [Char] -> [Char] -> [[Char]]
prepareText = Int
-> Concatenations -> Concatenations -> [Char] -> [Char] -> [[Char]]
prepareTextN Int
7
{-# INLINE prepareText #-}

sort2Concat
 :: [[String]]
 -> Concatenations  -- ^ Data used to concatenate the basic grammar preserving words and word sequences to the next word or
 -- to the previous word to
 -- leave the most of the meaning (semantics) of the text available to easy understanding while reading and listening to.
sort2Concat :: Concatenations -> Concatenations
sort2Concat Concatenations
xsss
 | Concatenations -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Concatenations
xsss = []
 | Bool
otherwise = ([[Char]] -> [[Char]]) -> Concatenations -> Concatenations
forall a b. (a -> b) -> [a] -> [b]
map [[Char]] -> [[Char]]
forall a. Ord a => [a] -> [a]
sort (Concatenations -> Concatenations)
-> (Concatenations -> Concatenations)
-> Concatenations
-> Concatenations
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Concatenations -> Concatenations
forall a. [a] -> [a]
reverse (Concatenations -> Concatenations)
-> (Concatenations -> Concatenations)
-> Concatenations
-> Concatenations
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([[Char]] -> [Int]) -> Concatenations -> Concatenations
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (([Char] -> Int) -> [[Char]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([[Char]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([[Char]] -> Int) -> ([Char] -> [[Char]]) -> [Char] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words)) (Concatenations -> Concatenations)
-> Concatenations -> Concatenations
forall a b. (a -> b) -> a -> b
$ Concatenations
xsss

-----------------------------------------------------

complexWords2 :: ConcatenationsArr -> String -> (String -> String,String)
complexWords2 :: ConcatenationsArr -> [Char] -> ([Char] -> [Char], [Char])
complexWords2 ysss :: ConcatenationsArr
ysss@(Array Int ([Char], Bool)
yss:ConcatenationsArr
zsss) zs :: [Char]
zs@(Char
r:[Char]
rs)
 | (Bool, Array Int ([Char], Bool)) -> [Char] -> Bool
forall a b. Ord a => (b, Array Int (a, b)) -> a -> b
getBFst' (Bool
False, Array Int ([Char], Bool)
yss) ([Char] -> Bool) -> ([[Char]] -> [Char]) -> [[Char]] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [Char]
unwords ([[Char]] -> Bool) -> [[Char]] -> Bool
forall a b. (a -> b) -> a -> b
$ [[Char]]
tss = (([Char]
uwxs [Char] -> [Char] -> [Char]
forall a. Monoid a => a -> a -> a
`mappend`), [[Char]] -> [Char]
unwords [[Char]]
uss)
 | Bool
otherwise = ConcatenationsArr -> [Char] -> ([Char] -> [Char], [Char])
complexWords2 ConcatenationsArr
zsss [Char]
zs
      where y :: Int
y = [[Char]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([[Char]] -> Int) -> (Int -> [[Char]]) -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words ([Char] -> [[Char]]) -> (Int -> [Char]) -> Int -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char], Bool) -> [Char]
forall a b. (a, b) -> a
fst (([Char], Bool) -> [Char])
-> (Int -> ([Char], Bool)) -> Int -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array Int ([Char], Bool) -> Int -> ([Char], Bool)
forall i e. Array i e -> Int -> e
unsafeAt Array Int ([Char], Bool)
yss (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
0
            ([[Char]]
tss,[[Char]]
uss) = Int -> [[Char]] -> ([[Char]], [[Char]])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
y ([[Char]] -> ([[Char]], [[Char]]))
-> ([Char] -> [[Char]]) -> [Char] -> ([[Char]], [[Char]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words ([Char] -> ([[Char]], [[Char]])) -> [Char] -> ([[Char]], [[Char]])
forall a b. (a -> b) -> a -> b
$ [Char]
zs
            uwxs :: [Char]
uwxs = [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Char]]
tss
complexWords2 ConcatenationsArr
_ [Char]
zs = ([Char] -> [Char]
forall a. a -> a
id,[Char]
zs)

pairCompl :: (String -> String,String) -> (String,String)
pairCompl :: ([Char] -> [Char], [Char]) -> ([Char], [Char])
pairCompl ([Char] -> [Char]
f,[Char]
xs) = ([Char] -> [Char]
f [],[Char]
xs)

splitWords :: ConcatenationsArr -> [String] -> String -> (String,String)
splitWords :: ConcatenationsArr -> [[Char]] -> [Char] -> ([Char], [Char])
splitWords ConcatenationsArr
ysss [[Char]]
tss [Char]
zs = let ([Char]
ws,[Char]
us) = ([Char] -> [Char], [Char]) -> ([Char], [Char])
pairCompl (([Char] -> [Char], [Char]) -> ([Char], [Char]))
-> ([Char] -> ([Char] -> [Char], [Char]))
-> [Char]
-> ([Char], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConcatenationsArr -> [Char] -> ([Char] -> [Char], [Char])
complexWords2 ConcatenationsArr
ysss ([Char] -> ([Char], [Char])) -> [Char] -> ([Char], [Char])
forall a b. (a -> b) -> a -> b
$ [Char]
zs in if
  | [[Char]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([[Char]] -> Bool) -> ([Char] -> [[Char]]) -> [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words ([Char] -> Bool) -> [Char] -> Bool
forall a b. (a -> b) -> a -> b
$ [Char]
zs -> ([[Char]] -> [Char]
forall a. Monoid a => [a] -> a
mconcat [[Char]]
tss,[])
  | [Char] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
ws -> (\([[Char]]
xss,[[Char]]
uss) -> ([[Char]] -> [Char]
forall a. Monoid a => [a] -> a
mconcat ([[Char]]
tss [[Char]] -> [[Char]] -> [[Char]]
forall a. Monoid a => a -> a -> a
`mappend` [[Char]]
xss), [[Char]] -> [Char]
unwords [[Char]]
uss)) (([[Char]], [[Char]]) -> ([Char], [Char]))
-> ([Char] -> ([[Char]], [[Char]])) -> [Char] -> ([Char], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [[Char]] -> ([[Char]], [[Char]])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
1 ([[Char]] -> ([[Char]], [[Char]]))
-> ([Char] -> [[Char]]) -> [Char] -> ([[Char]], [[Char]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words ([Char] -> ([Char], [Char])) -> [Char] -> ([Char], [Char])
forall a b. (a -> b) -> a -> b
$ [Char]
zs
  | Bool
otherwise -> ConcatenationsArr -> [[Char]] -> [Char] -> ([Char], [Char])
splitWords ConcatenationsArr
ysss ([[Char]]
tss [[Char]] -> [[Char]] -> [[Char]]
forall a. Monoid a => a -> a -> a
`mappend` [[Char]
ws]) [Char]
us

concatWordsFromLeftToRight :: ConcatenationsArr -> String -> [String]
concatWordsFromLeftToRight :: ConcatenationsArr -> [Char] -> [[Char]]
concatWordsFromLeftToRight ConcatenationsArr
ysss [Char]
zs = let ([Char]
ws,[Char]
us) = ConcatenationsArr -> [[Char]] -> [Char] -> ([Char], [Char])
splitWords ConcatenationsArr
ysss [] [Char]
zs in
  if [Char] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
us then [[Char]
ws] else [Char]
ws [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: ConcatenationsArr -> [Char] -> [[Char]]
concatWordsFromLeftToRight ConcatenationsArr
ysss [Char]
us

-----------------------------------------------------

append2prependConv :: Concatenations -> Concatenations
append2prependConv :: Concatenations -> Concatenations
append2prependConv = ([[Char]] -> [[Char]]) -> Concatenations -> Concatenations
forall a b. (a -> b) -> [a] -> [b]
map (([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([[Char]] -> [Char]
unwords ([[Char]] -> [Char]) -> ([Char] -> [[Char]]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [[Char]]
forall a. [a] -> [a]
reverse ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words))
{-# INLINE append2prependConv #-}

left2right :: [String] -> String
left2right :: [[Char]] -> [Char]
left2right = [[Char]] -> [Char]
unwords ([[Char]] -> [Char])
-> ([[Char]] -> [[Char]]) -> [[Char]] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [[Char]]
forall a. [a] -> [a]
reverse ([[Char]] -> [[Char]])
-> ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> [Char]
forall a. [a] -> [a]
reverse
{-# INLINE left2right #-}

-----------------------------------------------------

-- | A generalized variant of the 'prepareText' with the arbitrary maximum number of the words in the lines given as the first argument.
prepareTextN
 :: Int -- ^ A maximum number of the words or their concatenations in the resulting list of 'String's.
 -> [[String]] -- ^ Is intended to become a valid 'Concatenations'.
 -> [[String]] -- ^ Is intended to become a valid 'Concatenations'.
 -> String -- ^ A sorted 'String' of possible characters in the phonetic language representation.
 -> String
 -> [String]
prepareTextN :: Int
-> Concatenations -> Concatenations -> [Char] -> [Char] -> [[Char]]
prepareTextN Int
n Concatenations
ysss Concatenations
zsss [Char]
xs = ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ([Char] -> Char -> Bool
isPLL [Char]
xs)) ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [[Char]] -> [[Char]]
splitLinesN Int
n ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([[Char]] -> [Char]
left2right ([[Char]] -> [Char]) -> ([Char] -> [[Char]]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  ConcatenationsArr -> [Char] -> [[Char]]
concatWordsFromLeftToRight (Concatenations -> ConcatenationsArr
defaultConversion (Concatenations -> ConcatenationsArr)
-> (Concatenations -> Concatenations)
-> Concatenations
-> ConcatenationsArr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Concatenations -> Concatenations
sort2Concat (Concatenations -> Concatenations)
-> (Concatenations -> Concatenations)
-> Concatenations
-> Concatenations
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Concatenations -> Concatenations
append2prependConv (Concatenations -> ConcatenationsArr)
-> Concatenations -> ConcatenationsArr
forall a b. (a -> b) -> a -> b
$ Concatenations
zsss) ([Char] -> [[Char]]) -> ([Char] -> [Char]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [Char]
left2right ([[Char]] -> [Char]) -> ([Char] -> [[Char]]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  ConcatenationsArr -> [Char] -> [[Char]]
concatWordsFromLeftToRight (Concatenations -> ConcatenationsArr
defaultConversion (Concatenations -> ConcatenationsArr)
-> (Concatenations -> Concatenations)
-> Concatenations
-> ConcatenationsArr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Concatenations -> Concatenations
sort2Concat (Concatenations -> ConcatenationsArr)
-> Concatenations -> ConcatenationsArr
forall a b. (a -> b) -> a -> b
$ Concatenations
ysss)) ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([Char] -> Bool) -> [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines

-- | A predicate to check whether the given character is one of the \"\' \\x2019\\x02BC-\".
isSpC :: Char -> Bool
isSpC :: Char -> Bool
isSpC Char
x = Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\'' Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ' Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\x2019' Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\x02BC' Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-'
{-# INLINE isSpC #-}
{-# DEPRECATED #-}

-- | The first argument must be a 'String' of sorted 'Char's in the ascending order of all possible symbols that can be
-- used for the text in the phonetic language selected. Can be prepared beforehand, or read from the file.
isPLL :: String -> Char -> Bool
isPLL :: [Char] -> Char -> Bool
isPLL [Char]
xs Char
y = Bool -> [(Char, Bool)] -> Char -> Bool
forall a b. Ord a => b -> [(a, b)] -> a -> b
getBFstL' Bool
False ([Char] -> [Bool] -> [(Char, Bool)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Char]
xs ([Bool] -> [(Char, Bool)])
-> (Bool -> [Bool]) -> Bool -> [(Char, Bool)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Bool -> [Bool]
forall a. Int -> a -> [a]
replicate Int
10000 (Bool -> [(Char, Bool)]) -> Bool -> [(Char, Bool)]
forall a b. (a -> b) -> a -> b
$ Bool
True) Char
y

-- | The function is recursive and is applied so that all returned elements ('String') are no longer than 7 words in them.
splitLines :: [String] -> [String]
splitLines :: [[Char]] -> [[Char]]
splitLines = Int -> [[Char]] -> [[Char]]
splitLinesN Int
7
{-# INLINE splitLines #-}

-- | A generalized variant of the 'splitLines' with the arbitrary maximum number of the words in the lines given as the first argument.
splitLinesN :: Int -> [String] -> [String]
splitLinesN :: Int -> [[Char]] -> [[Char]]
splitLinesN Int
n [[Char]]
xss
 | [[Char]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Char]]
xss Bool -> Bool -> Bool
|| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = []
 | Bool
otherwise = ([Char] -> Bool) -> ([Char] -> [[Char]]) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> (a -> [a]) -> [a] -> [a]
mapI (\[Char]
xs -> Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ([[Char]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([[Char]] -> Int) -> ([Char] -> [[Char]]) -> [Char] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words ([Char] -> Int) -> [Char] -> Int
forall a b. (a -> b) -> a -> b
$ [Char]
xs) Int
n Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT) (\[Char]
xs -> let yss :: [[Char]]
yss = [Char] -> [[Char]]
words [Char]
xs in
     Int -> [[Char]] -> [[Char]]
splitLinesN Int
n ([[Char]] -> [[Char]])
-> ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([[Char]] -> [Char]) -> Concatenations -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map [[Char]] -> [Char]
unwords (Concatenations -> [[Char]])
-> ([[Char]] -> Concatenations) -> [[Char]] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\([[Char]]
q,[[Char]]
r) -> [[[Char]]
q,[[Char]]
r]) (([[Char]], [[Char]]) -> Concatenations)
-> ([[Char]] -> ([[Char]], [[Char]])) -> [[Char]] -> Concatenations
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [[Char]] -> ([[Char]], [[Char]])
forall a. Int -> [a] -> ([a], [a])
splitAt ([[Char]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Char]]
yss Int -> Int -> Int
forall a. Integral a => a -> a -> a
`quot` Int
2) ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ [[Char]]
yss) ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ [[Char]]
xss

------------------------------------------------

{-| @ since 0.8.0.0
Given a positive number and a list tries to rearrange the list's 'String's by concatenation of the several elements of the list
so that the number of words in every new 'String' in the resulting list is not greater than the 'Int' argument. If some of the
'String's have more than that number quantity of the words then these 'String's are preserved.
-}
growLinesN :: Int -> [String] -> [String]
growLinesN :: Int -> [[Char]] -> [[Char]]
growLinesN Int
n [[Char]]
xss
 | [[Char]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Char]]
xss Bool -> Bool -> Bool
|| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = []
 | Bool
otherwise = [[Char]] -> [Char]
unwords [[Char]]
yss [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: Int -> [[Char]] -> [[Char]]
growLinesN Int
n [[Char]]
zss
     where l :: Int
l = [Int] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> Int) -> ([[Char]] -> [Int]) -> [[Char]] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n) ([Int] -> [Int]) -> ([[Char]] -> [Int]) -> [[Char]] -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int) -> [Int] -> [Int]
forall a. (a -> a -> a) -> [a] -> [a]
scanl1 Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) ([Int] -> [Int]) -> ([[Char]] -> [Int]) -> [[Char]] -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Int) -> [[Char]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([[Char]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([[Char]] -> Int) -> ([Char] -> [[Char]]) -> [Char] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words) ([[Char]] -> Int) -> [[Char]] -> Int
forall a b. (a -> b) -> a -> b
$ [[Char]]
xss -- the maximum number of lines to be taken
           ([[Char]]
yss,[[Char]]
zss) = Int -> [[Char]] -> ([[Char]], [[Char]])
forall a. Int -> [a] -> ([a], [a])
splitAt (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
l Int
1) [[Char]]
xss

{-| @ since 0.8.0.0
The function combines the 'prepareTextN' and 'growLinesN' function. Applies needed phonetic language preparations
to the text and tries to \'grow\' the resulting 'String's in the list so that the number of the words in every
of them is no greater than the given first 'Int' number.
-}
prepareGrowTextMN
 :: Int -- ^ A maximum number of the words or their concatenations in the resulting list of 'String's.
 -> Int -- ^ A number of words in every 'String' that the function firstly forms. To have some sense of usage, must be less than the first argument.
 -> [[String]] -- ^ Is intended to become a valid 'Concatenations'.
 -> [[String]] -- ^ Is intended to become a valid 'Concatenations'.
 -> String -- ^ A sorted 'String' of possible characters in the phonetic language representation.
 -> String
 -> [String]
prepareGrowTextMN :: Int
-> Int
-> Concatenations
-> Concatenations
-> [Char]
-> [Char]
-> [[Char]]
prepareGrowTextMN Int
m Int
n Concatenations
ysss Concatenations
zsss [Char]
xs = Int -> [[Char]] -> [[Char]]
growLinesN Int
m ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> Concatenations -> Concatenations -> [Char] -> [Char] -> [[Char]]
prepareTextN Int
n Concatenations
ysss Concatenations
zsss [Char]
xs
{-# INLINE prepareGrowTextMN #-}

-------------------------------------

{-| @ since 0.6.0.0
Recursively splits the concatenated list of lines of words so that in every resulting 'String' in the list
except the last one there is just 'Int' -- the first argument -- words.
-}
tuneLinesN :: Int -> [String] -> [String]
tuneLinesN :: Int -> [[Char]] -> [[Char]]
tuneLinesN Int
n [[Char]]
xss
 | [[Char]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Char]]
xss Bool -> Bool -> Bool
|| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = []
 | Bool
otherwise =
    let wss :: [[Char]]
wss = [Char] -> [[Char]]
words ([Char] -> [[Char]])
-> ([[Char]] -> [Char]) -> [[Char]] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [Char]
unwords ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ [[Char]]
xss
        ([[Char]]
yss,[[Char]]
zss) = Int -> [[Char]] -> ([[Char]], [[Char]])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [[Char]]
wss
          in [[Char]] -> [Char]
unwords [[Char]]
yss [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: Int -> [[Char]] -> [[Char]]
tuneLinesN Int
n [[Char]]
zss

{-| @ since 0.6.0.0
The function combines the 'prepareTextN' and 'tuneLinesN' functions. Applies needed phonetic language preparations
to the phonetic language text and splits the list of 'String's so that the number of the words in each of them (except the last one)
is equal the given first 'Int' number.
-}
prepareTuneTextMN
  :: Int -- ^ A maximum number of the words or their concatenations in the resulting list of 'String's.
  -> Int -- ^ A number of words in every 'String' that the function firstly forms. To have some sense of usage, must be less than the first argument.
  -> [[String]] -- ^ Is intended to become a valid 'Concatenations'.
  -> [[String]] -- ^ Is intended to become a valid 'Concatenations'.
  -> String -- ^ A sorted 'String' of possible characters in the phonetic language representation.
  -> String
  -> [String]
prepareTuneTextMN :: Int
-> Int
-> Concatenations
-> Concatenations
-> [Char]
-> [Char]
-> [[Char]]
prepareTuneTextMN Int
m Int
n Concatenations
ysss Concatenations
zsss [Char]
xs = Int -> [[Char]] -> [[Char]]
tuneLinesN Int
m ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> Concatenations -> Concatenations -> [Char] -> [Char] -> [[Char]]
prepareTextN Int
n Concatenations
ysss Concatenations
zsss [Char]
xs
{-# INLINE prepareTuneTextMN #-}