Fleet  0.0.9
Inference in the LOT
Functions | Variables
Strings.h File Reference
#include <sstream>
#include <atomic>
#include <string>
#include "Miscellaneous.h"
#include "Numerics.h"
#include "Random.h"
#include "Vector3D.h"
#include "str.h"
Include dependency graph for Strings.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename T >
std::string to_string_with_precision (const T a_value, const int n=14)
 
template<typename T >
bool is_prefix (const T &prefix, const T &x)
 Check if prefix is a prefix of x – works with iterables, including strings and vectors. More...
 
bool contains (const std::string &s, const std::string &x)
 
bool contains (const std::string &s, const char x)
 
void replace_all (std::string &s, const std::string &x, const std::string &y, int add=0)
 Replace all occurances of x with y in s. More...
 
std::string remove_characters (std::string s, const std::string &rem)
 Remove all characters in rem from s. TODO: Not very optimized here yeah. More...
 
template<const float & add_p, const float & del_p, typename T = std::string>
double p_delete_append (const T &x, const T &y, const float log_alphabet)
 Probability of converting x into y by deleting some number (each with del_p, then stopping with prob 1-del_p), adding with probability add_p, and then when we add selecting from an alphabet of size alpha_n. More...
 
std::pair< std::string, std::string > divide (const std::string &s, const char delimiter)
 
size_t count (const std::string &str, const std::string &sub)
 
size_t count (const std::string &str, const char x)
 
std::string reverse (std::string x)
 
std::string QQ (const std::string &x)
 
std::string Q (const std::string &x)
 
void check_alphabet (const std::string &s, const std::string &a)
 Check that s only uses characters from a. On failure, we print the string and assert false. More...
 
void check_alphabet (std::vector< std::string > t, const std::string &a)
 
unsigned int levenshtein_distance (const std::string &s1, const std::string &s2)
 Compute levenshtein distiance between two strings (NOTE: Or O(N^2)) More...
 
double p_KashyapOommen1984_edit (const std::string x, const std::string y, const double perr, const size_t nalphabet)
 The string probability model from Kashyap & Oommen, 1983, basically giving a string edit distance that is a probability model. This could really use some unit tests, but it is hard to find implementations. More...
 

Variables

const std::string EMPTY_STRING = ""
 
const std::string LAMBDA_STRING = "\u03BB"
 
const std::string LAMBDAXDOT_STRING = LAMBDA_STRING+"x."
 

Function Documentation

◆ check_alphabet() [1/2]

void check_alphabet ( const std::string &  s,
const std::string &  a 
)

Check that s only uses characters from a. On failure, we print the string and assert false.

Parameters
s
a

◆ check_alphabet() [2/2]

void check_alphabet ( std::vector< std::string >  t,
const std::string &  a 
)

◆ contains() [1/2]

bool contains ( const std::string &  s,
const std::string &  x 
)

◆ contains() [2/2]

bool contains ( const std::string &  s,
const char  x 
)

◆ count() [1/2]

size_t count ( const std::string &  str,
const std::string &  sub 
)

How many times does sub occur in str? Does not count overlapping substrings

Parameters
str
sub
Returns

◆ count() [2/2]

size_t count ( const std::string &  str,
const char  x 
)

◆ divide()

std::pair<std::string, std::string> divide ( const std::string &  s,
const char  delimiter 
)

◆ is_prefix()

template<typename T >
bool is_prefix ( const T &  prefix,
const T &  x 
)

Check if prefix is a prefix of x – works with iterables, including strings and vectors.

Parameters
prefix
x
Returns

For any number of iterable types, is prefix a prefix of x

Parameters
prefix
x
Returns

◆ levenshtein_distance()

unsigned int levenshtein_distance ( const std::string &  s1,
const std::string &  s2 
)

Compute levenshtein distiance between two strings (NOTE: Or O(N^2))

Parameters
s1
s2
Returns

◆ p_delete_append()

template<const float & add_p, const float & del_p, typename T = std::string>
double p_delete_append ( const T &  x,
const T &  y,
const float  log_alphabet 
)
inline

Probability of converting x into y by deleting some number (each with del_p, then stopping with prob 1-del_p), adding with probability add_p, and then when we add selecting from an alphabet of size alpha_n.

Parameters
x
y
del_p- probability of deleting the next character (geometric)
add_p- probability of adding (geometric)
log_alphabet- log of the size of alphabet
Returns
The probability of converting x to y by deleting characters with probability del_p and then adding with probability add_p

This function computes the probability that x would be converted into y, when we insert with probability add_p and delete with probabiltiy del_p and when we add we add from an alphabet of size log_alphabet. Note that this is a template function because otherwise we end up computing log(add_p) and log(del_p) a lot, and these are in fact constant.

Parameters
x
y
log_alphabet
Returns

◆ p_KashyapOommen1984_edit()

double p_KashyapOommen1984_edit ( const std::string  x,
const std::string  y,
const double  perr,
const size_t  nalphabet 
)

The string probability model from Kashyap & Oommen, 1983, basically giving a string edit distance that is a probability model. This could really use some unit tests, but it is hard to find implementations.

This assumes that the deletions, insertions, and changes all happen with a constant, equal probability of perr, but note that swaps and insertions also have to choose the character out of nalphabet. This could probably be optimized to not have to compute these logs

Parameters
x
y
perr
nalphabet
Returns

◆ Q()

std::string Q ( const std::string &  x)

Handy adding single quotes to a string

Parameters
x- input string
Returns

◆ QQ()

std::string QQ ( const std::string &  x)

Handy adding double quotes to a string

Parameters
x- input string
Returns

◆ remove_characters()

std::string remove_characters ( std::string  s,
const std::string &  rem 
)

Remove all characters in rem from s. TODO: Not very optimized here yeah.

Parameters
s
rem

◆ replace_all()

void replace_all ( std::string &  s,
const std::string &  x,
const std::string &  y,
int  add = 0 
)

Replace all occurances of x with y in s.

Parameters
s
x
y
add– replace x plus this many characters

◆ reverse()

std::string reverse ( std::string  x)

◆ to_string_with_precision()

template<typename T >
std::string to_string_with_precision ( const T  a_value,
const int  n = 14 
)

Variable Documentation

◆ EMPTY_STRING

const std::string EMPTY_STRING = ""

◆ LAMBDA_STRING

const std::string LAMBDA_STRING = "\u03BB"

◆ LAMBDAXDOT_STRING

const std::string LAMBDAXDOT_STRING = LAMBDA_STRING+"x."