Browse Source

Added validation checks to RequestTextFromFile.

The validation checks in the console program still remain. Also, added a function to validate the file path the when the client passes it into the lib.
master
Craig Oates 6 years ago
parent
commit
dbb720d4c0
  1. 5
      WetPancake/DataProcessing.fs
  2. 32
      WetPancake/ProductServices.fs
  3. 4
      WetPancakeCLI/ConsoleCommands.cs

5
WetPancake/DataProcessing.fs

@ -3,6 +3,7 @@
open System.Text.RegularExpressions
open DataCleaning
open System
open System.IO
let MatchText pattern text = Regex.IsMatch(text, pattern)
@ -16,6 +17,10 @@
if sentences >= 1 then true
else false
let FilePathIsValid filePath =
if Path.GetExtension filePath = ".txt" && File.Exists filePath then true
else false
let SortIntoGroups groupSize text =
SplitText @"\s+" text // Splits text where there is a space.
|> Seq.windowed groupSize

32
WetPancake/ProductServices.fs

@ -21,6 +21,7 @@ module Pancake =
return GenerateMarkovText (PickRandomNumber 2 10) data
}
// TODO: Write tests for the validation checks (throws exceptions)
let RequestTextAsync (gibberishLevel: int) (sentences: int) =
async {
try
@ -32,22 +33,37 @@ module Pancake =
|> SortIntoGroups gibberishLevel
|> GenerateMap
return GenerateMarkovText sentences data
else return raise (ArgumentException("Invalid argument. Must be greater than 0.", "sentences"))
else
return raise (ArgumentException("Invalid argument. Must be greater than 0.", "sentences"))
else
return raise (ArgumentException("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberishLevel"))
with
| :? ArgumentException as ex->
| :? ArgumentException as ex ->
return ex.Message
}
// TODO: Write tests for the validation checks (throws exceptions)
let RequestTextFromFileAsync (gibberishLevel: int) (sentences: int) (filePath: string) =
async {
let data =
LoadFile filePath
|> ApplyStandardSetup
|> SortIntoGroups gibberishLevel
|> GenerateMap
return GenerateMarkovText sentences data
try
if GibberishLevelIsValid gibberishLevel then
if SentencesIsValid sentences then
if FilePathIsValid filePath then
let data =
LoadFile filePath
|> ApplyStandardSetup
|> SortIntoGroups gibberishLevel
|> GenerateMap
return GenerateMarkovText sentences data
else
return raise (ArgumentException("Invalid argument. File must be a .txt file and include a valid file path.", "filePath"))
else
return raise (ArgumentException("Invalid argument. Must be greater than 0.", "sentences"))
else
return raise (ArgumentException("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberishLevel"))
with
| :? ArgumentException as ex ->
return ex.Message
}
let RequestAllTemplateFilesAsync () =

4
WetPancakeCLI/ConsoleCommands.cs

@ -80,7 +80,6 @@ namespace WetPancakeCLI
{
try
{
// Should these checks be moved into the main library? How are people going to now to add these themselves when they are new to the project?
if (gibberishLevel < 2 || gibberishLevel > 20) throw new ArgumentException("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberishLevel");
if (sentences < 1) throw new ArgumentException("Invalid argument. Must be greater than 0.", "sentences");
if (Path.GetExtension(filePath) != ".txt") throw new FileLoadException("The file entered is not a plain text (.txt) file.", filePath);
@ -89,8 +88,7 @@ namespace WetPancakeCLI
catch (Exception e)
{
Debug.WriteLine(e.Message);
WriteLine("[WARNING] Unable to complete last request. Re-executing command...");
return FSharpAsync.StartAsTask(RequestTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result;
return e.Message;
}
}

Loading…
Cancel
Save