X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FSearchPage.kt;h=76c2341416f371022678a04ac2d15ce7b2c6749c;hp=e94d8798aba8f4e8727f34a74ca092e9e5125dcf;hb=42d2da43d7a3dd0645e17c4db39252faa20a92d7;hpb=210f03a3afc928e3bdba697957daa35bd8ffe4e5 diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt index e94d879..76c2341 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt @@ -98,22 +98,19 @@ class SearchPage(template: Template, webInterface: WebInterface, ticker: Ticker .filter { PostReply.FUTURE_REPLY_FILTER.apply(it) } .map { "${soneNameCache(it.sone)} ${it.text}" }.joinToString(" ", " ")).toLowerCase() + private fun Iterable.indicesFor(text: String, predicate: (Phrase) -> Boolean) = + filter(predicate).map(Phrase::phrase).map(String::toLowerCase).flatMap { text.findAll(it) } + private fun score(text: String, phrases: Iterable): Double { val requiredPhrases = phrases.count { it.required } - val requiredHits = phrases.filter(Phrase::required) - .map(Phrase::phrase) - .flatMap { text.findAll(it) } + val requiredHits = phrases.indicesFor(text, Phrase::required) .map { Math.pow(1 - it / text.length.toDouble(), 2.0) } .sum() - val optionalHits = phrases.filter(Phrase::optional) - .map(Phrase::phrase) - .flatMap { text.findAll(it) } + val optionalHits = phrases.indicesFor(text, Phrase::optional) .map { Math.pow(1 - it / text.length.toDouble(), 2.0) } .sum() - val forbiddenHits = phrases.filter(Phrase::forbidden) - .map(Phrase::phrase) - .map { text.findAll(it).size } - .sum() + val forbiddenHits = phrases.indicesFor(text, Phrase::forbidden) + .count() return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2) } @@ -126,7 +123,6 @@ class SearchPage(template: Template, webInterface: WebInterface, ticker: Ticker private fun String.parse() = StringEscaper.parseLine(this) - .map(String::toLowerCase) .map { when { it == "+" || it == "-" -> Phrase(it, OPTIONAL)