* Prepare a query for matching.
(query: string, options: MatcherOptions)
| 101 | * Prepare a query for matching. |
| 102 | */ |
| 103 | function prepareQuery(query: string, options: MatcherOptions): PreparedQuery { |
| 104 | const opts: Required<MatcherOptions> = { |
| 105 | stem: options.stem ?? true, |
| 106 | removeStopwords: options.removeStopwords ?? true, |
| 107 | minLength: options.minLength ?? 1, |
| 108 | k1: options.k1 ?? 1.2, |
| 109 | b: options.b ?? 0.75, |
| 110 | exactMatchBonus: options.exactMatchBonus ?? 0.15, |
| 111 | prefixMatchBonus: options.prefixMatchBonus ?? 0.1, |
| 112 | consecutiveBonus: options.consecutiveBonus ?? 0.1, |
| 113 | positionWeight: options.positionWeight ?? 0.05, |
| 114 | }; |
| 115 | |
| 116 | const tokens = tokenize(query, opts); |
| 117 | const termSet = new Set(tokens.map((t) => t.stemmed)); |
| 118 | |
| 119 | return { |
| 120 | original: query.toLowerCase(), |
| 121 | tokens, |
| 122 | termSet, |
| 123 | options: opts, |
| 124 | }; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Calculate BM25-style score for term matching. |
no test coverage detected