packages feed

phonetic-languages-simplified-examples-array-0.13.0.0: Phonetic/Languages/Simplified/Array/Ukrainian/ReadProperties.hs

-- |
-- Module      :  Phonetic.Languages.Simplified.Array.Ukrainian.ReadProperties
-- Copyright   :  (c) OleksandrZhabenko 2021
-- License     :  MIT
-- Stability   :  Experimental
-- Maintainer  :  olexandr543@yahoo.com
--
-- Functions to read the properties data from the files with the special Haskell-like syntaxis.

{-# LANGUAGE BangPatterns #-}

module Phonetic.Languages.Simplified.Array.Ukrainian.ReadProperties where

import CaseBi.Arr (getBFstL')
import Phonetic.Languages.Array.Ukrainian.PropertiesFuncRepG2
import Phonetic.Languages.Array.Ukrainian.PropertiesSyllablesG2
import Languages.Phonetic.Ukrainian.Syllable.Double.Arr
import Text.Read (readMaybe)
import Data.Maybe
import Interpreter.StringConversion
import Languages.Phonetic.Ukrainian.Syllable.Arr (UZPP(..),UZPP2,PhoneticType(..))

uzpp2s :: [UZPP2]
uzpp2s = [UZ 'A' D, UZ 'A' K, UZ 'B' D, UZ 'B' K, UZ 'C' S, UZ 'D' N, UZ 'E' L, UZ 'E' M, UZ 'F' L,
   UZ 'F' M, UZ 'a' W, UZ 'b' D, UZ 'b' K, UZ 'c' D, UZ 'd' D, UZ 'd' K, UZ 'e' W, UZ 'f' L,
   UZ 'f' M, UZ 'g' D, UZ 'g' K, UZ 'h' D, UZ 'h' K, UZ 'i' W, UZ 'j' D, UZ 'j' K, UZ 'k' L,
   UZ 'k' M, UZ 'l' S, UZ 'l' O, UZ 'm' S, UZ 'm' O, UZ 'n' S, UZ 'n' O, UZ 'o' W, UZ 'p' L,
   UZ 'p' M, UZ 'q' E, UZ 'r' S, UZ 'r' O, UZ 's' L, UZ 't' L, UZ 't' M, UZ 'u' W, UZ 'v' S,
   UZ 'v' O, UZ 'w' N, UZ 'x' L, UZ 'x' M, UZ 'y' W, UZ 'z' D, UZ 'z' K]

{-|
\"[UZ \'A\' D, UZ \'A\' K, UZ \'B\' D, UZ \'B\' K, UZ \'C\' S, UZ \'D\' N, UZ \'E\' L, UZ \'E\' M, UZ \'F\' L,
   UZ \'F\' M, UZ \'a\' W, UZ \'b\' D, UZ \'b\' K, UZ \'c\' D, UZ \'d\' D, UZ \'d\' K, UZ \'e\' W, UZ \'f\' L,
   UZ \'f\' M, UZ \'g\' D, UZ \'g\' K, UZ \'h\' D, UZ \'h\' K, UZ \'i\' W, UZ \'j\' D, UZ \'j\' K, UZ \'k\' L,
   UZ \'k\' M, UZ \'l\' S, UZ \'l\' O, UZ \'m\' S, UZ \'m\' O, UZ \'n\' S, UZ \'n\' O, UZ \'o\' W, UZ \'p\' L,
   UZ \'p\' M, UZ \'q\' E, UZ \'r\' S, UZ \'r\' O, UZ \'s\' L, UZ \'t\' L, UZ \'t\' M, UZ \'u\' W, UZ \'v\' S,
   UZ \'v\' O, UZ \'w\' N, UZ \'x\' L, UZ \'x\' M, UZ \'y\' W, UZ \'z\' D, UZ \'z\' K]\"
The first number is the default value that corresponds usually to the word gap duration (and here is not important).
The next 52 'Double' numbers become the durations of the above specified 'UZPP2' values respectively, the order
must be preserved (if you consider it important, well, it should be!). If some number in the file cannot be read
as a 'Double' number the function uses the first one that can be instead (the default value). If no such is specified
at all, then the default number is 1.0 for all the 'UZPP2' sound representations that is hardly correct.

-}
readUZPP2ToDouble :: String -> (Double,[(UZPP2, Double)])
readUZPP2ToDouble xs
 | null xs = (1.0,zip uzpp2s . replicate 10000 $ 1.0)
 | otherwise =
    let wws = lines xs
        dbls = map (\ks -> readMaybe ks::Maybe Double) wws
        dbH
         | null dbls || all isNothing dbls = 1.0
         | otherwise = fromJust . head . filter isJust $ dbls
        dbSs = map (fromMaybe dbH) dbls
        (firstD,lsts)
          | null dbls = (1.0,zip uzpp2s . replicate 10000 $ 1.0)
          | otherwise = (dbH,zip uzpp2s (dbSs `mappend` replicate 10000 1.0))
            in (firstD,lsts)

divide2SDDs :: String -> [String]
divide2SDDs ys
 | null tss = [unlines kss]
 | otherwise = unlines kss : divide2SDDs (unlines rss)
     where wwss = lines ys
           (kss,tss) = break (any (=='*')) wwss
           rss = dropWhile (any (== '*')) tss

readSyllableDurations :: FilePath -> IO [[[[UZPP2]]] -> [[Double]]]
readSyllableDurations file = do
  xs <- readFileIfAny file
  let yss = take 9 . divide2SDDs $ xs
      readData = map readUZPP2ToDouble yss
  return . map (\(d,zs) -> syllableDurationsGD (getBFstL' d zs)) $ readData