RandomText.purs 3.98 KB
Newer Older
1 2 3 4 5 6 7 8 9
{-|
Module      : RandomText
Description : Contextual randomized text
Copyright   : (c) CNRS / Alexandre Delanoe, 2017-present
License     : AGPL + CECILL v3
Maintainer  : alexandre.delanoe@iscpif.fr
Stability   : experimental
Portability : POSIX

10
How semantic emerges from contextualized randomness can be experimented
11 12 13 14 15 16 17 18 19
with these simple functions;

randomSentences: randomizes sentences in a paragraph.
randomWords    : randomizes words     in a sentence.
randomChars    : randomizes chars     in a word.

TODO: add some tests as examples.
-}

20
module Gargantext.Components.RandomText where
21 22 23

import Prelude

24
import Data.Array (drop, dropEnd, filter, foldl, head, length, tail, take, takeEnd, (!!))
25
import Data.Maybe (Maybe(Nothing, Just), fromJust)
Sudhir Kumar's avatar
Sudhir Kumar committed
26 27 28 29
import Data.String (Pattern(..), split)
import Data.String.CodeUnits (fromCharArray, toCharArray)
import Effect (Effect)
import Effect.Random (randomInt)
30 31
import Partial (crash)
import Partial.Unsafe (unsafePartial)
32

33 34

-------------------------------------------------------------------
35
randomSentences :: String -> Effect String
36 37 38 39 40
randomSentences ss = case (length (sentences ss)) >= 5 of
                    true -> foldl (\a b -> a <> "." <> b) "" <$> randomPart (sentences ss)
                    _    -> pure ss


41
randomWords :: String -> Effect String
42 43 44 45
randomWords ws = case (length (words ws)) >= 5 of
                    true -> foldl (\a b -> a <> " " <> b) "" <$> randomPart (words ws)
                    _    -> pure ws

46
randomChars :: String -> Effect String
47 48 49 50 51 52
randomChars word = case (length (toCharArray word)) >= 5 of
                    true -> fromCharArray <$> randomPart (toCharArray word)
                    _    -> pure word

-------------------------------------------------------------------
words :: String -> Array String
53 54
words sentence = filter ((/=) "")
               $ split (Pattern " ") sentence
55 56

sentences :: String -> Array String
57 58
sentences paragraph = filter ((/=) "")
                    $ split (Pattern ".") paragraph
59 60 61 62 63 64 65 66
-------------------------------------------------------------------


data RandomWheel a = RandomWheel { before :: Array a
                                 , during :: a
                                 , after  :: Array a
                                 }

Sudhir Kumar's avatar
Sudhir Kumar committed
67
randomPart :: forall b. Array b -> Effect (Array b)
68 69
randomPart array = randomArrayPoly middle
                 >>= \(middle') -> pure ( start <> middle' <> end)
70 71 72 73 74 75
        where
            start   = take    2          array
            middle  = dropEnd 2 $ drop 2 array
            end     = takeEnd 2          array


Sudhir Kumar's avatar
Sudhir Kumar committed
76
randomArrayPoly :: forall a. Array a -> Effect (Array a)
77 78
randomArrayPoly wheel = case head wheel of
                         Nothing -> pure []
79
                         Just wheel' -> randomWheel (RandomWheel { before:wheel, during:wheel', after:[]})
80 81
                                     >>= \(RandomWheel rand) -> (pure rand.after)

Sudhir Kumar's avatar
Sudhir Kumar committed
82
randomWheel :: forall b. RandomWheel b -> Effect (RandomWheel b)
83
randomWheel (RandomWheel {before:[], during:d, after:a}) =
84 85 86 87 88 89 90
    pure   (RandomWheel {before:[], during:d, after:a})

randomWheel (RandomWheel {before:b, during:d, after:a}) = do
    RandomWheel {before:b', during:d', after:a'} <- randomArray b
    randomWheel $ RandomWheel {before:b', during:d', after:(a <> [d'])}


Sudhir Kumar's avatar
Sudhir Kumar committed
91
randomArray :: forall b. Array b -> Effect (RandomWheel b)
92 93 94 95 96 97 98
randomArray array = unsafePartial $ do
    n    <- randomInt 0 (length array - 1)

    let maybeDuring = (array !! n)

    case maybeDuring of
         Nothing    ->
99
            crash "[G.C.N.H.R.RandomText ERROR] It should never happen."
100 101 102 103 104 105 106 107 108 109 110 111 112 113
         Just during  ->
            pure $ RandomWheel { before : remove n array
                               , during : during
                               , after  : []
                               }


remove :: forall a. Int -> Array a -> Array a
remove n [] = []
remove n xs = unsafePartial $ case n of
                   0 -> fromJust $ tail xs
                   _ -> (take n xs) <> (drop (n+1) xs)

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