5 people like it.

Content spinner

Given a string containing spinning expressions in the form "{string1|string2|...|stringN}", returns a string content randomly spinned. Spinning expressions can be nested. For instance, the spinned results of string "a{b|{c1|c2}}" will be one of the following : "ab" "ac1" "ac2"

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
open System.Text.RegularExpressions

let spin s =
    let pickRandom = Array.sortBy (fun _ -> System.Guid.NewGuid()) >> Array.head
    let regEx = "\{(?>\{(?<c>)|[^{}]+|\}(?<-c>))*(?(c)(?!))\}"
    let rec spinMatch (m:Match) =
        let s' =
            match m.Value.Substring(1, m.Value.Length-2) with
            | s when s.Contains("{") -> 
              Regex.Replace(s, regEx, spinMatch)
            | s -> s
        s'.Split('|') |> pickRandom
    Regex.Replace(s, regEx, spinMatch)
namespace System
namespace System.Text
namespace System.Text.RegularExpressions
val spin : s:string -> string

Full name: Script.spin
val s : string
val pickRandom : (string [] -> string)
module Array

from Microsoft.FSharp.Collections
val sortBy : projection:('T -> 'Key) -> array:'T [] -> 'T [] (requires comparison)

Full name: Microsoft.FSharp.Collections.Array.sortBy
Multiple items
type Guid =
  struct
    new : b:byte[] -> Guid + 4 overloads
    member CompareTo : value:obj -> int + 1 overload
    member Equals : o:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member ToByteArray : unit -> byte[]
    member ToString : unit -> string + 2 overloads
    static val Empty : Guid
    static member NewGuid : unit -> Guid
    static member Parse : input:string -> Guid
    static member ParseExact : input:string * format:string -> Guid
    ...
  end

Full name: System.Guid

--------------------
System.Guid()
System.Guid(b: byte []) : unit
System.Guid(g: string) : unit
System.Guid(a: int, b: int16, c: int16, d: byte []) : unit
System.Guid(a: uint32, b: uint16, c: uint16, d: byte, e: byte, f: byte, g: byte, h: byte, i: byte, j: byte, k: byte) : unit
System.Guid(a: int, b: int16, c: int16, d: byte, e: byte, f: byte, g: byte, h: byte, i: byte, j: byte, k: byte) : unit
System.Guid.NewGuid() : System.Guid
val head : array:'T [] -> 'T

Full name: Microsoft.FSharp.Collections.Array.head
val regEx : string
val spinMatch : (Match -> string)
val m : Match
type Match =
  inherit Group
  member Groups : GroupCollection
  member NextMatch : unit -> Match
  member Result : replacement:string -> string
  static member Empty : Match
  static member Synchronized : inner:Match -> Match

Full name: System.Text.RegularExpressions.Match
val s' : string
property Capture.Value: string
System.String.Substring(startIndex: int) : string
System.String.Substring(startIndex: int, length: int) : string
property System.String.Length: int
System.String.Contains(value: string) : bool
Multiple items
type Regex =
  new : pattern:string -> Regex + 1 overload
  member GetGroupNames : unit -> string[]
  member GetGroupNumbers : unit -> int[]
  member GroupNameFromNumber : i:int -> string
  member GroupNumberFromName : name:string -> int
  member IsMatch : input:string -> bool + 1 overload
  member Match : input:string -> Match + 2 overloads
  member Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  member Replace : input:string * replacement:string -> string + 5 overloads
  ...

Full name: System.Text.RegularExpressions.Regex

--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
Regex.Replace(input: string, pattern: string, evaluator: MatchEvaluator) : string
Regex.Replace(input: string, pattern: string, replacement: string) : string
Regex.Replace(input: string, pattern: string, evaluator: MatchEvaluator, options: RegexOptions) : string
Regex.Replace(input: string, pattern: string, replacement: string, options: RegexOptions) : string
System.String.Split([<System.ParamArray>] separator: char []) : string []
System.String.Split(separator: string [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int) : string []
System.String.Split(separator: string [], count: int, options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int, options: System.StringSplitOptions) : string []
Raw view New version

More information

Link:http://fssnip.net/7QR
Posted:3 years ago
Author:Didier Colin
Tags: text processing , content spinning