The purpose of this repository is to provide a way for people to generate random "placeholder text" -- with a Markov Chain. https://www.craigoates.net/Software/project/12
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

174 lines
6.9 KiB

namespace WetPancake
/// This module contains all the public functions in the Wet Pancake NuGet package.
/// It enables you to generate random text.
module Pancake =
open SystemServices
open DataAccess
open DataProcessing
open DataStructuring
open DataServices
open System
open System.IO
// TODO: Add XML comments to CleanResult function.
let CleanResultAsync noOfSentences text =
async {
let cleanText =
text
|> RemoveArtifactSentences noOfSentences
return cleanText
}
/// <summary>
/// An asynchronous function which generates random text.
/// </summary>
/// <returns>
/// A string of randomly generated text.
/// </returns>
/// <remarks>
/// This function will produce a maximum of 10 sentences with each function call.
/// </remarks>
let RequestRandomTextAsync() =
async {
let data =
LoadFile (SelectRandomSampleFile())
|> ApplyStandardSetup
|> SortIntoGroups (PickRandomNumber 2 10)
|> GenerateMap
return GenerateMarkovText (PickRandomNumber 2 10) data
}
/// <summary>
/// An asynchronous function which generates random text.
/// It uses the built-in template (.txt) files as a reference and the parameters specified by the caller.
/// </summary>
/// <param name="gibberishLevel">
/// Specifies how coherent the generated text is.
/// Min. value = 2. Max. value = 20.
/// The lower the value the more gibberish it will produce.
/// </param>
/// <param name="sentences">
/// Specifies the number of sentences to generate.
/// </param>
/// <returns>
/// A string of randomly generated text.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown when the "gibberishLevel" is not between 2 and 20.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// Thrown when the "sentences" count is less than 1.
/// </exception>
/// <remarks>
/// The higher the “gibberishLevel” and the “sentences” count is the longer it will take to complete the task.
/// Try to make the most of the asynchronous nature of the function as possible.
///</remarks>
let RequestTextAsync (gibberishLevel: int) (sentences: int) =
async {
try
GibberishLevelIsValid gibberishLevel |> ignore
SentencesIsValid sentences |> ignore
let data =
LoadFile (SelectRandomSampleFile())
|> ApplyStandardSetup
|> SortIntoGroups gibberishLevel
|> GenerateMap
return GenerateMarkovText sentences data
with
| :? ArgumentException as ex -> return ex.Message
}
/// <summary>
/// An asynchronous function which generates text.
/// It generates the text by using the (.txt) file passed in by the caller (as the reference) and the parameters specified by the caller.
/// </summary>
/// <param name="gibberishLevel">
/// Specifies how coherent the generated text is.
/// Min. value = 2. Max. value = 20.
/// The lower the value the more gibberish it will produce.
/// </param>
/// <param name="sentences">
/// Specifies the number of sentences to generate.
/// </param>
/// <param name="filePath">
/// The path of the (.txt) file the function will use as the reference text.
/// </param>
/// <returns>
/// A string of randomly generated text.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown when the "gibberishLevel" is not between 2 and 20.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// Thrown when the "sentences" count is less than 1.
/// </exception>
/// <exception cref="System.FileNotFoundException">
/// Thrown when the (.txt) file cannot be found.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// Thrown when the specified file is not a plain-text (.txt) file.
/// </exception>
/// <remarks>
/// The higher the “gibberishLevel” and the “sentences” count is the longer it will take to complete the task.
/// This, also, applies to the (.txt) file passed into the function.
/// The bigger it is, the longer it will take to read.
/// Try to make the most of the asynchronous nature of the function as possible.
/// Also, the function does not know what is in the (.txt) file until it reads it.
/// Defensive coding should be applied here.
/// </remarks>
let RequestTextFromFileAsync (gibberishLevel: int) (sentences: int) (filePath: string) =
async {
try
GibberishLevelIsValid gibberishLevel |> ignore
SentencesIsValid sentences |> ignore
FilePathIsValid |> ignore
let data =
LoadFile filePath
|> ApplyStandardSetup
|> SortIntoGroups gibberishLevel
|> GenerateMap
return GenerateMarkovText sentences data
with
| :? ArgumentException as ex -> return ex.Message
| :? FileNotFoundException as ex -> return ex.Message
}
/// <summary>
/// Finds every available template (.txt) file in Wet Pancake and forms a list of their file paths.
/// </summary>
/// <returns>
/// A list of every template (.txt) file path.
/// </returns>
/// <remarks>
/// You can use the information provided by this function alongside the "RequestTextFromFileAsync” function.
/// This allows you to generate text using one of the built-in template (.txt) files.
/// </remarks>
let RequestAllTemplateFilesAsync () =
async {
let files =
ListSampleFiles
|> Array.toList
return files
}
// ============================================================================================
// DO NOT FORGET TO WRITE TESTS FOR THESE TWO FUNCTIONS -- OR THE CLEANING FUNCTION ABOVE --
// ============================================================================================
// TODO: Add XML comment for RequestCleanTextAsync.
let RequestCleanTextAsync (gibberishLevel: int) (sentences: int) =
async {
let! text = RequestTextAsync gibberishLevel sentences
let! cleanText = CleanResultAsync sentences text
return cleanText
}
// TODO: Add XML comment for RequestCleanTextFromFileAsync.
let RequestCleanTextFromFileAsync (gibberishLevel: int) (sentences: int) (filePath: string) =
async {
let! text = RequestTextFromFileAsync gibberishLevel sentences filePath
let! cleanText = CleanResultAsync sentences text
return cleanText
}