2004-06-01 01:18:12 +08:00
|
|
|
#include "trgm.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "utils/array.h"
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
float4 trgm_limit = 0.3;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(set_limit);
|
2004-08-29 13:07:03 +08:00
|
|
|
Datum set_limit(PG_FUNCTION_ARGS);
|
2004-06-01 01:18:12 +08:00
|
|
|
Datum
|
2004-08-29 13:07:03 +08:00
|
|
|
set_limit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
float4 nlimit = PG_GETARG_FLOAT4(0);
|
|
|
|
|
|
|
|
if (nlimit < 0 || nlimit > 1.0)
|
|
|
|
elog(ERROR, "Wrong limit, should be between 0 and 1");
|
2004-06-01 01:18:12 +08:00
|
|
|
trgm_limit = nlimit;
|
|
|
|
PG_RETURN_FLOAT4(trgm_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(show_limit);
|
2004-08-29 13:07:03 +08:00
|
|
|
Datum show_limit(PG_FUNCTION_ARGS);
|
2004-06-01 01:18:12 +08:00
|
|
|
Datum
|
2004-08-29 13:07:03 +08:00
|
|
|
show_limit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
PG_RETURN_FLOAT4(trgm_limit);
|
|
|
|
}
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
#define WORDWAIT 0
|
|
|
|
#define INWORD 1
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
static int
|
2004-08-29 13:07:03 +08:00
|
|
|
comp_trgm(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return CMPTRGM(a, b);
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-08-29 13:07:03 +08:00
|
|
|
unique_array(trgm * a, int len)
|
|
|
|
{
|
|
|
|
trgm *curend,
|
|
|
|
*tmp;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
curend = tmp = a;
|
|
|
|
while (tmp - a < len)
|
2004-08-29 13:07:03 +08:00
|
|
|
if (CMPTRGM(tmp, curend))
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
curend++;
|
2004-08-29 13:07:03 +08:00
|
|
|
CPTRGM(curend, tmp);
|
2004-06-01 01:18:12 +08:00
|
|
|
tmp++;
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
2004-06-01 01:18:12 +08:00
|
|
|
tmp++;
|
2004-08-29 13:07:03 +08:00
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
return curend + 1 - a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
TRGM *
|
|
|
|
generate_trgm(char *str, int slen)
|
|
|
|
{
|
|
|
|
TRGM *trg;
|
|
|
|
char *buf,
|
|
|
|
*sptr,
|
|
|
|
*bufptr;
|
|
|
|
trgm *tptr;
|
|
|
|
int state = WORDWAIT;
|
|
|
|
int wl,
|
|
|
|
len;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
trg = (TRGM *) palloc(TRGMHRDSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
|
2004-06-01 01:18:12 +08:00
|
|
|
trg->flag = ARRKEY;
|
|
|
|
trg->len = TRGMHRDSIZE;
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (slen + LPADDING + RPADDING < 3 || slen == 0)
|
2004-06-01 01:18:12 +08:00
|
|
|
return trg;
|
|
|
|
|
|
|
|
tptr = GETARR(trg);
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
buf = palloc(sizeof(char) * (slen + 4));
|
2004-06-01 01:18:12 +08:00
|
|
|
sptr = str;
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (LPADDING > 0)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
*buf = ' ';
|
2004-08-29 13:07:03 +08:00
|
|
|
if (LPADDING > 1)
|
|
|
|
*(buf + 1) = ' ';
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
bufptr = buf + LPADDING;
|
|
|
|
while (sptr - str < slen)
|
|
|
|
{
|
|
|
|
if (state == WORDWAIT)
|
|
|
|
{
|
|
|
|
if (
|
2004-06-01 01:18:12 +08:00
|
|
|
#ifdef KEEPONLYALNUM
|
2004-08-29 13:07:03 +08:00
|
|
|
isalnum((unsigned char) *sptr)
|
2004-06-01 01:18:12 +08:00
|
|
|
#else
|
2004-08-29 13:07:03 +08:00
|
|
|
!isspace((unsigned char) *sptr)
|
2004-06-01 01:18:12 +08:00
|
|
|
#endif
|
2004-08-29 13:07:03 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
*bufptr = *sptr; /* start put word in buffer */
|
2004-06-01 01:18:12 +08:00
|
|
|
bufptr++;
|
|
|
|
state = INWORD;
|
2004-08-29 13:07:03 +08:00
|
|
|
if (sptr - str == slen - 1 /* last char */ )
|
2004-06-01 01:18:12 +08:00
|
|
|
goto gettrg;
|
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
if (
|
|
|
|
#ifdef KEEPONLYALNUM
|
2004-08-29 13:07:03 +08:00
|
|
|
!isalnum((unsigned char) *sptr)
|
2004-06-01 01:18:12 +08:00
|
|
|
#else
|
2004-08-29 13:07:03 +08:00
|
|
|
isspace((unsigned char) *sptr)
|
2004-06-01 01:18:12 +08:00
|
|
|
#endif
|
2004-08-29 13:07:03 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
gettrg:
|
2004-06-01 01:18:12 +08:00
|
|
|
/* word in buffer, so count trigrams */
|
|
|
|
*bufptr = ' ';
|
2004-08-29 13:07:03 +08:00
|
|
|
*(bufptr + 1) = ' ';
|
|
|
|
wl = bufptr - (buf + LPADDING) - 2 + LPADDING + RPADDING;
|
|
|
|
if (wl <= 0)
|
|
|
|
{
|
|
|
|
bufptr = buf + LPADDING;
|
2004-06-01 01:18:12 +08:00
|
|
|
state = WORDWAIT;
|
|
|
|
sptr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef IGNORECASE
|
2004-08-29 13:07:03 +08:00
|
|
|
do
|
|
|
|
{ /* lower word */
|
|
|
|
int wwl = bufptr - buf;
|
|
|
|
|
|
|
|
bufptr = buf + LPADDING;
|
|
|
|
while (bufptr - buf < wwl)
|
|
|
|
{
|
|
|
|
*bufptr = tolower((unsigned char) *bufptr);
|
2004-06-01 01:18:12 +08:00
|
|
|
bufptr++;
|
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
} while (0);
|
2004-06-01 01:18:12 +08:00
|
|
|
#endif
|
|
|
|
bufptr = buf;
|
|
|
|
/* set trigrams */
|
2004-08-29 13:07:03 +08:00
|
|
|
while (bufptr - buf < wl)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
CPTRGM(tptr, bufptr);
|
|
|
|
bufptr++;
|
|
|
|
tptr++;
|
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
bufptr = buf + LPADDING;
|
2004-06-01 01:18:12 +08:00
|
|
|
state = WORDWAIT;
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*bufptr = *sptr; /* put in buffer */
|
2004-06-01 01:18:12 +08:00
|
|
|
bufptr++;
|
2004-08-29 13:07:03 +08:00
|
|
|
if (sptr - str == slen - 1)
|
2004-06-01 01:18:12 +08:00
|
|
|
goto gettrg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfree(buf);
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if ((len = tptr - GETARR(trg)) == 0)
|
2004-06-01 01:18:12 +08:00
|
|
|
return trg;
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
qsort((void *) GETARR(trg), len, sizeof(trgm), comp_trgm);
|
|
|
|
len = unique_array(GETARR(trg), len);
|
|
|
|
}
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
trg->len = CALCGTSIZE(ARRKEY, len);
|
|
|
|
|
|
|
|
return trg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(show_trgm);
|
2004-08-29 13:07:03 +08:00
|
|
|
Datum show_trgm(PG_FUNCTION_ARGS);
|
2004-06-01 01:18:12 +08:00
|
|
|
Datum
|
2004-08-29 13:07:03 +08:00
|
|
|
show_trgm(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *in = PG_GETARG_TEXT_P(0);
|
|
|
|
TRGM *trg;
|
|
|
|
Datum *d;
|
|
|
|
ArrayType *a;
|
|
|
|
trgm *ptr;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
trg = generate_trgm(VARDATA(in), VARSIZE(in) - VARHDRSZ);
|
2004-08-29 13:07:03 +08:00
|
|
|
d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(trg)));
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
ptr = GETARR(trg);
|
2004-08-29 13:07:03 +08:00
|
|
|
while (ptr - GETARR(trg) < ARRNELEM(trg))
|
|
|
|
{
|
|
|
|
text *item = (text *) palloc(VARHDRSZ + 3);
|
|
|
|
|
|
|
|
VARATT_SIZEP(item) = VARHDRSZ + 3;
|
2004-06-01 01:18:12 +08:00
|
|
|
CPTRGM(VARDATA(item), ptr);
|
2004-08-29 13:07:03 +08:00
|
|
|
d[ptr - GETARR(trg)] = PointerGetDatum(item);
|
2004-06-01 01:18:12 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = construct_array(
|
2004-08-29 13:07:03 +08:00
|
|
|
d,
|
|
|
|
ARRNELEM(trg),
|
|
|
|
TEXTOID,
|
|
|
|
-1,
|
|
|
|
false,
|
|
|
|
'i'
|
|
|
|
);
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
ptr = GETARR(trg);
|
2004-08-29 13:07:03 +08:00
|
|
|
while (ptr - GETARR(trg) < ARRNELEM(trg))
|
|
|
|
{
|
|
|
|
pfree(DatumGetPointer(d[ptr - GETARR(trg)]));
|
2004-06-01 01:18:12 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
pfree(d);
|
|
|
|
pfree(trg);
|
2004-08-29 13:07:03 +08:00
|
|
|
PG_FREE_IF_COPY(in, 0);
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
float4
|
2004-08-29 13:07:03 +08:00
|
|
|
cnt_sml(TRGM * trg1, TRGM * trg2)
|
|
|
|
{
|
|
|
|
trgm *ptr1,
|
|
|
|
*ptr2;
|
|
|
|
int count = 0;
|
|
|
|
int len1,
|
|
|
|
len2;
|
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
ptr1 = GETARR(trg1);
|
|
|
|
ptr2 = GETARR(trg2);
|
2004-08-29 13:07:03 +08:00
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
len1 = ARRNELEM(trg1);
|
|
|
|
len2 = ARRNELEM(trg2);
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
|
|
|
|
{
|
|
|
|
int res = CMPTRGM(ptr1, ptr2);
|
|
|
|
|
|
|
|
if (res < 0)
|
2004-06-01 01:18:12 +08:00
|
|
|
ptr1++;
|
2004-08-29 13:07:03 +08:00
|
|
|
else if (res > 0)
|
2004-06-01 01:18:12 +08:00
|
|
|
ptr2++;
|
2004-08-29 13:07:03 +08:00
|
|
|
else
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
ptr1++;
|
|
|
|
ptr2++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DIVUNION
|
2004-08-29 13:07:03 +08:00
|
|
|
return ((((float4) count) / ((float4) (len1 + len2 - count))));
|
2004-06-01 01:18:12 +08:00
|
|
|
#else
|
2004-08-29 13:07:03 +08:00
|
|
|
return (((float) count) / ((float) ((len1 > len2) ? len1 : len2)));
|
2004-06-01 01:18:12 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(similarity);
|
2004-08-29 13:07:03 +08:00
|
|
|
Datum similarity(PG_FUNCTION_ARGS);
|
2004-06-01 01:18:12 +08:00
|
|
|
Datum
|
2004-08-29 13:07:03 +08:00
|
|
|
similarity(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *in1 = PG_GETARG_TEXT_P(0);
|
|
|
|
text *in2 = PG_GETARG_TEXT_P(1);
|
|
|
|
TRGM *trg1,
|
|
|
|
*trg2;
|
|
|
|
float4 res;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
trg1 = generate_trgm(VARDATA(in1), VARSIZE(in1) - VARHDRSZ);
|
|
|
|
trg2 = generate_trgm(VARDATA(in2), VARSIZE(in2) - VARHDRSZ);
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
res = cnt_sml(trg1, trg2);
|
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
pfree(trg1);
|
|
|
|
pfree(trg2);
|
2004-08-29 13:07:03 +08:00
|
|
|
PG_FREE_IF_COPY(in1, 0);
|
|
|
|
PG_FREE_IF_COPY(in2, 1);
|
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
PG_RETURN_FLOAT4(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(similarity_op);
|
2004-08-29 13:07:03 +08:00
|
|
|
Datum similarity_op(PG_FUNCTION_ARGS);
|
2004-06-01 01:18:12 +08:00
|
|
|
Datum
|
2004-08-29 13:07:03 +08:00
|
|
|
similarity_op(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
float4 res = DatumGetFloat4(DirectFunctionCall2(
|
|
|
|
similarity,
|
|
|
|
PG_GETARG_DATUM(0),
|
|
|
|
PG_GETARG_DATUM(1)
|
|
|
|
));
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(res >= trgm_limit);
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|