Skip to content

Latest commit

 

History

History
206 lines (179 loc) · 19.4 KB

README.md

File metadata and controls

206 lines (179 loc) · 19.4 KB

Compare with vector search

examples/python/compare.py searches the documents and the queries of Search with dependency pairs by vector search too, on SQLite, and puts the ways side by side. Its 20 documents are handmade to show how the ways work, not how well, which Measuring the weights measures for the words and the pairs. CI does not run it.

For each query, and for each of the embedding models intfloat/multilingual-e5-base and cl-nagoya/ruri-v3-310m, it ranks the documents by:

Column Ranking
Words OR of the example: the bunsetsu of the query in FTS5 with lindera_tokenizer and ipadic.yml, by -bm25(). - for a document that it does not find
Words + pairs OR + pairs of the example: 1 added for each pair A of the query that a document has, and 0.05 for each pair B
Vector The cosine distance of the embeddings of the query and of the document in a vec0 table of sqlite-vec, with the prefixes that the card of each model gives
RRF Reciprocal rank fusion of Words + pairs and Vector: the sum of 1 / (60 + place), with nothing from Words + pairs for a document that it does not find
Top 10 by pairs The 10 nearest documents, by the number of pairs of the query that they have, the nearest first among equals. - for the others
Top 10, reranker The same 10, by the score of the cross-encoder cl-nagoya/ruri-v3-reranker-310m for the query and the document

The script makes the bunsetsu and the pairs with the Python package of Lindera, as the example does, from the IPADIC dictionary and the model of the dependency filter of the release v7.3.0 of Lindera, and loads the extension and sqlite-vec into the sqlite3 of Python. That Python has to load extensions, which sqlite3.connect(":memory:").enable_load_extension tells: the system Python of Ubuntu does, with the package of venv for it, and pyenv builds one that does with PYTHON_CONFIGURE_OPTS="--enable-loadable-sqlite-extensions". The file .python-version of the repository names Python 3.14.8 to pyenv, which the steps below were checked with: build it with that option, PYTHON_CONFIGURE_OPTS="--enable-loadable-sqlite-extensions" pyenv install 3.14.8. On Linux, from the root of the repository:

python3 -m venv .venv
.venv/bin/pip install torch==2.15.0 --index-url https://download.pytorch.org/whl/cpu
.venv/bin/pip install -r examples/python/requirements.txt
curl -LO https://github.com/lindera/lindera/releases/download/v7.3.0/lindera-ipadic-7.3.0.zip
curl -LO https://github.com/lindera/lindera/releases/download/v7.3.0/lindera-dependency-ipadic-7.3.0.zip
unzip lindera-ipadic-7.3.0.zip
unzip lindera-dependency-ipadic-7.3.0.zip
cargo build --release --features=embed-ipadic
.venv/bin/python examples/python/compare.py --extension target/release/liblindera_sqlite.so --dictionary lindera-ipadic --model lindera-dependency-ipadic/dependency.model

The first run downloads the three models, about 3.5 GB, at the revisions that the script names. It prints:

  • Embedding models: intfloat/multilingual-e5-base (d128750597153bb5987e10b1c3493a34e5a4502a), cl-nagoya/ruri-v3-310m (18b60fb8c2b9df296fb4212bb7d23ef94e579cd3)
  • Reranker: cl-nagoya/ruri-v3-reranker-310m (bb46934ee9ed09f850b9fcff17501b3ef7ddb2b3)
  • Weights of OR + pairs: 1 for a pair A, 0.05 for a pair B; N = 10; k of RRF = 60

Q1 犬が人を噛んだ: a dog bites a person (the dog is the subject). Pairs A: 人>噛む 犬>噛む; pairs B: 人/を>噛む 犬/が>噛む

multilingual-e5-base:

