Prose

public static class Prose
Utility class for various textual and linguistic operations.

Methods

BreakIntoWords

#
public static string[] BreakIntoWords(string phrase, Font font, float maximumWidth)
Break a phrase into word fragments. This method will split the phrase at all spaces, and attempt to break up words which exceed the maximum width into hyphenated chunks.
phrase (string)
font (Font)
maximumWidth (float)

BreakWordsIntoLines

#
public static string[] BreakWordsIntoLines(string phrase, Font font, float maximumWidth)
Break a phrase into lines whose length doesn't exceed the maximum width. An attempt will be made to hyphenate words which exceed the limit.
phrase (string)
font (Font)
maximumWidth (float)

ComposeSequence<T>

#
public static string ComposeSequence<T>(IList<T> list, Func<T, string> formatter, SequenceTrimming trimming, SequenceSymbols symbols = null, int limit = 2147483647)
Compose a symbol-separated list of values whose entire length does not exceed some character count.
list (IList<T>)
formatter (Func<T, string>)
trimming (Prose.SequenceTrimming)
symbols (Prose.SequenceSymbols = null)
limit (int = 2147483647)

ConvertWildcardToRegex

#
public static string ConvertWildcardToRegex(string wildcardPattern, string decimalSeparator = ".", string thousandSeparator = ",")
Convert a wildcard pattern string into an equivalent regex notation.

  • = any sequence.

? = any single character.

# = any numeric sequence.

wildcardPattern (string)
decimalSeparator (string = ".")
thousandSeparator (string = ",")

ConvertWildcardToRegexWithGroups

#
public static string ConvertWildcardToRegexWithGroups(string wildcardPattern, string decimalSeparator = ".", string thousandSeparator = ",")
Convert a wildcard pattern string into an equivalent regex notation with wildcard group capture.

  • = any sequence.

? = any single character.

# = any numeric sequence.

wildcardPattern (string)
decimalSeparator (string = ".")
thousandSeparator (string = ",")

DefaultArticle

#
public static string DefaultArticle(string text)
Determines the default indefinite article (i.e. a/an) to use with this string. It may result in ungrammatical English as there exist irregular cases.
text (string)Text to examine.
The most likely indefinite article to use.

Distance

#
public static int Distance(string text, string other, StringComparison comparison, StringMetric metric)
Measure the distance between two strings.
text (string)String to compare.
other (string)String to compare to.
comparison (StringComparison)Comparison to use.
metric (Prose.StringMetric)Distance metric algorithm.
'Edit' distance between the two strings.

GroupValues<T>

#
public static T[][] GroupValues<T>(T[] values, Func<T, string> name, int minGroupSize, int maxGroupSize)
group a set of values based on their descriptions.
values (T[])Value to categorise.
name (Func<T, string>)Value -> Word function used for sorting and grouping.
minGroupSize (int)Groups will be populated until they are at least this big.
maxGroupSize (int)Groups will be terminated when they exceed this size.
Array of value groups.

Interpolate

#
public static string Interpolate(string a, string b, float interpolation)
Interpolate between two strings.
a (string)First string.
b (string)Second string.
interpolation (float)Interpolation factor.
A string in between a and b.

IsVariableName

#Example
public static bool IsVariableName(this string text, out string message)
Test to see whether a string is a valid variable name.
text (string)Variable name to test.
message (out string)Message in case of a violation.
True if name is a valid variable name.

LabelSequence

#
public static IEnumerable<string> LabelSequence(string template = "{0}", params string[] pool)
Create an endless labeling sequence of strings: A, B, C, ..., AA, AB, AC, ..., XUP, XUQ, ...
template (string = "{0}")
pool (params string[])

LabelSequence

