Commit 0d6b9b29 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[Home/RandomText] Adding functions and home todo.

parent b5591b44
......@@ -194,25 +194,34 @@ loginSpec = simpleSpec performAction render
[ div [ className "col-md-4 content" ]
[ h3 []
[ a [ href "#", title "Random sentences in Gargantua's Books chapters, historically true" ]
[ text "Historic" ]
[ i [className "fas fa-random"] []
, text " Historic"
]
]
, p []
-- TODO use RandomText.randomSentences on this text (should be editable by user later)
[ text "Chapter 1.XV. How Gargantua was put under other schoolmasters. Chapter 2.XXII. How Panurge served a Parisian lady a trick that pleased her not very well. Chapter 3.XXXVII. How Pantagruel persuaded Panurge to take counsel of a fool. Chapter 4.LXI. How Gaster invented means to get and preserve corn. Chapter 5.XXXVIII. Of the temple's admirable pavement." ]
]
, div [ className "col-md-4 content" ]
[ h3 []
[ a [ href "#", title "Randomized words, semantically and syntaxically falses." ]
[ text "Presentation" ]
[ i [className "fas fa-random"] []
, text " Presentation"
]
]
, p []
-- TODO use RandomText.randomWords on this text (should be editable by user later)
[ text "Autem nascetur iaculis, sedfusce enimsed cursus posuere consectetuer eu justo aliquammauris. Phasellus vero nisi porttitor elit quod, leo feliscras ultricies non tempor sagittis. Liberoduis facilisinam erat dapibusnam, lacus dui duis tristique volutpatut quis vestibulum magna. Nobis faucibusvestibulum dolores minim. Bibendumin malesuada adipiscing ante, mattis fames nequeetiam lorem. No diam id. Litora quisaenean commodo lobortisetiam neque, libero mollis scelerisque inceptos ullamcorper sea congue delenit possim. " ]
]
, div [ className "col-md-4 content" ]
[ h3 []
[ a [ href "#", title "Randomized letters, true or false ?" ]
[ text "Tutoreil" ]
[ i [className "fas fa-random"] []
, text " Tutoreil"
]
]
, p []
-- TODO use RandomText.randomChars on this text (should be editable by user later)
[ text "Il paraît que l'rdore des lettres dans un mot n'a pas d'imtraopnce. La première et la dernière lettre doeivnt être à la bonne place. Le reste peut être dans un désordre total et on peut touojurs lire sans prolèbme. On ne lit donc pas chaque lettre en ellêem-me, mais le mot comme un tout. Un chaegmnent de référentiel et nous tranpossons ce résultat au texte luimê-me: l'rdore des mots est failbement important copamré au contexte du texte qui, lui, est copmté: comptexter avec Gargantext. "
, text " "
, text ""
......@@ -234,7 +243,7 @@ loginSpec = simpleSpec performAction render
[ text ", Copyrights "
, span [ className "glyphicon glyphicon-copyright-mark" ]
[]
, text " CNRS 2017"
, text " CNRS 2017-Present"
]
, a [ href "http://gitlab.iscpif.fr/humanities/gargantext/blob/stable/LICENSE", target "blank", title "Legal instructions of the project." ]
[ text ", Licences aGPLV3 and CECILL variant Affero compliant" ]
......
{-|
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
How semantic emerge from contextualized randomness can be experimented
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.
-}
module RandomText where
import Prelude
import Control.Monad.Eff (Eff(..))
import Control.Monad.Eff.Random (RANDOM(..), randomInt)
import Data.Maybe (Maybe(Nothing, Just), fromJust)
import Data.Array ( length, (!!), filter, foldl
, head, tail
, take, takeEnd
, drop, dropEnd
)
import Data.String ( toCharArray, fromCharArray
, split, Pattern(..)
)
import Data.Tuple.Nested ((/\))
import Partial (crash)
import Partial.Unsafe (unsafePartial)
import Unsafe.Coerce (unsafeCoerce)
-------------------------------------------------------------------
randomSentences :: forall a. String -> Eff ( random :: RANDOM | a ) String
randomSentences ss = case (length (sentences ss)) >= 5 of
true -> foldl (\a b -> a <> "." <> b) "" <$> randomPart (sentences ss)
_ -> pure ss
randomWords :: forall a. String -> Eff ( random :: RANDOM | a ) String
randomWords ws = case (length (words ws)) >= 5 of
true -> foldl (\a b -> a <> " " <> b) "" <$> randomPart (words ws)
_ -> pure ws
randomChars :: forall a. String -> Eff ( random :: RANDOM | a ) String
randomChars word = case (length (toCharArray word)) >= 5 of
true -> fromCharArray <$> randomPart (toCharArray word)
_ -> pure word
-------------------------------------------------------------------
words :: String -> Array String
words sentence = filter ((/=) "") $ split (Pattern " ") sentence
sentences :: String -> Array String
sentences paragraph = filter ((/=) "") $ split (Pattern ".") paragraph
-------------------------------------------------------------------
data RandomWheel a = RandomWheel { before :: Array a
, during :: a
, after :: Array a
}
randomPart :: forall a b. Array b -> Eff ( random :: RANDOM | a ) (Array b)
randomPart array = randomArrayPoly middle >>= \(middle') -> pure ( start <> middle' <> end)
where
start = take 2 array
middle = dropEnd 2 $ drop 2 array
end = takeEnd 2 array
randomArrayPoly :: forall a b. Array a -> Eff ( random :: RANDOM | b ) (Array a)
randomArrayPoly wheel = case head wheel of
Nothing -> pure []
Just wheel' -> randomWheel (RandomWheel { before:wheel, during:wheel', after:[]})
>>= \(RandomWheel rand) -> (pure rand.after)
randomWheel :: forall a b. RandomWheel b -> Eff ( random :: RANDOM | a ) (RandomWheel b)
randomWheel (RandomWheel {before:[], during:d, after:a}) =
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'])}
randomArray :: forall a b. Array b -> Eff ( random :: RANDOM | a ) (RandomWheel b)
randomArray array = unsafePartial $ do
n <- randomInt 0 (length array - 1)
let maybeDuring = (array !! n)
case maybeDuring of
Nothing ->
crash "[ERROR] It should never happen."
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)
-------------------------------------------------------------------
module RandomText where
import Prelude
import Control.Monad.Eff (Eff(..))
import Control.Monad.Eff.Random (RANDOM(..), randomInt)
import Data.Array (length, (!!), head, tail, take, takeEnd, drop, dropEnd)
import Data.Maybe (Maybe(Nothing, Just), fromJust)
import Data.String (toCharArray, fromCharArray)
import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\))
import Partial (crash)
import Partial.Unsafe (unsafePartial)
import Unsafe.Coerce (unsafeCoerce)
--rando Ran x [] = Ran x []
--rando Ran x xs = Ran (x <> [x']) (rando xs')
-- where
-- Ran x' xs' = randomIt xs
--
remove :: forall t5. Int -> Array t5 -> Array t5
remove n [] = []
remove n xs = unsafePartial $ case n of
0 -> fromJust $ tail xs
_ -> (take n xs) <> (drop (n+1) xs)
data Ran = Ran { l :: Char, r :: Array Char}
randomIt :: forall t46. String -> Eff ( random :: RANDOM | t46 ) Ran
randomIt ar = unsafePartial $ do
let ar' = toCharArray ar
n <- randomInt 0 (length ar' - 1)
let maybeChar = (ar' !! n )
let rest = remove n ar'
case maybeChar of
Nothing ->
crash "it should not happen"
Just char ->
pure $ Ran {l : char, r : rest}
randomText :: String -> String
randomText txt = fromCharArray ( start <> middle <> end)
where
txt' = toCharArray txt
start = take 2 txt'
middle = dropEnd 2 $ drop 2 txt'
end = takeEnd 2 txt'
testText :: String -> String
testText txt = case (length (toCharArray txt)) >= 5 of
true -> randomText txt
_ -> txt
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment