根据
answer to another question,在sqlite中,Levenshtein距离在名为editdist3的sql函数中实现. (比较
documentation)
╰┄┄> sqlite3 sqlite version 3.11.1 2016-03-03 16:17:53 Enter ".help" for usage hints. Connected to a transient in-memory database. Use ".open FILENAME" to reopen on a persistent database. sqlite> CREATE TABLE test (col1 TEXT); sqlite> INSERT INTO test VALUES ('foobar'); sqlite> SELECT * FROM test WHERE editdist3(col1,'f00bar') < 3; Error: no such function: editdist3
我在Gentoo Linux上使用sqlite-3.11.1(默认)USE标志icu,readline和secure-delete.
解决方法
事实证明,editdist3包含在必须明确加载的sqlite扩展中.由于我没有在Gentoo sqlite包中找到它,我也必须自己构建它.正如
documentation所说:
The spellfix1 virtual table is not included in the sqlite amalgamation
and is not a part of any standard sqlite build. It is a loadable
extension.
wget https://sqlite.org/2016/sqlite-src-3110100.zip unzip sqlite-src-3110100.zip
然后它必须编译
gcc -shared -fPIC -Wall -Isqlite-src-3110100 sqlite-src-3110100/ext/misc/spellfix.c -o spellfix.so
最后它可以加载
.load ./spellfix
要在python中使用它 – 这是我的初衷 – 需要完成以下工作:
db = sqlite3.connect(':memory:') db.enable_load_extension(True) db.load_extension('./spellfix') db.enable_load_extension(False)