Document Pairs Words Words + pairs Vector RRF Top 10 by pairs Top 10, reranker
A1 犬が人を噛んだ。 人/を>噛む 人>噛む 犬/が>噛む 犬>噛む 1 (1.96) 1 (4.06) 1 (0.056) 1 (0.0328) 1 1 (0.982)
A2 人を犬が噛んだ。 人/を>噛む 人>噛む 犬/が>噛む 犬>噛む 2 (1.96) 2 (4.06) 2 (0.064) 2 (0.0323) 2 3 (0.922)
A3 人が犬を噛んだ。 人>噛む 犬>噛む 3 (1.96) 3 (3.96) 3 (0.074) 3 (0.0317) 3 5 (0.726)
A5 人が犬に噛まれた。 人>噛む 犬>噛む 4 (1.75) 4 (3.75) 4 (0.075) 4 (0.0312) 4 2 (0.923)
A4 犬が噛んだ人が病院に運ばれた。 犬/が>噛む 犬>噛む 5 (1.45) 5 (2.50) 5 (0.101) 5 (0.0308) 5 4 (0.897)
A6 犬と遊んだ人が猫に噛まれた。 人>噛む 6 (1.45) 6 (2.45) 6 (0.110) 6 (0.0303) 7 7 (0.025)
A7 猫が人を噛んだ。 人/を>噛む 人>噛む 7 (1.30) 7 (2.35) 7 (0.116) 7 (0.0299) 6 6 (0.041)
A8 犬が吠えた。 - 8 (0.74) 8 (0.74) 8 (0.123) 8 (0.0294) 8 8 (0.005)
C3 薬で頭痛を治した。 - - - 9 (0.209) 9 (0.0145) 9 10 (0.001)
B1 駅に近いホテルに泊まった。 - - - 10 (0.218) 10 (0.0143) 10 9 (0.001)
C4 頭痛は治さずに放置した。薬は嫌いだ。 - - - 11 (0.219) 11 (0.0141) - -
C1 頭痛を治す薬を買った。 - - - 12 (0.222) 12 (0.0139) - -
C6 頭痛に効く薬を探した。 - - - 13 (0.222) 13 (0.0137) - -
B5 駅に近い公園で遊んだ。 - - - 14 (0.228) 14 (0.0135) - -
C2 この薬は頭痛を治す。 - - - 15 (0.235) 15 (0.0133) - -
B4 駅の近くのホテルを探した。 - - - 16 (0.235) 16 (0.0132) - -
C5 この薬を飲むと頭痛になることがある。 - - - 17 (0.237) 17 (0.0130) - -
B2 そのホテルは駅に近い。 - - - 18 (0.243) 18 (0.0128) - -
B6 ホテルの朝食がおいしい。 - - - 19 (0.249) 19 (0.0127) - -
B3 駅から遠いが海に近いホテルだ。 - - - 20 (0.253) 20 (0.0125) - -

ruri-v3-310m:

Document Pairs Words Words + pairs Vector RRF Top 10 by pairs Top 10, reranker
A2 人を犬が噛んだ。 人/を>噛む 人>噛む 犬/が>噛む 犬>噛む 2 (1.96) 2 (4.06) 1 (0.075) 1 (0.0325) 1 3 (0.922)
A3 人が犬を噛んだ。 人>噛む 犬>噛む 3 (1.96) 3 (3.96) 2 (0.077) 3 (0.0320) 3 5 (0.726)
A1 犬が人を噛んだ。 人/を>噛む 人>噛む 犬/が>噛む 犬>噛む 1 (1.96) 1 (4.06) 3 (0.084) 2 (0.0323) 2 1 (0.982)
A5 人が犬に噛まれた。 人>噛む 犬>噛む 4 (1.75) 4 (3.75) 4 (0.085) 4 (0.0312) 4 2 (0.923)
A4 犬が噛んだ人が病院に運ばれた。 犬/が>噛む 犬>噛む 5 (1.45) 5 (2.50) 5 (0.086) 5 (0.0308) 5 4 (0.897)
A6 犬と遊んだ人が猫に噛まれた。 人>噛む 6 (1.45) 6 (2.45) 6 (0.106) 6 (0.0303) 7 7 (0.025)
A7 猫が人を噛んだ。 人/を>噛む 人>噛む 7 (1.30) 7 (2.35) 7 (0.119) 7 (0.0299) 6 6 (0.041)
A8 犬が吠えた。 - 8 (0.74) 8 (0.74) 8 (0.172) 8 (0.0294) 8 8 (0.005)
C5 この薬を飲むと頭痛になることがある。 - - - 9 (0.199) 9 (0.0145) 9 10 (0.001)
C3 薬で頭痛を治した。 - - - 10 (0.210) 10 (0.0143) 10 9 (0.001)
C2 この薬は頭痛を治す。 - - - 11 (0.212) 11 (0.0141) - -
C6 頭痛に効く薬を探した。 - - - 12 (0.214) 12 (0.0139) - -
C4 頭痛は治さずに放置した。薬は嫌いだ。 - - - 13 (0.217) 13 (0.0137) - -
C1 頭痛を治す薬を買った。 - - - 14 (0.221) 14 (0.0135) - -
B2 そのホテルは駅に近い。 - - - 15 (0.233) 15 (0.0133) - -
B4 駅の近くのホテルを探した。 - - - 16 (0.234) 16 (0.0132) - -
B1 駅に近いホテルに泊まった。 - - - 17 (0.236) 17 (0.0130) - -
B6 ホテルの朝食がおいしい。 - - - 18 (0.237) 18 (0.0128) - -
B3 駅から遠いが海に近いホテルだ。 - - - 19 (0.244) 19 (0.0127) - -
B5 駅に近い公園で遊んだ。 - - - 20 (0.257) 20 (0.0125) - -

