jaro.c 7.62 KB
Newer Older
eulerto's avatar
eulerto committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*----------------------------------------------------------------------------
 *
 * jaro.c
 *
 * Jaro Distance [1] is a similarity measure
 *
 *      1      m      1      m      1     m - t
 * s = --- * ----- + --- * ----- + --- * -------
 *      3     |a|     3     |b|     3       m
 *
 * where m is the number of matching characters [2], t is the number of
 * transpositions [3], |a| is the length of string a and |b| is the length of
 * string b.
 *
 * [2] two characters from a and b are considered matching iif they're not
 * farther than floor(max(|a|, |b|) / 2) - 1.
 *
 * [3] number of transpositions is the number of matchings that are in a
 * different sequence order divided by 2.
 *
 * Jaro-Winkler [4] Distance is a similarity measure
 *
 * It's an improvement over Jaro's original work. It gives more weight if the
 * initial characters are the same. So,
 *
 * w = s + (l * p * (1 - s))
 *
 * where l is the length of common prefix up to 4 characters, p is a scaling
 * factor (Winkler's suggestion is 0.1), and s is the Jaro Distance.
 *
 * For example:
 *
 * x: euler
 * y: heuser
 *
 *      1     4     1     4     1     4 - 0     4      2     1
 * s = --- * --- + --- * --- + --- * ------- = ---- + --- + --- = 0.822...
 *      3     5     3     6     3       4       15     9     3
 *
 *
 * w = 0.822 + (0 * 0.1 * (1 - 0.822)) = 0.822...
 *
 *
 * [1] Jaro, M. A. (1989). "Advances in record linking methodology as applied
 * to the 1985 census of Tampa Florida". Journal of the American Statistical
 * Society 84 (406): 414–20.
 *
 * [4] Winkler, W. E. (2006). "Overview of Record Linkage and Current Research
 * Directions". Research Report Series, RRS.
 * http://www.census.gov/srd/papers/pdf/rrs2006-02.pdf.
 *
 *
Euler Taveira's avatar
Euler Taveira committed
53
 * Copyright (c) 2008-2018, Euler Taveira de Oliveira
eulerto's avatar
eulerto committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
 *
 *----------------------------------------------------------------------------
 */

#include "similarity.h"

#include <math.h>

/* GUC variables */
double	pgs_jaro_threshold = 0.7f;
bool	pgs_jaro_is_normalized = true;
double	pgs_jarowinkler_threshold = 0.7f;
bool	pgs_jarowinkler_is_normalized = true;


69
static double _jaro(char *a, char *b)
eulerto's avatar
eulerto committed
70 71 72 73 74 75 76
{
	int		alen, blen;
	int		i, j, k;

	int		cd;		/* common window distance */
	int		cc = 0;		/* number of common characters */
	int		tr = 0;		/* number of transpositions */
77
	double	res;
eulerto's avatar
eulerto committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	int		*amatch;	/* matchs in string a; match = 1; unmatch = 0  !! USED? !!*/
	int		*bmatch;	/* matchs in string b; match = 1; unmatch = 0 */
	int		*posa;		/* positions of matched characters in a */
	int		*posb;		/* positions of matched characters in b */

	alen = strlen(a);
	blen = strlen(b);

	elog(DEBUG1, "alen: %d; blen: %d", alen, blen);

	if (alen > PGS_MAX_STR_LEN || blen > PGS_MAX_STR_LEN)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
				errmsg("argument exceeds the maximum length of %d bytes",
					PGS_MAX_STR_LEN)));

	/* if one string has zero length then return zero */
	if (alen == 0 || blen == 0)
		return 0.0;

	/*
	 * allocate 2 vectors of integers. each position will be 0 or 1 depending
	 * on the character in that position is found between common window distance.
	 */
	amatch = palloc(sizeof(int) * alen);
	bmatch = palloc(sizeof(int) * blen);

	for (i = 0; i < alen; i++)
		amatch[i] = 0;
	for (j = 0; j < blen; j++)
		bmatch[j] = 0;

	/* common window distance is floor(max(alen, blen) / 2) - 1 */
	cd = (int) floor((alen > blen ? alen : blen) / 2) - 1;
	/* catch case when alen = blen = 1 */
	if (cd < 0)
		cd = 0;

	elog(DEBUG1, "common window distance: %d", cd);

#ifdef PGS_IGNORE_CASE
	elog(DEBUG2, "case-sensitive turns off");
	for (i = 0; i < alen; i++)
		a[i] = tolower(a[i]);
	for (j = 0; j < blen; j++)
		b[j] = tolower(b[j]);
#endif

	for (i = 0; i < alen; i++)
	{
		/*
		 * calculate window test limits. limit inf to 0 and sup to blen 
		 */
		int inf = max2(i - cd, 0);
		int sup = i + cd + 1;
		sup = min2(sup, blen);

		/*
		 * no more common characters 'cause we don't have characters in b
		 * to test with characters in a
		 */
		if (inf >= sup)
			break;

		for (j = inf; j < sup; j++)
		{
			/*
			 * if found some match and it's not matched yet:
			 * (i) flag match characters in a and b
			 * (ii) increment cc
			 */
			if (bmatch[j] != 1 && a[i] == b[j])
			{
				amatch[i] = 1;
				bmatch[j] = 1;
				cc++;

				break;
			}
		}
	}

	elog(DEBUG1, "common characters: %d", cc);

	/* no common characters then return 0 */
	if (cc == 0)
		return 0.0;

	/* allocate vector of positions */
	posa = palloc(sizeof(int) * cc);
	posb = palloc(sizeof(int) * cc);
	
	k = 0;
	for (i = 0; i < alen; i++)
	{
		if (amatch[i] == 1)
		{
			posa[k] = i;
			k++;
		}
	}

	k = 0;
	for (j = 0; j < blen; j++)
	{
		if (bmatch[j] == 1)
		{
			posb[k] = j;
			k++;
		}
	}

