CARVIEW |
Select Language
HTTP/2 200
date: Wed, 15 Oct 2025 04:26:04 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"3af7aa2472eb42d7c406f1697ff6d88a"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com github.githubassets.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com wss://alive-staging.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com marketplace-screenshots.githubusercontent.com/ copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=dFSQs57ZjXxtswiEJA9VzaQCj%2F4yGX0lR9L33GvIGmmCRod2m3OyedpVdV4W%2FJBCXVSkWMhaJKmnw91GbqNWhIORiBLRiz6go2wCMsFfBlcJUxhf9AROr2qXUw4h4vC1ee9kF88bhYO4W3FTMqv6LAagoOLdm6lyyJAYyayoUsxyLs%2BVu23sEnyBv4j1i2YDtxR%2FlVVCUY%2FXFBaFglOD2xV4%2BkpsKss1v9KmL%2FN3LJYlqYL%2B6nRaGB4K2fPMu2nuTpJ3%2FmqOE%2BIJ4TTxMd3Ueg%3D%3D--PcWZ2SFv6uksW%2BC9--FTgyyOa4y4nMhXG4KXJCVQ%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.12925977.1760502364; Path=/; Domain=github.com; Expires=Thu, 15 Oct 2026 04:26:04 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Thu, 15 Oct 2026 04:26:04 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: 84C4:7E5E8:12A2D6C:167A6B9:68EF225C
Demonstrating a bug in Peewee's bm25 function - see https://github.com/coleifer/peewee/issues/1826 · GitHub
Show Gist options
Save simonw/e0b9156d66b41b172a66d0cfe32d9391 to your computer and use it in GitHub Desktop.
{{ message }}
Instantly share code, notes, and snippets.
Last active
January 6, 2019 22:06
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save simonw/e0b9156d66b41b172a66d0cfe32d9391 to your computer and use it in GitHub Desktop.
Demonstrating a bug in Peewee's bm25 function - see https://github.com/coleifer/peewee/issues/1826
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import struct | |
import sqlite3 | |
conn = sqlite3.connect(":memory:") | |
conn.executescript(""" | |
CREATE VIRTUAL TABLE docs USING fts4(c0, c1); | |
INSERT INTO docs (c0, c1) VALUES ("this is about a dog", "more about that dog dog"); | |
INSERT INTO docs (c0, c1) VALUES ("this is about a cat", "stuff on that cat cat"); | |
INSERT INTO docs (c0, c1) VALUES ("something about a ferret", "yeah a ferret ferret"); | |
INSERT INTO docs (c0, c1) VALUES ("both of them", "both dog dog and cat here"); | |
INSERT INTO docs (c0, c1) VALUES ("not mammals", "maybe talk about fish"); | |
""") | |
def _parse_match_info(buf): | |
bufsize = len(buf) # Length in bytes. | |
return [struct.unpack('@I', buf[i:i+4])[0] for i in range(0, bufsize, 4)] | |
def bm25(match_info, *args): | |
""" | |
Usage: | |
# Format string *must* be pcnalx | |
# Second parameter to bm25 specifies the index of the column, on | |
# the table being queries. | |
bm25(matchinfo(document_tbl, 'pcnalx'), 1) AS rank | |
""" | |
K = 1.2 | |
B = 0.75 | |
score = 0.0 | |
P_O, C_O, N_O, A_O = range(4) | |
term_count = match_info[P_O] | |
col_count = match_info[C_O] | |
total_docs = match_info[N_O] | |
print("term_count={}, col_count={}, total_docs={}".format( | |
term_count, col_count, total_docs | |
)) | |
L_O = A_O + col_count | |
X_O = L_O + col_count | |
if not args: | |
weights = [1] * col_count | |
else: | |
weights = [0] * col_count | |
for i, weight in enumerate(args): | |
weights[i] = args[i] | |
for i in range(term_count): | |
for j in range(col_count): | |
weight = weights[j] | |
if weight == 0: | |
continue | |
print("term (i) = {}, column (j) = {}".format(i, j)) | |
avg_length = float(match_info[A_O + j]) | |
doc_length = float(match_info[L_O + j]) | |
print(" avg_length={}, doc_length={}".format(avg_length, doc_length)) | |
if avg_length == 0: | |
D = 0 | |
else: | |
D = 1 - B + (B * (doc_length / avg_length)) | |
x = X_O + (3 * j * (i + 1)) | |
term_frequency = float(match_info[x]) | |
docs_with_term = float(match_info[x + 2]) | |
print(" term_frequency_in_this_column={}, docs_with_term_in_this_column={}".format( | |
term_frequency, docs_with_term | |
)) | |
idf = max( | |
math.log( | |
(total_docs - docs_with_term + 0.5) / | |
(docs_with_term + 0.5)), | |
0) | |
denom = term_frequency + (K * D) | |
if denom == 0: | |
rhs = 0 | |
else: | |
rhs = (term_frequency * (K + 1)) / denom | |
score += (idf * rhs) * weight | |
return -score | |
for search in ("dog", "dog cat"): | |
results = conn.execute(""" | |
select *, matchinfo(docs, 'pcnalx') from docs | |
where docs match ? | |
""", [search]).fetchall() | |
print('search = {}'.format(search)) | |
print("============") | |
for r in results: | |
print(r[:2]) | |
print(_parse_match_info(r[-1])) | |
print(bm25(_parse_match_info(r[-1]))) | |
print() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
search = dog | |
============ | |
('this is about a dog', 'more about that dog dog') | |
[1, 2, 5, 4, 5, 5, 5, 1, 1, 1, 2, 4, 2] | |
term_count=1, col_count=2, total_docs=5 | |
term (i) = 0, column (j) = 0 | |
avg_length=4.0, doc_length=5.0 | |
term_frequency_in_this_column=1.0, docs_with_term_in_this_column=1.0 | |
term (i) = 0, column (j) = 1 | |
avg_length=5.0, doc_length=5.0 | |
term_frequency_in_this_column=2.0, docs_with_term_in_this_column=2.0 | |
-1.45932851507369 | |
('both of them', 'both dog dog and cat here') | |
[1, 2, 5, 4, 5, 3, 6, 0, 1, 1, 2, 4, 2] | |
term_count=1, col_count=2, total_docs=5 | |
term (i) = 0, column (j) = 0 | |
avg_length=4.0, doc_length=3.0 | |
term_frequency_in_this_column=0.0, docs_with_term_in_this_column=1.0 | |
term (i) = 0, column (j) = 1 | |
avg_length=5.0, doc_length=6.0 | |
term_frequency_in_this_column=2.0, docs_with_term_in_this_column=2.0 | |
-0.438011195601579 | |
search = dog cat | |
============ | |
('both of them', 'both dog dog and cat here') | |
[2, 2, 5, 4, 5, 3, 6, 0, 1, 1, 2, 4, 2, 0, 1, 1, 1, 3, 2] | |
term_count=2, col_count=2, total_docs=5 | |
term (i) = 0, column (j) = 0 | |
avg_length=4.0, doc_length=3.0 | |
term_frequency_in_this_column=0.0, docs_with_term_in_this_column=1.0 | |
term (i) = 0, column (j) = 1 | |
avg_length=5.0, doc_length=6.0 | |
term_frequency_in_this_column=2.0, docs_with_term_in_this_column=2.0 | |
term (i) = 1, column (j) = 0 | |
avg_length=4.0, doc_length=3.0 | |
term_frequency_in_this_column=0.0, docs_with_term_in_this_column=1.0 | |
term (i) = 1, column (j) = 1 | |
avg_length=5.0, doc_length=6.0 | |
term_frequency_in_this_column=0.0, docs_with_term_in_this_column=1.0 | |
-0.438011195601579 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can’t perform that action at this time.