Fix search for Sone elements
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 23 Jan 2018 18:18:20 +0000 (19:18 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 23 Jan 2018 18:18:20 +0000 (19:18 +0100)
src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt

index e94d879..76c2341 100644 (file)
@@ -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<Phrase>.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<Phrase>): 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)
index 104006c..b0aee64 100644 (file)
@@ -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<Sone>())
-               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<Post>())
-               addHttpRequestParameter("query", "post://post-id")
-               verifyRedirect("viewPost.html?post=post-id")
+               addPost("Post-id", mock<Post>())
+               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<PostReply>().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")
        }