Skip to content

Latest commit

 

History

History
289 lines (262 loc) · 20.5 KB

README.md

File metadata and controls

289 lines (262 loc) · 20.5 KB

Search with dependency pairs

examples/rust/src/bin/dependency_pairs.rs uses the dependency filter (Use the dependency filter) in search, and compares five ways to query the same index, three with the words alone and two with the pairs. lindera-tantivy has an example of the same kind. The dependencies cannot reach an index through lindera_tokenizer, so the example calls Lindera itself to make pairs of words from the dependencies, and keeps them in a table of their own. It runs with IPADIC, and with UniDic when its first argument is unidic (With UniDic). Its tables are:

  • docs: the text, in FTS5 with lindera_tokenizer. Before it loads the extension, the example sets LINDERA_CONFIG_FILE to examples/ipadic.yml, which keeps the base forms of the nouns, the verbs and the adjectives.
  • doc_pairs: the pairs of each document, one per row, in a plain table. Each bunsetsu that depends on another gives A, <word>><head word>, and B, <word>/<particle>><head word>, from the base form of the head word of each bunsetsu and the particle of the first one: 頭痛を治す gives 頭痛>治す and 頭痛/を>治す.

The 20 documents and the 3 queries are in examples/documents.tsv and examples/queries.tsv, which the examples in Rust and in Python both read. The documents are in a group for each query, and the output gives what each query looks for on its line intent: A for Q1 犬が人を噛んだ, a dog that bites a person, the dog being the subject; B for Q2 駅に近いホテル, a hotel that is near the station; and C for Q3 頭痛を治す薬, a medicine that cures a headache. A group has the text of its query, the same meaning in other words, and the words of the query in other relations, or some of them, so that the ways find different documents. The groups are on different subjects, so that each word of a query is in less than half of the documents: FTS5 floors at 1e-6 the IDF of a word that half of them or more have, and such a word would weigh nothing.

The table names the five ways by their headings in the output, which say what each is for, and gives their FTS5 queries of Q1. Each way scores a document by -bm25(docs), and orders the documents of the same score as they were added:

Way FTS5 query of Q1 Documents found
OR: every document on the subject "犬が" OR "人を" OR "噛んだ" Those with any of the words
AND: every word, in any relation "犬が" AND "人を" AND "噛んだ" Those with all of them
Phrase: the text of the query itself "犬が人を噛んだ" Those with them in the order of the query, next to each other
OR + pairs: the same relations first As OR Those of OR, with 1 added to the score for each pair A of the query that a document has, and 0.05 for each pair B, as measured (Measuring the weights)
OR, a pair B: only the documents where a word has the role of the query As OR Those of OR that have a pair B of the query, 人/を>噛む or 犬/が>噛む: a word of the query with its particle and its head

bm25() is lower for a better match, hence the minus. FTS5 analyzes each string of a MATCH with the tokenizer of the table, and a base form alone can be analyzed as another word: 嫌い, the base form of 嫌いだ, gives 嫌う when it stands alone. So OR and AND join the bunsetsu of the query as they are written, each in double quotes, and not its words. The phrase is the query whole, in double quotes. A token that the configuration drops, such as a particle, leaves no gap in FTS5, so the phrase 市販の薬 finds 市販薬の. lindera-tantivy keeps the positions of the dropped tokens, and its phrase does not (lindera/lindera-tantivy#125).

With the pairs of the query in a table query_pairs (pair, kind, weight), one statement ranks the documents for the two ways with the pairs:

WITH boosts AS (
    SELECT doc_pairs.doc AS doc,
           sum(query_pairs.weight) AS boost,
           group_concat(query_pairs.pair, ' ' ORDER BY query_pairs.kind, query_pairs.pair)
               AS matched
    FROM query_pairs JOIN doc_pairs ON doc_pairs.pair = query_pairs.pair
    GROUP BY doc_pairs.doc
)
SELECT docs.name,
       docs.text,
       -bm25(docs) AS by_words,
       -bm25(docs) + coalesce(boosts.boost, 0.0) AS with_pairs,
       coalesce(boosts.matched, '') AS matched
FROM docs LEFT JOIN boosts ON boosts.doc = docs.rowid
WHERE docs MATCH ?1
ORDER BY with_pairs DESC, docs.rowid

OR, a pair B orders by by_words and adds a condition to the WHERE clause:

AND EXISTS (
    SELECT 1
    FROM query_pairs JOIN doc_pairs ON doc_pairs.pair = query_pairs.pair
    WHERE query_pairs.kind = 'B' AND doc_pairs.doc = docs.rowid
)

OR, AND and the phrase take the same statement, ordered by by_words.

Run it with IPADIC. The target builds the extension with embed-ipadic, and the example with the features embed-ipadic and embed-dependency-ipadic of its package, examples/rust:

% make dependency-pairs
## Documents
A1 犬が人を噛んだ。
   bunsetsu: 犬が→噛んだ。  人を→噛んだ。  噛んだ。→(root)
   A: 人>噛む 犬>噛む
   B: 人/を>噛む 犬/が>噛む
A2 人を犬が噛んだ。
   bunsetsu: 人を→噛んだ。  犬が→噛んだ。  噛んだ。→(root)
   A: 人>噛む 犬>噛む
   B: 人/を>噛む 犬/が>噛む
A3 人が犬を噛んだ。
   bunsetsu: 人が→噛んだ。  犬を→噛んだ。  噛んだ。→(root)
   A: 人>噛む 犬>噛む
   B: 人/が>噛む 犬/を>噛む
A4 犬が噛んだ人が病院に運ばれた。
   bunsetsu: 犬が→噛んだ  噛んだ→人が  人が→運ばれた。  病院に→運ばれた。  運ばれた。→(root)
   A: 人>運ぶ 噛む>人 犬>噛む 病院>運ぶ
   B: 人/が>運ぶ 犬/が>噛む 病院/に>運ぶ
A5 人が犬に噛まれた。
   bunsetsu: 人が→噛まれた。  犬に→噛まれた。  噛まれた。→(root)
   A: 人>噛む 犬>噛む
   B: 人/が>噛む 犬/に>噛む
A6 犬と遊んだ人が猫に噛まれた。
   bunsetsu: 犬と→遊んだ  遊んだ→人が  人が→噛まれた。  猫に→噛まれた。  噛まれた。→(root)
   A: 人>噛む 犬>遊ぶ 猫>噛む 遊ぶ>人
   B: 人/が>噛む 犬/と>遊ぶ 猫/に>噛む
A7 猫が人を噛んだ。
   bunsetsu: 猫が→噛んだ。  人を→噛んだ。  噛んだ。→(root)
   A: 人>噛む 猫>噛む
   B: 人/を>噛む 猫/が>噛む
A8 犬が吠えた。
   bunsetsu: 犬が→吠えた。  吠えた。→(root)
   A: 犬>吠える
   B: 犬/が>吠える
B1 駅に近いホテルに泊まった。
   bunsetsu: 駅に→近い  近い→ホテルに  ホテルに→泊まった。  泊まった。→(root)
   A: ホテル>泊まる 近い>ホテル 駅>近い
   B: ホテル/に>泊まる 駅/に>近い
B2 そのホテルは駅に近い。
   bunsetsu: その→ホテルは  ホテルは→近い。  駅に→近い。  近い。→(root)
   A: その>ホテル ホテル>近い 駅>近い
   B: ホテル/は>近い 駅/に>近い
B3 駅から遠いが海に近いホテルだ。
   bunsetsu: 駅から→遠いが  遠いが→近い  海に→近い  近い→ホテルだ。  ホテルだ。→(root)
   A: 海>近い 近い>ホテル 遠い>近い 駅>遠い
   B: 海/に>近い 遠い/が>近い 駅/から>遠い
B4 駅の近くのホテルを探した。
   bunsetsu: 駅の→近くの  近くの→ホテルを  ホテルを→探した。  探した。→(root)
   A: ホテル>探す 近く>ホテル 駅>近く
   B: ホテル/を>探す 近く/の>ホテル 駅/の>近く
B5 駅に近い公園で遊んだ。
   bunsetsu: 駅に→近い  近い→公園で  公園で→遊んだ。  遊んだ。→(root)
   A: 公園>遊ぶ 近い>公園 駅>近い
   B: 公園/で>遊ぶ 駅/に>近い
B6 ホテルの朝食がおいしい。
   bunsetsu: ホテルの→朝食が  朝食が→おいしい。  おいしい。→(root)
   A: ホテル>朝食 朝食>おいしい
   B: ホテル/の>朝食 朝食/が>おいしい
C1 頭痛を治す薬を買った。
   bunsetsu: 頭痛を→治す  治す→薬を  薬を→買った。  買った。→(root)
   A: 治す>薬 薬>買う 頭痛>治す
   B: 薬/を>買う 頭痛/を>治す
C2 この薬は頭痛を治す。
   bunsetsu: この→薬は  薬は→治す。  頭痛を→治す。  治す。→(root)
   A: この>薬 薬>治す 頭痛>治す
   B: 薬/は>治す 頭痛/を>治す
C3 薬で頭痛を治した。
   bunsetsu: 薬で→治した。  頭痛を→治した。  治した。→(root)
   A: 薬>治す 頭痛>治す
   B: 薬/で>治す 頭痛/を>治す
C4 頭痛は治さずに放置した。薬は嫌いだ。
   bunsetsu: 頭痛は→治さずに  治さずに→放置した。  放置した。→(root)  薬は→嫌いだ。  嫌いだ。→(root)
   A: 治す>放置 薬>嫌い 頭痛>治す
   B: 治す/に>放置 薬/は>嫌い 頭痛/は>治す
C5 この薬を飲むと頭痛になることがある。
   bunsetsu: この→薬を  薬を→飲むと  飲むと→なることがある。  頭痛に→なることがある。  なることがある。→(root)
   A: この>薬 薬>飲む 頭痛>なる 飲む>なる
   B: 薬/を>飲む 頭痛/に>なる 飲む/と>なる
C6 頭痛に効く薬を探した。
   bunsetsu: 頭痛に→効く  効く→薬を  薬を→探した。  探した。→(root)
   A: 効く>薬 薬>探す 頭痛>効く
   B: 薬/を>探す 頭痛/に>効く

## Queries
Q1 犬が人を噛んだ
   intent: a dog bites a person (the dog is the subject)
   bunsetsu: 犬が→噛んだ  人を→噛んだ  噛んだ→(root)
   words: 犬 人 噛む
   A: 人>噛む 犬>噛む
   B: 人/を>噛む 犬/が>噛む
   [OR: every document on the subject] "犬が" OR "人を" OR "噛んだ"
      1.  1.96  A1 犬が人を噛んだ。
      2.  1.96  A2 人を犬が噛んだ。
      3.  1.96  A3 人が犬を噛んだ。
      4.  1.75  A5 人が犬に噛まれた。
      5.  1.45  A4 犬が噛んだ人が病院に運ばれた。
      6.  1.45  A6 犬と遊んだ人が猫に噛まれた。
      7.  1.30  A7 猫が人を噛んだ。
      8.  0.74  A8 犬が吠えた。
   [AND: every word, in any relation] "犬が" AND "人を" AND "噛んだ"
      1.  1.96  A1 犬が人を噛んだ。
      2.  1.96  A2 人を犬が噛んだ。
      3.  1.96  A3 人が犬を噛んだ。
      4.  1.75  A5 人が犬に噛まれた。
      5.  1.45  A4 犬が噛んだ人が病院に運ばれた。
      6.  1.45  A6 犬と遊んだ人が猫に噛まれた。
   [phrase: the text of the query itself] "犬が人を噛んだ"
      1.  2.84  A1 犬が人を噛んだ。
   [OR + pairs: the same relations first; +1 a pair A, +0.05 a pair B]
      1.  4.06  A1 犬が人を噛んだ。  (人>噛む 犬>噛む 人/を>噛む 犬/が>噛む)
      2.  4.06  A2 人を犬が噛んだ。  (人>噛む 犬>噛む 人/を>噛む 犬/が>噛む)
      3.  3.96  A3 人が犬を噛んだ。  (人>噛む 犬>噛む)
      4.  3.75  A5 人が犬に噛まれた。  (人>噛む 犬>噛む)
      5.  2.50  A4 犬が噛んだ人が病院に運ばれた。  (犬>噛む 犬/が>噛む)
      6.  2.45  A6 犬と遊んだ人が猫に噛まれた。  (人>噛む)
      7.  2.35  A7 猫が人を噛んだ。  (人>噛む 人/を>噛む)
      8.  0.74  A8 犬が吠えた。
   [OR, a pair B: only the documents where a word has the role of the query (人/を>噛む, 犬/が>噛む)]
      1.  1.96  A1 犬が人を噛んだ。  (人>噛む 犬>噛む 人/を>噛む 犬/が>噛む)
      2.  1.96  A2 人を犬が噛んだ。  (人>噛む 犬>噛む 人/を>噛む 犬/が>噛む)
      3.  1.45  A4 犬が噛んだ人が病院に運ばれた。  (犬>噛む 犬/が>噛む)
      4.  1.30  A7 猫が人を噛んだ。  (人>噛む 人/を>噛む)
Q2 駅に近いホテル
   intent: a hotel that is near the station
   bunsetsu: 駅に→近い  近い→ホテル  ホテル→(root)
   words: 駅 近い ホテル
   A: 近い>ホテル 駅>近い
   B: 駅/に>近い
   [OR: every document on the subject] "駅に" OR "近い" OR "ホテル"
      1.  3.74  B2 そのホテルは駅に近い。
      2.  3.35  B1 駅に近いホテルに泊まった。
      3.  3.04  B3 駅から遠いが海に近いホテルだ。
      4.  2.32  B5 駅に近い公園で遊んだ。
      5.  2.06  B4 駅の近くのホテルを探した。
      6.  1.15  B6 ホテルの朝食がおいしい。
   [AND: every word, in any relation] "駅に" AND "近い" AND "ホテル"
      1.  3.74  B2 そのホテルは駅に近い。
      2.  3.35  B1 駅に近いホテルに泊まった。
      3.  3.04  B3 駅から遠いが海に近いホテルだ。
   [phrase: the text of the query itself] "駅に近いホテル"
      1.  2.55  B1 駅に近いホテルに泊まった。
   [OR + pairs: the same relations first; +1 a pair A, +0.05 a pair B]
      1.  5.40  B1 駅に近いホテルに泊まった。  (近い>ホテル 駅>近い 駅/に>近い)
      2.  4.79  B2 そのホテルは駅に近い。  (駅>近い 駅/に>近い)
      3.  4.04  B3 駅から遠いが海に近いホテルだ。  (近い>ホテル)
      4.  3.37  B5 駅に近い公園で遊んだ。  (駅>近い 駅/に>近い)
      5.  2.06  B4 駅の近くのホテルを探した。
      6.  1.15  B6 ホテルの朝食がおいしい。
   [OR, a pair B: only the documents where a word has the role of the query (駅/に>近い)]
      1.  3.74  B2 そのホテルは駅に近い。  (駅>近い 駅/に>近い)
      2.  3.35  B1 駅に近いホテルに泊まった。  (近い>ホテル 駅>近い 駅/に>近い)
      3.  2.32  B5 駅に近い公園で遊んだ。  (駅>近い 駅/に>近い)
Q3 頭痛を治す薬
   intent: a medicine that cures a headache
   bunsetsu: 頭痛を→治す  治す→薬  薬→(root)
   words: 頭痛 治す 薬
   A: 治す>薬 頭痛>治す
   B: 頭痛/を>治す
   [OR: every document on the subject] "頭痛を" OR "治す" OR "薬"
      1.  3.22  C2 この薬は頭痛を治す。
      2.  3.22  C3 薬で頭痛を治した。
      3.  2.89  C1 頭痛を治す薬を買った。
      4.  2.40  C4 頭痛は治さずに放置した。薬は嫌いだ。
      5.  1.60  C6 頭痛に効く薬を探した。
      6.  1.32  C5 この薬を飲むと頭痛になることがある。
   [AND: every word, in any relation] "頭痛を" AND "治す" AND "薬"
      1.  3.22  C2 この薬は頭痛を治す。
      2.  3.22  C3 薬で頭痛を治した。
      3.  2.89  C1 頭痛を治す薬を買った。
      4.  2.40  C4 頭痛は治さずに放置した。薬は嫌いだ。
   [phrase: the text of the query itself] "頭痛を治す薬"
      1.  2.55  C1 頭痛を治す薬を買った。
   [OR + pairs: the same relations first; +1 a pair A, +0.05 a pair B]
      1.  4.94  C1 頭痛を治す薬を買った。  (治す>薬 頭痛>治す 頭痛/を>治す)
      2.  4.27  C2 この薬は頭痛を治す。  (頭痛>治す 頭痛/を>治す)
      3.  4.27  C3 薬で頭痛を治した。  (頭痛>治す 頭痛/を>治す)
      4.  3.40  C4 頭痛は治さずに放置した。薬は嫌いだ。  (頭痛>治す)
      5.  1.60  C6 頭痛に効く薬を探した。
      6.  1.32  C5 この薬を飲むと頭痛になることがある。
   [OR, a pair B: only the documents where a word has the role of the query (頭痛/を>治す)]
      1.  3.22  C2 この薬は頭痛を治す。  (頭痛>治す 頭痛/を>治す)
      2.  3.22  C3 薬で頭痛を治した。  (頭痛>治す 頭痛/を>治す)
      3.  2.89  C1 頭痛を治す薬を買った。  (治す>薬 頭痛>治す 頭痛/を>治す)

How to read it:

  • OR finds every document with a word of the query, down to A8 犬が吠えた and B6 ホテルの朝食がおいしい.
  • AND drops the documents without a word of the query: A7 and A8, B4 to B6, C5 and C6. It keeps the documents that have every word in another relation: A3, where a man bites a dog, A5 and A6, B3, a hotel near the sea and far from the station, and C4, which does not cure.
  • The phrase finds the text of the query alone: A1, B1 and C1. The same meaning in another word order, A2, B2 and C2, is not a phrase.
  • OR + pairs puts first the documents whose words relate as those of the query: A1 and A2, with the four pairs of Q1, B1, with the three of Q2, then B2, whose ホテル>近い is not the 近い>ホテル of Q2, and C1, C2 and C3. A3, where a man bites a dog, has the two pairs A of Q1 and no pair B, and stays third, 0.10 below A1 and A2: the pairs B tell the subject from the object, but a pair B adds 0.05 alone, the weight measured on JQaRA.
  • OR, a pair B keeps the documents in which a word has the particle of the query and the same head: it drops A3, A5, the passive, and C4, whose 頭痛 has は, and keeps A7 and B5, which do not have every word.

The limits, as the output shows them:

  • The passive gives other pairs: A5 人が犬に噛まれた, the event of Q1, has 犬/に>噛む and 人/が>噛む, so OR, a pair B drops it, and OR + pairs adds the pairs A of Q1 alone to it, as to A3, where a man bites a dog.
  • The pairs A do not tell negation apart: C4 頭痛は治さずに has 頭痛>治す, which OR + pairs adds to its score.
  • A pair does not need the other words of the query: B5 駅に近い公園で遊んだ, without ホテル, gains 1.05 from its pairs, as B2 does, and A7 猫が人を噛んだ, without 犬, gains 1.05, as A4 does. With these documents, neither comes above a document with every word.
  • The order that the pairs give depends on how high the words score, and that depends on the engine and on the documents. The IDF of bm25() of FTS5 is log((N - n + 0.5) / (n + 0.5)), for N documents of which n have the word, floored at 1e-6 when n is half of N or more, where Tantivy has ln(1 + (N - n + 0.5) / (n + 0.5)). With these documents, Tantivy scores the words higher (lindera/lindera-tantivy#125). The weights here are measured with FTS5 (Measuring the weights), and those of lindera-tantivy, 0.25 for a pair A and 0.5 for a pair B, with Tantivy.
  • Other words of the same meaning do not match: 近く in B4 is not 近い, and 効く in C6 is not 治す. Only OR finds them.
  • The analysis is not always right: finding the bunsetsu from the morphemes, the model for IPADIC gives the right head for 84.72% to 86.11% of them (the section "Accuracy" of the parsing docs of Lindera).

The extension itself can also index the pairs, without a table that the caller fills: see Index dependency pairs.

With UniDic

% make dependency-pairs DICTIONARY=unidic

The configuration for UniDic, examples/unidic.yml, also keeps 代名詞, 形状詞 and 接尾辞, which UniDic gives a first level of their own to, and which IPADIC tags under the nouns, the verbs and the adjectives. With these documents, the output differs from the one with IPADIC in the scores alone: the bunsetsu and the pairs are the same, and each way finds the same documents in the same order. The configuration for IPADIC keeps れる, the passive of 噛まれた and 運ばれた in A4, A5 and A6, and the one for UniDic drops it, so the documents have other lengths, and bm25() other scores. Q1 with OR + pairs, for example:

   [OR + pairs: the same relations first; +1 a pair A, +0.05 a pair B]
      1.  4.03  A1 犬が人を噛んだ。  (人>噛む 犬>噛む 人/を>噛む 犬/が>噛む)
      2.  4.03  A2 人を犬が噛んだ。  (人>噛む 犬>噛む 人/を>噛む 犬/が>噛む)
      3.  3.93  A3 人が犬を噛んだ。  (人>噛む 犬>噛む)
      4.  3.93  A5 人が犬に噛まれた。  (人>噛む 犬>噛む)
      5.  2.61  A4 犬が噛んだ人が病院に運ばれた。  (犬>噛む 犬/が>噛む)
      6.  2.56  A6 犬と遊んだ人が猫に噛まれた。  (人>噛む)
      7.  2.34  A7 猫が人を噛んだ。  (人>噛む 人/を>噛む)
      8.  0.73  A8 犬が吠えた。

Finding the bunsetsu from the morphemes, the model for unidic gives the right head for 84.81% to 86.00% of them, about as the model for IPADIC does (the section "With UniDic" of the parsing docs of Lindera).