190 191 192
	pfree(amatch);
	pfree(bmatch);

eulerto's avatar
eulerto committed
193 194 195 196 197
	/* counting half-transpositions */
	for (i = 0; i < cc; i++)
		if (a[posa[i]] != b[posb[i]])
			tr++;

198 199 200
	pfree(posa);
	pfree(posb);

eulerto's avatar
eulerto committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	elog(DEBUG1, "half transpositions: %d", tr);

	/* real number of transpositions */
	tr /= 2;

	elog(DEBUG1, "real transpositions: %d", tr);

	res = PGS_JARO_W1 * cc / alen + PGS_JARO_W2 * cc / blen + PGS_JARO_WT * (cc - tr) / cc;

	elog(DEBUG1, "jaro(%s, %s) = %f * %d / %d + %f * %d / %d + %f * (%d - %d) / %d = %f",
			a, b, PGS_JARO_W1, cc, alen, PGS_JARO_W2, cc, blen, PGS_JARO_WT, cc, tr, cc, res);

	return res;
}

PG_FUNCTION_INFO_V1(jaro);

Datum
jaro(PG_FUNCTION_ARGS)
{
	char	*a, *b;
222
	float8	res;
eulerto's avatar
eulerto committed
223 224 225 226 227 228 229 230 231 232

	a = DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0))));
	b = DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1))));

	res = _jaro(a, b);

	elog(DEBUG1, "is normalized: %d", pgs_jaro_is_normalized);
	elog(DEBUG1, "jaro(%s, %s) = %f", a, b, res);

	/* normalized and unnormalized version are the same */
233
	PG_RETURN_FLOAT8(res);
eulerto's avatar
eulerto committed
234 235 236 237 238 239
}

PG_FUNCTION_INFO_V1(jaro_op);

Datum jaro_op(PG_FUNCTION_ARGS)
{
240
	float8	res;
eulerto's avatar
eulerto committed
241 242 243 244 245 246 247 248

	/*
	 * store *_is_normalized value temporarily 'cause
	 * threshold (we're comparing against) is normalized
	 */
	bool	tmp = pgs_jaro_is_normalized;
	pgs_jaro_is_normalized = true;

249
	res = DatumGetFloat8(DirectFunctionCall2(
eulerto's avatar
eulerto committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
					jaro,
					PG_GETARG_DATUM(0),
					PG_GETARG_DATUM(1)));

	/* we're done; back to the previous value */
	pgs_jaro_is_normalized = tmp;

	PG_RETURN_BOOL(res >= pgs_jaro_threshold);
}

PG_FUNCTION_INFO_V1(jarowinkler);

Datum
jarowinkler(PG_FUNCTION_ARGS)
{
	char	*a, *b;
266
	float8	resj, res;
eulerto's avatar
eulerto committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	int	i;
	int	plen = 0;

	a = DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0))));
	b = DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1))));

	resj = _jaro(a, b);

	res = resj;

	elog(DEBUG1, "jaro(%s, %s) = %f", a, b, resj);

	if (resj > PGS_JARO_BOOST_THRESHOLD)
	{
		for (i = 0; i < strlen(a) && i < strlen(b) && i < PGS_JARO_PREFIX_SIZE; i++)
		{
			if (a[i] == b[i])
				plen++;
			else
				break;
		}

		elog(DEBUG1, "prefix length: %d", plen);

		res += PGS_JARO_SCALING_FACTOR * plen * (1.0 - resj);
	}

	elog(DEBUG1, "is normalized: %d", pgs_jarowinkler_is_normalized);
	elog(DEBUG1, "jarowinkler(%s, %s) = %f + %d * %f * (1.0 - %f) = %f",
			a, b, resj, plen, PGS_JARO_SCALING_FACTOR, resj, res);

	/* normalized and unnormalized version are the same */
299
	PG_RETURN_FLOAT8(res);
eulerto's avatar
eulerto committed
300 301 302 303 304 305
}

PG_FUNCTION_INFO_V1(jarowinkler_op);

Datum jarowinkler_op(PG_FUNCTION_ARGS)
{
306
	float8	res;
eulerto's avatar
eulerto committed
307 308 309 310 311 312 313 314

	/*
	 * store *_is_normalized value temporarily 'cause
	 * threshold (we're comparing against) is normalized
	 */
	bool	tmp = pgs_jarowinkler_is_normalized;
	pgs_jarowinkler_is_normalized = true;

315
	res = DatumGetFloat8(DirectFunctionCall2(
eulerto's avatar
eulerto committed
316 317 318 319 320 321 322 323 324
					jarowinkler,
					PG_GETARG_DATUM(0),
					PG_GETARG_DATUM(1)));

	/* we're done; back to the previous value */
	pgs_jarowinkler_is_normalized = tmp;

	PG_RETURN_BOOL(res >= pgs_jarowinkler_threshold);
}