【问题标题】:Postgresql: Levenshtein number for substring matchPostgresql:用于子字符串匹配的 Levenshtein 编号
【发布时间】:2020-09-22 15:30:56
【问题描述】:

问题:我正在提取长描述字段中包含“posthole”一词的行。这些经常拼写错误。我使用 Levenshtein 函数创建了一个字段来计算描述和术语“posthole”之间的差异,但它匹配整个字符串。我需要找到一种方法来修改它以计算到最接近术语“posthole”的字符串的子字符串的距离

解决方案:我唯一能想到的是将字符串拆分为空格上的子字符串,并将每个子字符串与搜索词匹配。我只是想看看是否有人知道这样做的更好方法。

目前这是纯 PostgreSQL,但如果有处理此问题的模块,我可以将一些 Python 代码插入数据库。

【问题讨论】:

    标签: python postgresql levenshtein-distance fuzzy-search


    【解决方案1】:

    您可以将字符串拆分为行:

    with inputs (id, textcol) as (
      values (1, 'this is a test of postole and some other posthole expressions'),
             (2, 'just another posthole entry')
    )
    select id, word, levenshtein(upper(word), 'POSTHOLE') 
      from inputs
           cross join lateral regexp_split_to_table(textcol, '\y') r(word) 
     where length(word) > 5
       and levenshtein(upper(word), 'POSTHOLE') < 4
    ;
    
    ┌────┬──────────┬─────────────┐
    │ id │   word   │ levenshtein │
    ├────┼──────────┼─────────────┤
    │  1 │ postole  │           1 │
    │  1 │ posthole │           0 │
    │  2 │ posthole │           0 │
    └────┴──────────┴─────────────┘
    (3 rows)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-30
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 2017-07-13
      • 1970-01-01
      相关资源
      最近更新 更多