25 std::ostringstream out;
27 out << std::fixed << a_value;
47 if(prefix.size() > x.size())
return false;
48 if(prefix.size() == 0)
return true;
50 return std::equal(prefix.begin(), prefix.end(), x.begin());
53 bool contains(
const std::string& s,
const std::string& x) {
54 return s.find(x) != std::string::npos;
57 bool contains(
const std::string& s,
const char x) {
58 return s.find(x) != std::string::npos;
68 void replace_all(std::string& s,
const std::string& x,
const std::string& y,
int add=0) {
70 while(pos != std::string::npos) {
71 s.replace(pos,x.length()+add,y);
84 for(
auto it=s.begin(); it != s.end();) {
106 template<const
float& add_p, const
float& del_p,
typename T=std::
string>
119 static const float log_add_p = log(add_p);
120 static const float log_del_p = log(del_p);
121 static const float log_1madd_p = log(1.0-add_p);
122 static const float log_1mdel_p = log(1.0-del_p);
126 float lp = log_del_p*x.size() +
127 (log_add_p-log_alphabet)*y.size() + log_1madd_p;
131 for(
size_t mi=1;mi<=std::min(x.size(),y.size());mi++){
132 if(x.at(mi-1) == y.at(mi-1)) {
133 lp =
logplusexp(lp, log_del_p*(x.size()-mi) + log_1mdel_p +
134 (log_add_p-log_alphabet)*(y.size()-mi) + log_1madd_p);
144 std::pair<std::string, std::string>
divide(
const std::string& s,
const char delimiter) {
146 auto k = s.find(delimiter);
147 assert(k != std::string::npos &&
"*** Cannot divide a string without delimiter");
148 return std::make_pair(s.substr(0,k), s.substr(k+1));
153 size_t count(
const std::string&
str,
const std::string& sub) {
163 if (sub.length() == 0)
return 0;
166 for (
size_t offset = str.find(sub);
167 offset != std::string::npos;
168 offset = str.find(sub, offset + sub.length())) {
175 size_t count(
const std::string&
str,
const char x) {
186 return std::string(x.rbegin(),x.rend());
190 std::string
QQ(
const std::string& x) {
197 return std::string(
"\"") + x + std::string(
"\"");
199 std::string
Q(
const std::string& x) {
206 return std::string(
"\'") + x + std::string(
"\'");
217 if(a.find(c) == std::string::npos) {
218 std::cerr <<
"*** Character '" << c <<
"' in " << s <<
" not in alphabet=" << a << std::endl;
240 const std::size_t len1 = s1.size(), len2 = s2.size();
241 std::vector<std::vector<unsigned int>> d(len1 + 1, std::vector<unsigned int>(len2 + 1));
244 for(
unsigned int i = 1; i <= len1; ++i) d[i][0] = i;
245 for(
unsigned int i = 1; i <= len2; ++i) d[0][i] = i;
247 for(
unsigned int i = 1; i <= len1; ++i)
248 for(
unsigned int j = 1; j <= len2; ++j)
249 d[i][j] = std::min(d[i - 1][j] + 1, std::min(d[i][j - 1] + 1, d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1) ));
251 return d[len1][len2];
274 const int m = x.length();
275 const int n = y.length();
276 const int T = std::max(m,n);
277 const int E = std::max(m,n);
278 const int R = std::min(m,n);
280 const double lp_insert = -log(nalphabet);
281 const double lp_delete = log(perr);
286 mytable(0,0,0) = 0.0;
292 const auto Sab = [&](
int i,
int j) ->
double {
293 return log(perr / nalphabet + (1.0-perr)*(y[i-1]==x[j-1] ? 1 : 0));
296 for(
int t=1;t<=T;t++)
297 mytable(t,0,0) = mytable(t-1,0,0) + lp_insert;
299 for(
int d=1;d<=E;d++)
300 mytable(0,d,0) = mytable(0,d-1,0) + lp_delete;
302 for(
int s=1;s<=R;s++)
303 mytable(0,0,s) = mytable(0,0,s-1) + Sab(s-1,s-1);
305 for(
int t=1;t<=T;t++)
306 for(
int d=1;d<=E;d++)
307 mytable(t,d,0) =
logplusexp(mytable(t-1,d,0)+lp_insert,
308 mytable(t,d-1,0)+lp_delete);
310 for(
int t=1;t<=T;t++)
311 for(
int s=1;s<=n-t;s++)
312 mytable(t,0,s) =
logplusexp(mytable(t-1,0,s)+lp_insert,
313 mytable(t,0,s-1)+Sab(s+t-1,s-1));
315 for(
int d=1;d<=E;d++)
316 for(
int s=1;s<=m-d;s++)
317 mytable(0,d,s) =
logplusexp(mytable(0,d-1,s)+lp_delete,
318 mytable(0,d,s-1)+Sab(s-1,s+d-1));
320 for(
int t=1;t<=T;t++)
321 for(
int d=1;d<=E;d++)
322 for(
int s=1;s<=std::min(n-t,m-d);s++)
323 mytable(t,d,s) =
logplusexp(mytable(t-1,d,s)+lp_insert,
325 mytable(t,d,s-1)+Sab(t+s-1,d+s-1)));
329 for(
int t=std::max(0,n-m); t<=std::min(T,E+n-m);t++){
std::string QQ(const std::string &x)
Definition: Strings.h:190
std::pair< std::string, std::string > divide(const std::string &s, const char delimiter)
Definition: Strings.h:144
const std::string LAMBDA_STRING
Definition: Strings.h:19
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...
Definition: Strings.h:215
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 tha...
Definition: Strings.h:269
size_t count(const std::string &str, const std::string &sub)
Definition: Strings.h:153
std::string reverse(std::string x)
Definition: Strings.h:185
double geometric_lpdf(size_t k, double p)
Definition: Random.h:144
std::string to_string_with_precision(const T a_value, const int n=14)
Definition: Strings.h:23
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.
Definition: Strings.h:68
std::string remove_characters(std::string s, const std::string &rem)
Remove all characters in rem from s. TODO: Not very optimized here yeah.
Definition: Strings.h:82
std::string str(BindingTree *t)
Definition: BindingTree.h:195
unsigned int levenshtein_distance(const std::string &s1, const std::string &s2)
Compute levenshtein distiance between two strings (NOTE: Or O(N^2))
Definition: Strings.h:236
constexpr double infinity
Definition: Numerics.h:20
T logplusexp(const T a, const T b)
Definition: Numerics.h:131
std::string Q(const std::string &x)
Definition: Strings.h:199
Like Vector2D, but one dimension bigger. TODO: Replace this and Vector2D with a template please...
Definition: Vector3D.h:13
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 ...
Definition: Strings.h:107
double lfactorial(double x)
Definition: Numerics.h:230
const std::string LAMBDAXDOT_STRING
Definition: Strings.h:20
bool is_prefix(const T &prefix, const T &x)
Check if prefix is a prefix of x – works with iterables, including strings and vectors.
Definition: Strings.h:39
const std::string EMPTY_STRING
Definition: Strings.h:17
This is a thread_local rng whose first object is used to see others (in other threads). This way, we can have thread_local rngs that all are seeded deterministcally in Fleet via –seed=X.
bool contains(const std::string &s, const std::string &x)
Definition: Strings.h:53