栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

与Python + Sqlite的字符串相似度(Levenshtein距离/编辑距离)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

与Python + Sqlite的字符串相似度(Levenshtein距离/编辑距离)

这是一个现成的示例

test.py

import sqlite3db = sqlite3.connect(':memory:')db.enable_load_extension(True)db.load_extension('./spellfix')      # for Linux#db.load_extension('./spellfix.dll') # <-- UNCOMMENT HERE FOR WINDOWSdb.enable_load_extension(False)c = db.cursor()c.execute('CREATE TABLE mytable (id integer, description text)')c.execute('INSERT INTO mytable VALUES (1, "hello world, guys")')c.execute('INSERT INTO mytable VALUES (2, "hello there everybody")')c.execute('SELECt * FROM mytable WHERe editdist3(description, "hel o wrold guy") < 600')print c.fetchall()# Output: [(1, u'hello world, guys')]

重要说明:距离editdist3已标准化,因此

值100用于插入和删除,值150用于替换


这是在Windows上首先要执行的操作:

  1. 下载https://sqlite.org/2016/sqlite-src-3110100.zip,https://sqlite.org/2016/sqlite-amalgamation-3110100.zip和解压他们

  2. 从此处替换

    C:Python27DLLssqlite3.dll
    为新的sqlite3.dll。如果跳过此步骤,您将在以后得到
    sqlite3.OperationalError: The specified procedure could not be found

  3. 跑:

    call "C:Program Files (x86)Microsoft Visual Studio 12.0VCvcvarsall.bat"

要么

    call "C:Program Files (x86)Microsoft Visual Studio 12.0VCvcvarsall.bat" x64cl /I sqlite-amalgamation-3110100/ sqlite-src-3110100/ext/misc/spellfix.c /link /DLL /OUT:spellfix.dllpython test.py

(使用MinGW,这将是:

gcc -g -shared spellfix.c -I ~/sqlite-amalgation-3230100/ -ospellfix.dll

这是在Linux Debian上的方法:

apt-get -y install unzip build-essential libsqlite3-devwget https://sqlite.org/2016/sqlite-src-3110100.zipunzip sqlite-src-3110100.zipgcc -shared -fPIC -Wall -Isqlite-src-3110100 sqlite-src-3110100/ext/misc/spellfix.c -o spellfix.sopython test.py

以下是在具有旧Python版本的Linux Debian上执行此操作的方法:

如果您的发行版的Python有点旧,它将需要另一种方法。由于

sqlite3
模块是内置在Python中的,因此对其进行升级似乎并不容易(
pipinstall --upgradepysqlite
仅升级pysqlite模块,而不升级底层SQLite库)。因此,此方法例如在if
importsqlite3; print sqlite3.sqlite_version
为3.8.2的情况下有效:

wget https://www.sqlite.org/src/tarball/27392118/SQLite-27392118.tar.gztar xvfz SQLite-27392118.tar.gzcd SQLite-27392118 ; sh configure ; make sqlite3.c ; cd ..gcc -g -fPIC -shared SQLite-27392118/ext/misc/spellfix.c -I SQLite-27392118/src/ -o spellfix.sopython test.py   # [(1, u'hello world, guys')]


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/638280.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号