Created
April 8, 2022 20:27
-
-
Save baronfel/df26cb0fbf5a7969f1421e4e35ed4dad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Text.RegularExpressions | |
let alternativeCharacters = dict [ ' ', ' ' ] | |
let bypassCharactersJoined = "" | |
let escape s = | |
String.map (fun character -> match alternativeCharacters.TryGetValue(character) with (true, c) -> c | (false, _) -> character) s | |
let tryMatch pattern s = | |
let m = Regex.Match(s, pattern) | |
if m.Success then Some(m.ToString()) else None | |
/// these are the actual operations you want to do to the string | |
let trials wordList = | |
let joined = String.concat "|" wordList | |
let pattern = $"\\b({joined})\\b" | |
let excludedPattern = $"[^{bypassCharactersJoined}]({joined})[^{bypassCharactersJoined}]" | |
[ | |
id, tryMatch pattern | |
escape, tryMatch pattern | |
id, tryMatch excludedPattern | |
] | |
// given a set of comparisons, evaluate them against an input string, returning the first successful match | |
let evaluate (actions: ((string -> string) * (string -> string option)) seq) (inputString:string) = | |
actions | |
|> Seq.tryPick (fun (transform, tryMatch) -> tryMatch (transform inputString)) | |
type BsonValue = string | |
type BsonArray = BsonValue [] | |
/// now, to find the exact match, you calculate the comparisons you want to make and run them in sequence, taking the first successful. | |
/// this formulation allows us to precompute the actions once for a set of words (might not be useful for your case) | |
let exactMatch (wordList: BsonArray) = | |
let evaluator = evaluate (trials wordList) | |
fun inputString -> evaluator inputString | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment