packages feed

phonetic-languages-simplified-examples-common-0.1.4.0: Phonetic/Languages/Common.hs

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

-- |
-- Module      :  Phonetic.Languages.Common
-- Copyright   :  (c) OleksandrZhabenko 2020-2021
-- License     :  MIT
-- Stability   :  Experimental
-- Maintainer  :  olexandr543@yahoo.com
--
--
--

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

import Phonetic.Languages.Ukrainian.PrepareText
import Data.Char (isAlpha,isSpace)
import Data.Monoid (mappend)

fLines :: Int -> String -> [String]
fLines !toOneLine ys = filter (any (\x -> isUkrainianL x && isAlpha x)) . prepareText .
  (\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 isSpace s of
                      "" -> []
                      s' -> w `mappend` (' ' : unls s'')
                        where (w, s'') = break isSpace s'

fLinesIO :: String -> IO ()
fLinesIO ys = mapM_ putStrLn . map (\(i,x) -> show (i + 1) ++ "\t" ++ x) . helpG3 . indexedL "" .
   filter (any (\x -> isUkrainianL x && isAlpha x)) . prepareText $ ys

-- | Is taken mostly from the Phonetic.Languages.Simplified.Lists.UniquenessPeriodsG module from the @phonetic-languages-simplified-common@ package.
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