字符串与Python的相似性+;Sqlite(Levenshtein距离/编辑距离)
Python+Sqlite中是否有可用的字符串相似性度量,例如使用字符串与Python的相似性+;Sqlite(Levenshtein距离/编辑距离),python,sqlite,string-comparison,similarity,Python,Sqlite,String Comparison,Similarity,Python+Sqlite中是否有可用的字符串相似性度量,例如使用sqlite3模块 用例示例: import sqlite3 conn = sqlite3.connect(':memory:') c = conn.cursor() c.execute('CREATE TABLE mytable (id integer, description text)') c.execute('INSERT INTO mytable VALUES (1, "hello world, guys")') c.e
sqlite3
模块
用例示例:
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.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")')
此查询应匹配ID为1的行,但不匹配ID为2的行:
c.execute('SELECT * FROM mytable WHERE dist(description, "He lo wrold gyus") < 6')
但我没有发现字符串比较有这样的“相似距离”,FTS的特征匹配
或接近
似乎没有字母变化的相似性度量,等等
有各种各样的标记化器可用,但它们相对简单。“simple”标记器简单地将每个单词拆分并小写:例如,在字符串“The quick brown fox jumps over The lazy dog”中,“jumps”一词将匹配,但不匹配“jump”。“porter”标记器更高级一些,去掉了单词的变位,这样“jumps”和“jumping”就可以匹配了,但是像“jmups”这样的打字错误就不匹配了。 遗憾的是,后者(无法找到“jmups”与“jumps”类似的事实)使我的用例不实用
- 这里是一个现成的示例
test.py
:
import sqlite3
db = sqlite3.connect(':memory:')
db.enable_load_extension(True)
db.load_extension('./spellfix') # for Linux
#db.load_extension('./spellfix.dll') # <-- UNCOMMENT HERE FOR WINDOWS
db.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')]
或
(使用MinGW,它将是:gcc-g-shared spellfix.c-I~/sqlite-amalgation-3230100/-o spellfix.dll
)
以下是如何在Linux Debian上执行此操作:
(基于)
以下是如何在Linux Debian上使用较旧的Python版本执行此操作:
如果您的发行版的Python有点旧,则需要另一种方法。由于sqlite3
模块是Python内置的,它似乎在升级它(pip安装——升级pysqlite
只会升级pysqlite模块,而不会升级底层的SQLite库)。例如,如果导入sqlite3;打印sqlite3。sqlite_版本
为3.8.2:
wget https://www.sqlite.org/src/tarball/27392118/SQLite-27392118.tar.gz
tar xvfz SQLite-27392118.tar.gz
cd SQLite-27392118 ; sh configure ; make sqlite3.c ; cd ..
gcc -g -fPIC -shared SQLite-27392118/ext/misc/spellfix.c -I SQLite-27392118/src/ -o spellfix.so
python test.py # [(1, u'hello world, guys')]
谢谢@JacquesGaudin,我修复了链接,并包含了你的MinGW版本。一直以来都是一种乐趣,我在途中学会了一点msvc命令行!写得很好:-)。请注意,只需稍加努力,您就可以将权重修改为不同于100和150。哦,这在@bodo中可能会很有趣。你试过其他重量吗?(最初我想用权重1(或100)来表示一切,就像Levenshtein距离的通常定义一样,但后来我想:100和150用作默认值可能是有原因的…)?我对此很好奇,因为我正在编写一个“您确定您输入的文本不在我们的数据库中吗?”功能[有点类似于您提问时SO的“可能已经有您的答案的问题”),因此我需要将用户输入的文本与数据库中已经存在的文本进行比较。@SL5net这是文件夹名称问题,可能与引号等一起使用。除此之外,我不知道它可能是什么
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x64
cl /I sqlite-amalgamation-3110100/ sqlite-src-3110100/ext/misc/spellfix.c /link /DLL /OUT:spellfix.dll
python test.py
apt-get -y install unzip build-essential libsqlite3-dev
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
python test.py
wget https://www.sqlite.org/src/tarball/27392118/SQLite-27392118.tar.gz
tar xvfz SQLite-27392118.tar.gz
cd SQLite-27392118 ; sh configure ; make sqlite3.c ; cd ..
gcc -g -fPIC -shared SQLite-27392118/ext/misc/spellfix.c -I SQLite-27392118/src/ -o spellfix.so
python test.py # [(1, u'hello world, guys')]