From: David ‘Bombe’ Roden Date: Tue, 23 Jan 2018 18:18:20 +0000 (+0100) Subject: Fix search for Sone elements X-Git-Tag: v79^2~179 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=42d2da43d7a3dd0645e17c4db39252faa20a92d7;p=Sone.git Fix search for Sone elements --- 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) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt index 104006c..b0aee64 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt @@ -66,9 +66,9 @@ class SearchPageTest: WebPageTest({ template, webInterface -> SearchPage(templat @Test fun `searching for sone link redirects to view sone page`() { - addSone("sone-id", mock()) - addHttpRequestParameter("query", "sone://sone-id") - verifyRedirect("viewSone.html?sone=sone-id") + addSone("Sone-ID", mock()) + addHttpRequestParameter("query", "sone://Sone-ID") + verifyRedirect("viewSone.html?sone=Sone-ID") } @Test @@ -80,9 +80,9 @@ class SearchPageTest: WebPageTest({ template, webInterface -> SearchPage(templat @Test fun `searching for a post link redirects to post page`() { - addPost("post-id", mock()) - addHttpRequestParameter("query", "post://post-id") - verifyRedirect("viewPost.html?post=post-id") + addPost("Post-id", mock()) + addHttpRequestParameter("query", "post://Post-id") + verifyRedirect("viewPost.html?post=Post-id") } @Test @@ -95,8 +95,8 @@ class SearchPageTest: WebPageTest({ template, webInterface -> SearchPage(templat @Test fun `searching for a reply link redirects to the post page`() { val postReply = mock().apply { whenever(postId).thenReturn("post-id") } - addPostReply("reply-id", postReply) - addHttpRequestParameter("query", "reply://reply-id") + addPostReply("Reply-id", postReply) + addHttpRequestParameter("query", "reply://Reply-id") verifyRedirect("viewPost.html?post=post-id") }