#
public static IEnumerable<string> LabelSequence(string template = "{0}", string pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Create an endless labeling sequence of strings: A, B, C, ..., AA, AB, AC, ..., XUP, XUQ, ...
template (string = "{0}")
pool (string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ")

LargestSize

#Example
public static SizeF LargestSize(this IEnumerable<string> strings, Font font)
Measure the largest width and height amongst a collection of strings. Width and height are measured independently and may belong to different strings.
strings (IEnumerable<string>)Strings to measure.
font (Font)Font to use.
Size containing all individual sizes..

MaskCsCode

#Example
public static Prose.Mask MaskCsCode(this string code, bool lineComments, bool blockComments, bool charLiterals, bool stringLiterals)
Mask various ranges in a string representing C# style code.
code (string)Code to mask.
lineComments (bool)If true, line comments will be masked.
blockComments (bool)If true, block comments will be masked.
charLiterals (bool)If true, char literals will be masked.
stringLiterals (bool)If true, string literals (including style prefix symbols) will be masked.
Mask.

MaskCsCode

#Example
public static Prose.Mask MaskCsCode(this string code)
Mask all comment and text literal ranges in a string representing C# style code.
code (string)Code to mask.
Mask.

MaskString

#Example
public static Prose.Mask MaskString(this string text, params MaskPattern[] patterns)
Apply a set of masking patterns to a string.
text (string)String to mask.
patterns (params Prose.MaskPattern[])Patterns.
String mask.

MatchPenalty

#
public static int MatchPenalty(string[] keys, string pattern, StringComparison comparison = StringComparison.OrdinalIgnoreCase, StringMetric metric = StringMetric.Damerau)
Compute the matching penalty for a string and a set of search keys. This function can be used to find the best match from a collection of strings using fuzzy matching.
keys (string[])Search keys.
pattern (string)String to match against.
comparison (StringComparison = StringComparison.OrdinalIgnoreCase)Comparison type.
metric (Prose.StringMetric = StringMetric.Damerau)String distance metric.
Match penalty. An exact match yields zero, any deviation yields a negative value. The lower the return value, the worse the match.

Mode

#
public static string Mode(IEnumerable<string> strings, StringComparison comparison, Func<string[], string> resolution)
Finds the most common value in a collection of strings. Null strings are not included in the search, but empty ones are.
strings (IEnumerable<string>)String collection to inspect.
comparison (StringComparison)String comparison.
resolution (Func<string[], string>)Resolution method for when various strings are all tied.
Most common string in collection or null if collection is empty.

Mode

#
public static string Mode(IEnumerable<string> strings, StringComparison comparison)
Finds the most common value in a collection of strings. Null strings are not included in the search, but empty ones are.
strings (IEnumerable<string>)String collection to inspect.
comparison (StringComparison)String comparison.
Most common string in collection or null if collection is empty.

Mode

#
public static string Mode(IEnumerable<string> strings)
Finds the most common value in a collection of strings. Null strings are not included in the search, but empty ones are.
strings (IEnumerable<string>)String collection to inspect.
Most common string in collection or null if collection is empty.

ModifyCasing

#
public static string ModifyCasing(string text, IEnumerable<bool> casing)
Change the casing of a string based on a boolean pattern.
text (string)String to case.
casing (IEnumerable<bool>)Boolean pattern (true=uppercase, false=lowercase).
Cased string.

ModifyCasing

#
public static string ModifyCasing(string text, IEnumerable<int> casing)
Change the casing of a string based on an integer pattern.
text (string)String to case.
casing (IEnumerable<int>)Integer pattern (negative = lowercase, 0 = originalcase, positive = uppercase).
Cased string.

Overlap

#
public static string Overlap(IEnumerable<string> strings, string variesSymbol, bool ignoreTrailingCharacters = true)
Create a string which has a varies symbol for each char location that differs between a collection of strings.
strings (IEnumerable<string>)Strings to test.
variesSymbol (string)Varies symbol to insert.
ignoreTrailingCharacters (bool = true)If true, non-matching trailing characters won't get masked.
Overlayed string.

ParseKeyValueLine

#
public static int ParseKeyValueLine(string line, out string leadup, out KeyValueFragment[] fragments)
Parse a line of colon separated key value pairs.
line (string)
leadup (out string)
fragments (out Prose.KeyValueFragment[])

PluralPhrasing

#
public static string PluralPhrasing(int count, params string[] cardinalPhrases)
Create a formatted phrase based on a count integer.
count (int)Count.
cardinalPhrases (params string[])Array of phrases. The first phrase will be used in case count = 0, the second phrase if count = 1, and so on.

ReadFromJson

#
public static string ReadFromJson(string json, string name)
Read a value from a json string.
json (string)Json string to read from.
name (string)Name of value to retrieve.

RemoveCsComments

#Example
public static string RemoveCsComments(this string code)
Replace all C# style comments in a piece of code with whitespace.
code (string)String to clean.
String with no comments.

RemoveFormators

#
public static string RemoveFormators(string text, bool punctionation = true, bool formators = true)
Remove those parts of a text which are not helpful during searches. These include commas and other punctuation symbols as well as formator words such as 'the', 'or', 'as', 'is' etc.
text (string)Text to process.
punctionation (bool = true)If true, various kinds of punctuation will be replaced by spaces.
formators (bool = true)If true, words carrying no important meaning in the context of searches will be removed.

SortAlphaNumeric

#
public static string[] SortAlphaNumeric(string[] strings, StringComparison comparison)
Sort a bunch of strings using alphanumeric comparisons.
strings (string[])Strings to sort.
comparison (StringComparison)Comparison to use for the non-numeric portions of the strings.
An index map for the sorted strings.

SortAlphaNumericMap

#
public static Maths.IndexMap SortAlphaNumericMap(string[] strings, StringComparison comparison)
Sort a bunch of strings using alphanumeric comparisons.
strings (string[])Strings to sort.
comparison (StringComparison)Comparison to use for the non-numeric portions of the strings.
An index map for the sorted strings.

Syllabify

#
public static string[] Syllabify(string text)
Break a text into syllables.
text (string)Text to break.
Array of syllables.

ToPlural

#
public static string ToPlural(string word)
word (string)

ToSentenceCase

#
public static string ToSentenceCase(string text, CultureInfo culture)
Upper case first non-whitespace character after every sentence terminator.
text (string)String to case.
culture (CultureInfo)Culture to use for cse changes.
Sentence cased string.

ToSentenceCase

#
public static string ToSentenceCase(string text)
Upper case first non-whitespace character after every sentence terminator.
text (string)String to case.
Sentence cased string.

ToSubscript

#Example
public static string ToSubscript(this string text)
Convert a string to subscript notation. This only works for digits (0-9), the plus and minus symbols and parenthesis. Do note that not all fonts support these subscript characters.
text (string)Text to modify.
Modified text.

ToSuperscript

#Example
public static string ToSuperscript(this string text)
Convert a string to superscript notation. This only works for digits (0-9), the plus and minus symbols and parenthesis. Do note that not all fonts support these superscript characters.
text (string)Text to modify.
Modified text.

ToVariableName

#Example
public static string ToVariableName(this string text)
Turn a string into a valid variable name. This method applies VB/C# conventions, only alphanumeric chars and the underscore, provided the name doesn't begin with a digit. This method may return an empty string if all the characters are considered invalid, empty strings are not valid names and need to be handled separately.
text (string)String to convert.
Variable name.

ToWordCase

#
public static string ToWordCase(string text, CultureInfo culture)
Upper case the first character of every word.
text (string)String to case.
culture (CultureInfo)Culture to use for case modification.
Word cased string.

ToWordCase

#
public static string ToWordCase(string text)
Upper case the first character of every word.
text (string)String to case.
Word cased string.

TryParseInteger

#
public static bool TryParseInteger(string text, bool allowSign, int startIndex, out int nextIndex, out BigInteger integer)
Figure out whether a string contains an integer at a specific location.
text (string)String to parse.
allowSign (bool)If true, the first symbol is allowed to be a plus or minus sign.
startIndex (int)Index to start from.
nextIndex (out int)Index of first non-numeric character following the integer, or 'startIndex+1' if no number could be found.
integer (out BigInteger)Integer at location.
True if an integer starts at the given location.

TryParseIntegerSubscript

#
public static bool TryParseIntegerSubscript(string text, bool allowSign, int startIndex, out int nextIndex, out BigInteger integer)
Figure out whether a string contains a subscript integer at a specific location.
text (string)String to parse.
allowSign (bool)If true, the first symbol is allowed to be a plus or minus sign.
startIndex (int)Index to start from.
nextIndex (out int)Index of first non-numeric character following the integer, or 'startIndex+1' if no number could be found.
integer (out BigInteger)Integer found at location.
True if an integer starts at the given location.

TryParseIntegerSuperscript

#
public static bool TryParseIntegerSuperscript(string text, bool allowSign, int startIndex, out int nextIndex, out BigInteger integer)
Figure out whether a string contains a subscript integer at a specific location.
text (string)String to parse.
allowSign (bool)If true, the first symbol is allowed to be a plus or minus sign.
startIndex (int)Index to start from.
nextIndex (out int)Index of first non-numeric character following the integer, or 'startIndex+1' if no number could be found.
integer (out BigInteger)Integer found at location.
True if an integer starts at the given location.

TryParseNumber

#
public static bool TryParseNumber(string text, int startIndex, out int endIndex, out double number)
Try and parse a number which occurs in a text.
text (string)Text containing possible number.
startIndex (int)Index of first character to consider.
endIndex (out int)Index of first character after the numeric substring, or just the next character if no number was found.
number (out double)Number at the given index.
True if a number starts at the given index.

TryParseNumber

#
public static bool TryParseNumber(string text, int startIndex, out int endIndex, out object number)
Try and parse a number which occurs in a text.
text (string)Text containing possible number.
startIndex (int)Index of first character to consider.
endIndex (out int)Index of first character after the numeric substring, or just the next character if no number was found.
number (out object)Number at the given index. Could be a double, an int or a BigInteger.
True if a number starts at the given index.

TryParsePoint

#
public static bool TryParsePoint(string text, int startIndex, out int endIndex, out Point3d point)
Try and parse a set of coordinates from a text. Leading whitespace is ignored, and if the coordinates are surrounded by braces that will be taken into account.
text (string)Text to parse.
startIndex (int)Index at which to start looking for point coordinates.
endIndex (out int)Index *after* the final character part of the coordinates, if found. At least 'startIndex+1'."/>
point (out Point3d)Point coordinate, if found.
True if a point was found at the given string index.

UndoSubscript

#Example
public static string UndoSubscript(this string text)
Convert all subscript characters into regular ones.
text (string)

UndoSuperscript

#Example
public static string UndoSuperscript(this string text)
Convert all superscript characters into regular ones.
text (string)

WriteToJson

#
public static string WriteToJson(string json, string name, string value)
Add a name+value entry to an existing Json string.
json (string)
name (string)
value (string)