我想知道是否有一种在BigQuery中测量字符串相似度的方法。
看起来这将是一个很棒的函数。
我的情况是,我需要比较两个URL的相似性,以确保它们指的是同一篇文章。
我可以找到使用JavaScript的示例,所以可能UDF是一个好方法,但我从未使用过UDF(或JavaScript)。
只是想知道是否有使用现有正则表达式函数的方法,或者是否有人能够帮助我将JavaScript示例转换为UDF。
非常感谢任何帮助。
编辑:添加一些示例代码
如果我定义了一个UDF:
// distance function
function levenshteinDistance (row, emit) {
//if (row.inputA.length <= 0 ) {var myresult = row.inputB.length};
if (typeof row.inputA === 'undefined') {var myresult = 1};
if (typeof row.inputB === 'undefined') {var myresult = 1};
//if (row.inputB.length <= 0 ) {var myresult = row.inputA.length};
var myresult = Math.min(
levenshteinDistance(row.inputA.substr(1), row.inputB) + 1,
levenshteinDistance(row.inputB.substr(1), row.inputA) + 1,
levenshteinDistance(row.inputA.substr(1), row.inputB.substr(1)) + (row.inputA[0] !== row.inputB[0] ? 1 : 0)
) + 1;
emit({outputA: myresult})
}
bigquery.defineFunction(
'levenshteinDistance', // Name of the function exported to SQL
['inputA', 'inputB'], // Names of input columns
[{'name': 'outputA', 'type': 'integer'}], // Output schema
levenshteinDistance // Reference to JavaScript UDF
);
// make a test function to test individual parts
function test(row, emit) {
if (row.inputA.length <= 0) { var x = row.inputB.length} else { var x = row.inputA.length};
emit({outputA: x});
}
bigquery.defineFunction(
'test', // Name of the function exported to SQL
['inputA', 'inputB'], // Names of input columns
[{'name': 'outputA', 'type': 'integer'}], // Output schema
test // Reference to JavaScript UDF
);
任何我尝试的测试都带有这样的查询:
SELECT outputA FROM (levenshteinDistance(SELECT "abc" AS inputA, "abd" AS inputB))
我遇到了错误:
错误:TypeError: 无法读取未定义的属性 'substr',位于第11行,38-39列 错误位置:用户定义函数
看起来可能是row.inputA不是字符串,或者由于某种原因字符串函数无法在其上工作。不确定这是否是类型问题,或者UDF默认可以使用哪些有趣的utils。
再次感谢任何帮助,谢谢。