(body, searchterms)
| 157 | } |
| 158 | |
| 159 | function makeTeaser(body, searchterms) { |
| 160 | // The strategy is as follows: |
| 161 | // First, assign a value to each word in the document: |
| 162 | // Words that correspond to search terms (stemmer aware): 40 |
| 163 | // Normal words: 2 |
| 164 | // First word in a sentence: 8 |
| 165 | // Then use a sliding window with a constant number of words and count the |
| 166 | // sum of the values of the words within the window. Then use the window that got the |
| 167 | // maximum sum. If there are multiple maximas, then get the last one. |
| 168 | // Enclose the terms in <em>. |
| 169 | var stemmed_searchterms = searchterms.map(function(w) { |
| 170 | return elasticlunr.stemmer(w.toLowerCase()); |
| 171 | }); |
| 172 | var searchterm_weight = 40; |
| 173 | var weighted = []; // contains elements of ["word", weight, index_in_document] |
| 174 | // split in sentences, then words |
| 175 | var sentences = body.toLowerCase().split('. '); |
| 176 | var index = 0; |
| 177 | var value = 0; |
| 178 | var searchterm_found = false; |
| 179 | for (var sentenceindex in sentences) { |
| 180 | var words = sentences[sentenceindex].split(' '); |
| 181 | value = 8; |
| 182 | for (var wordindex in words) { |
| 183 | var word = words[wordindex]; |
| 184 | if (word.length > 0) { |
| 185 | for (var searchtermindex in stemmed_searchterms) { |
| 186 | if (elasticlunr.stemmer(word).startsWith(stemmed_searchterms[searchtermindex])) { |
| 187 | value = searchterm_weight; |
| 188 | searchterm_found = true; |
| 189 | } |
| 190 | }; |
| 191 | weighted.push([word, value, index]); |
| 192 | value = 2; |
| 193 | } |
| 194 | index += word.length; |
| 195 | index += 1; // ' ' or '.' if last word in sentence |
| 196 | }; |
| 197 | index += 1; // because we split at a two-char boundary '. ' |
| 198 | }; |
| 199 | |
| 200 | if (weighted.length == 0) { |
| 201 | return body; |
| 202 | } |
| 203 | |
| 204 | var window_weight = []; |
| 205 | var window_size = Math.min(weighted.length, results_options.teaser_word_count); |
| 206 | |
| 207 | var cur_sum = 0; |
| 208 | for (var wordindex = 0; wordindex < window_size; wordindex++) { |
| 209 | cur_sum += weighted[wordindex][1]; |
| 210 | }; |
| 211 | window_weight.push(cur_sum); |
| 212 | for (var wordindex = 0; wordindex < weighted.length - window_size; wordindex++) { |
| 213 | cur_sum -= weighted[wordindex][1]; |
| 214 | cur_sum += weighted[wordindex + window_size][1]; |
| 215 | window_weight.push(cur_sum); |
| 216 | }; |
no outgoing calls
no test coverage detected