Q2 駅に近いホテル: a hotel that is near the station. Pairs A: 近い>ホテル 駅>近い; pairs B: 駅/に>近い

multilingual-e5-base:

Document Pairs Words Words + pairs Vector RRF Top 10 by pairs Top 10, reranker
B2 そのホテルは駅に近い。 駅/に>近い 駅>近い 1 (3.74) 2 (4.79) 1 (0.091) 1 (0.0325) 2 2 (0.781)
B4 駅の近くのホテルを探した。 - 5 (2.06) 5 (2.06) 2 (0.104) 3 (0.0315) 5 4 (0.012)
B1 駅に近いホテルに泊まった。 近い>ホテル 駅/に>近い 駅>近い 2 (3.35) 1 (5.40) 3 (0.113) 2 (0.0323) 1 1 (0.785)
B3 駅から遠いが海に近いホテルだ。 近い>ホテル 3 (3.04) 3 (4.04) 4 (0.118) 4 (0.0315) 4 3 (0.018)
B5 駅に近い公園で遊んだ。 駅/に>近い 駅>近い 4 (2.32) 4 (3.37) 5 (0.153) 5 (0.0310) 3 6 (0.001)
B6 ホテルの朝食がおいしい。 - 6 (1.15) 6 (1.15) 6 (0.157) 6 (0.0303) 6 5 (0.002)
C6 頭痛に効く薬を探した。 - - - 7 (0.220) 7 (0.0149) 7 10 (0.000)
A8 犬が吠えた。 - - - 8 (0.222) 8 (0.0147) 8 7 (0.000)
C3 薬で頭痛を治した。 - - - 9 (0.225) 9 (0.0145) 9 9 (0.000)
C1 頭痛を治す薬を買った。 - - - 10 (0.225) 10 (0.0143) 10 8 (0.000)
A4 犬が噛んだ人が病院に運ばれた。 - - - 11 (0.227) 11 (0.0141) - -
A5 人が犬に噛まれた。 - - - 12 (0.229) 12 (0.0139) - -
A2 人を犬が噛んだ。 - - - 13 (0.233) 13 (0.0137) - -
C4 頭痛は治さずに放置した。薬は嫌いだ。 - - - 14 (0.233) 14 (0.0135) - -
A3 人が犬を噛んだ。 - - - 15 (0.238) 15 (0.0133) - -
A1 犬が人を噛んだ。 - - - 16 (0.240) 16 (0.0132) - -
A6 犬と遊んだ人が猫に噛まれた。 - - - 17 (0.240) 17 (0.0130) - -
C2 この薬は頭痛を治す。 - - - 18 (0.246) 18 (0.0128) - -
A7 猫が人を噛んだ。 - - - 19 (0.247) 19 (0.0127) - -
C5 この薬を飲むと頭痛になることがある。 - - - 20 (0.259) 20 (0.0125) - -

ruri-v3-310m:

Document Pairs Words Words + pairs Vector RRF Top 10 by pairs Top 10, reranker
B2 そのホテルは駅に近い。 駅/に>近い 駅>近い 1 (3.74) 2 (4.79) 1 (0.057) 1 (0.0325) 2 2 (0.781)
B3 駅から遠いが海に近いホテルだ。 近い>ホテル 3 (3.04) 3 (4.04) 2 (0.099) 3 (0.0320) 4 3 (0.018)
B1 駅に近いホテルに泊まった。 近い>ホテル 駅/に>近い 駅>近い 2 (3.35) 1 (5.40) 3 (0.102) 2 (0.0323) 1 1 (0.785)
B4 駅の近くのホテルを探した。 - 5 (2.06) 5 (2.06) 4 (0.117) 4 (0.0310) 5 4 (0.012)
B6 ホテルの朝食がおいしい。 - 6 (1.15) 6 (1.15) 5 (0.161) 6 (0.0305) 6 5 (0.002)
B5 駅に近い公園で遊んだ。 駅/に>近い 駅>近い 4 (2.32) 4 (3.37) 6 (0.191) 5 (0.0308) 3 6 (0.001)
C5 この薬を飲むと頭痛になることがある。 - - - 7 (0.249) 7 (0.0149) 7 9 (0.000)
C2 この薬は頭痛を治す。 - - - 8 (0.250) 8 (0.0147) 8 7 (0.000)
C3 薬で頭痛を治した。 - - - 9 (0.254) 9 (0.0145) 9 8 (0.000)
C6 頭痛に効く薬を探した。 - - - 10 (0.255) 10 (0.0143) 10 10 (0.000)
A8 犬が吠えた。 - - - 11 (0.255) 11 (0.0141) - -
C4 頭痛は治さずに放置した。薬は嫌いだ。 - - - 12 (0.263) 12 (0.0139) - -
A4 犬が噛んだ人が病院に運ばれた。 - - - 13 (0.266) 13 (0.0137) - -
C1 頭痛を治す薬を買った。 - - - 14 (0.268) 14 (0.0135) - -
A1 犬が人を噛んだ。 - - - 15 (0.276) 15 (0.0133) - -
A5 人が犬に噛まれた。 - - - 16 (0.276) 16 (0.0132) - -
A2 人を犬が噛んだ。 - - - 17 (0.281) 17 (0.0130) - -
A3 人が犬を噛んだ。 - - - 18 (0.288) 18 (0.0128) - -
A7 猫が人を噛んだ。 - - - 19 (0.292) 19 (0.0127) - -
A6 犬と遊んだ人が猫に噛まれた。 - - - 20 (0.301) 20 (0.0125) - -

Q3 頭痛を治す薬: a medicine that cures a headache. Pairs A: 治す>薬 頭痛>治す; pairs B: 頭痛/を>治す

multilingual-e5-base:

Document Pairs Words Words + pairs Vector RRF Top 10 by pairs Top 10, reranker
C6 頭痛に効く薬を探した。 - 5 (1.60) 5 (1.60) 1 (0.108) 3 (0.0318) 5 5 (0.086)
C1 頭痛を治す薬を買った。 治す>薬 頭痛/を>治す 頭痛>治す 3 (2.89) 1 (4.94) 2 (0.113) 1 (0.0325) 1 2 (0.645)
C2 この薬は頭痛を治す。 頭痛/を>治す 頭痛>治す 1 (3.22) 2 (4.27) 3 (0.115) 2 (0.0320) 2 1 (0.852)
C3 薬で頭痛を治した。 頭痛/を>治す 頭痛>治す 2 (3.22) 3 (4.27) 4 (0.115) 4 (0.0315) 3 3 (0.575)
C4 頭痛は治さずに放置した。薬は嫌いだ。 頭痛>治す 4 (2.40) 4 (3.40) 5 (0.142) 5 (0.0310) 4 4 (0.178)
C5 この薬を飲むと頭痛になることがある。 - 6 (1.32) 6 (1.32) 6 (0.150) 6 (0.0303) 6 6 (0.029)
A8 犬が吠えた。 - - - 7 (0.196) 7 (0.0149) 7 10 (0.000)
A4 犬が噛んだ人が病院に運ばれた。 - - - 8 (0.205) 8 (0.0147) 8 7 (0.001)
B1 駅に近いホテルに泊まった。 - - - 9 (0.208) 9 (0.0145) 9 8 (0.001)
B4 駅の近くのホテルを探した。 - - - 10 (0.214) 10 (0.0143) 10 9 (0.001)
B5 駅に近い公園で遊んだ。 - - - 11 (0.215) 11 (0.0141) - -
B3 駅から遠いが海に近いホテルだ。 - - - 12 (0.220) 12 (0.0139) - -
A2 人を犬が噛んだ。 - - - 13 (0.223) 13 (0.0137) - -
A6 犬と遊んだ人が猫に噛まれた。 - - - 14 (0.226) 14 (0.0135) - -
A7 猫が人を噛んだ。 - - - 15 (0.227) 15 (0.0133) - -
A5 人が犬に噛まれた。 - - - 16 (0.227) 16 (0.0132) - -
B6 ホテルの朝食がおいしい。 - - - 17 (0.227) 17 (0.0130) - -
A1 犬が人を噛んだ。 - - - 18 (0.228) 18 (0.0128) - -
B2 そのホテルは駅に近い。 - - - 19 (0.230) 19 (0.0127) - -
A3 人が犬を噛んだ。 - - - 20 (0.234) 20 (0.0125) - -

ruri-v3-310m:

Document Pairs Words Words + pairs Vector RRF Top 10 by pairs Top 10, reranker
C2 この薬は頭痛を治す。 頭痛/を>治す 頭痛>治す 1 (3.22) 2 (4.27) 1 (0.068) 1 (0.0325) 2 1 (0.852)
C5 この薬を飲むと頭痛になることがある。 - 6 (1.32) 6 (1.32) 2 (0.105) 4 (0.0313) 5 6 (0.029)
C3 薬で頭痛を治した。 頭痛/を>治す 頭痛>治す 2 (3.22) 3 (4.27) 3 (0.112) 3 (0.0317) 3 3 (0.575)
C1 頭痛を治す薬を買った。 治す>薬 頭痛/を>治す 頭痛>治す 3 (2.89) 1 (4.94) 4 (0.117) 2 (0.0320) 1 2 (0.645)
C4 頭痛は治さずに放置した。薬は嫌いだ。 頭痛>治す 4 (2.40) 4 (3.40) 5 (0.123) 5 (0.0310) 4 4 (0.178)
C6 頭痛に効く薬を探した。 - 5 (1.60) 5 (1.60) 6 (0.141) 6 (0.0305) 6 5 (0.086)
A4 犬が噛んだ人が病院に運ばれた。 - - - 7 (0.221) 7 (0.0149) 7 7 (0.001)
A5 人が犬に噛まれた。 - - - 8 (0.226) 8 (0.0147) 8 10 (0.001)
A1 犬が人を噛んだ。 - - - 9 (0.233) 9 (0.0145) 9 8 (0.001)
A2 人を犬が噛んだ。 - - - 10 (0.237) 10 (0.0143) 10 9 (0.001)
A3 人が犬を噛んだ。 - - - 11 (0.238) 11 (0.0141) - -
B6 ホテルの朝食がおいしい。 - - - 12 (0.239) 12 (0.0139) - -
B2 そのホテルは駅に近い。 - - - 13 (0.240) 13 (0.0137) - -
A7 猫が人を噛んだ。 - - - 14 (0.244) 14 (0.0135) - -
A6 犬と遊んだ人が猫に噛まれた。 - - - 15 (0.246) 15 (0.0133) - -
B1 駅に近いホテルに泊まった。 - - - 16 (0.248) 16 (0.0132) - -
B3 駅から遠いが海に近いホテルだ。 - - - 17 (0.250) 17 (0.0130) - -
A8 犬が吠えた。 - - - 18 (0.253) 18 (0.0128) - -
B4 駅の近くのホテルを探した。 - - - 19 (0.256) 19 (0.0127) - -
B5 駅に近い公園で遊んだ。 - - - 20 (0.268) 20 (0.0125) - -

How to read it:

  • Each vector search ranks every document, and puts the documents of the group of the query first, as the words find them: at a distance of 0.191 or less, and the others at 0.196 or more.
  • The vector search does not tell the roles apart in Q1: A3, where a man bites a dog, is third with multilingual-e5-base, 0.018 behind A1, and second with ruri-v3-310m, above A1.
  • The reranker does: it scores A1, A5, A2 and A4, where a dog bites a person, from 0.897 to 0.982, A3 0.726, and the documents where another animal bites, or the dog does something else, below 0.05. It ranks A5 人が犬に噛まれた, the same event in the passive, second, where the pairs miss it: its pairs are 犬/に>噛む and 人/が>噛む.
  • C6 頭痛に効く薬を探した, the same meaning as Q3 with 効く, which no pair of the query matches, is first by the vector search with multilingual-e5-base, and sixth with ruri-v3-310m. The reranker puts it fifth, at 0.086, below C4, which does not cure, at 0.178. ruri-v3-310m puts C5, where the medicine gives a headache, second.
  • In Q2, the reranker sets B1 and B2, a hotel near the station, at 0.785 and 0.781, apart from the others, at 0.018 or less. The vector search puts B4 駅の近くのホテル, with 近く, second with multilingual-e5-base, and B3, a hotel near the sea and far from the station, second with ruri-v3-310m. Top 10 by pairs puts B5, a park near the station, third, by its pairs of 駅に近い.
  • RRF puts the documents that the words find first, as they get a share of both rankings, and orders them by both: in Q3 with multilingual-e5-base, C6 is third, between its first place by the vector search and its fifth by Words + pairs.
  • The two last ways reorder the 10 nearest documents only. Here, these hold every document of the group of the query: with more documents, one out of the 10 nearest stays out, where RRF can bring it back by the words.

The models are under their own licenses, MIT for multilingual-e5-base and Apache-2.0 for the two of ruri-v3, sqlite-vec is under MIT or Apache-2.0, and the dictionary and the model of Lindera come with their NOTICE.txt. The script downloads the models, and the steps above the dictionary and the model of Lindera: none of them is in this repository.