.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)
}
private fun String.parse() =
StringEscaper.parseLine(this)
- .map(String::toLowerCase)
.map {
when {
it == "+" || it == "-" -> Phrase(it, OPTIONAL)
@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
@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
@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")
}