如何在postgresql中创建n-gram

时间:2016-07-26 05:30:03

标签: postgresql n-gram trigram

我想在我的应用程序中使用搜索功能。

我使用trigram实现它并且它工作正常。

问题是:

trigram正在创建3个字符组的序列。 我希望单个对象中有超过3个字符。 例如:

select show_trgm('abcpqrs');

这将返回:{" a"," ab","abc","bcp","cpq","pqr","qrs","rs "}

我想要{" a"," ab","abc","bcp","cpq","pqr","qrs","rs ","abcd","bcpq","cpqr"...}

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

尝试此功能。

Sub SettingFormatToGeneral()
 Dim works As Worksheet
  For Each works In ActiveWorkbook.Worksheets
    Range("A:Q").Select 'specify the range which suits your purpose
    With Selection
    Selection.NumberFormat = "General"
    .Value = .Value
    End With
  Next works
End Sub

我的测试结果。

CREATE OR REPLACE FUNCTION myngram(mystr TEXT, n INT) RETURNS TEXT[]
AS $$
DECLARE
str VARCHAR;
arr TEXT[];
BEGIN

  str := lpad(mystr, n - 1 + char_length(mystr), ' ');
  str := rpad(str, n - 1 + char_length(str), ' ');

  arr := array[]::TEXT[];
  FOR i IN 1 .. char_length(str) - n + 1 LOOP
    arr := arr || substring(str from i for n);
  END LOOP;
  RETURN arr;
END
$$
LANGUAGE plpgsql;