-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hashtags matching on note content
- Loading branch information
1 parent
46d9163
commit 57919f8
Showing
3 changed files
with
137 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
app/src/main/kotlin/net/primal/android/core/utils/HashtagMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package net.primal.android.core.utils | ||
|
||
class HashtagMatcher( | ||
content: String, | ||
hashtags: List<String>, | ||
) { | ||
|
||
private val matches: MutableList<HashtagMatch> = mutableListOf() | ||
fun matches(): List<HashtagMatch> = matches | ||
|
||
private fun containsIndex(index: Int) = matches.find { it.startIndex == index } != null | ||
|
||
private fun String.indexOfNotMatchedBefore(hashtag: String): Int? { | ||
var startIndex = 0 | ||
var foundIndex: Int? = -1 | ||
while (foundIndex == -1) { | ||
val indexOf = this.indexOf(hashtag, startIndex = startIndex) | ||
when { | ||
indexOf == -1 -> foundIndex = null | ||
containsIndex(index = indexOf) -> startIndex = indexOf + 1 | ||
else -> foundIndex = indexOf | ||
} | ||
} | ||
return foundIndex | ||
} | ||
|
||
init { | ||
hashtags.forEach { | ||
val startIndex = content.indexOfNotMatchedBefore(hashtag = it) | ||
if (startIndex != null) { | ||
matches.add( | ||
HashtagMatch( | ||
value = it, | ||
startIndex = startIndex, | ||
endIndex = startIndex + it.length, | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
data class HashtagMatch( | ||
val value: String, | ||
val startIndex: Int, | ||
val endIndex: Int, | ||
) |
46 changes: 46 additions & 0 deletions
46
app/src/test/kotlin/net/primal/android/core/utils/HashtagMatcherTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package net.primal.android.core.utils | ||
|
||
import io.kotest.matchers.collections.shouldContain | ||
import io.kotest.matchers.collections.shouldContainAll | ||
import io.kotest.matchers.collections.shouldNotContain | ||
import org.junit.Test | ||
|
||
class HashtagMatcherTest { | ||
|
||
@Test | ||
fun `matches should match all known hashtags`() { | ||
val hashtags = listOf("#TextureTuesday", "#Texture", "#Details", "#Photography") | ||
val matcher = HashtagMatcher( | ||
content = "Rusty iron chain on gray gravel. #TextureTuesday #Texture #Details #Photography", | ||
hashtags = hashtags, | ||
) | ||
|
||
val actual = matcher.matches().map { it.value } | ||
actual.shouldContainAll(hashtags) | ||
} | ||
|
||
@Test | ||
fun `matches should match hashtags which are contained in other hashtags`() { | ||
val hashtags = listOf("#TextureTuesday", "#Texture", "#Zapathon", "#Zap") | ||
val matcher = HashtagMatcher( | ||
content = "Rusty iron chain on gray gravel. #TextureTuesday #Texture #Zap #Zapathon", | ||
hashtags = hashtags, | ||
) | ||
|
||
val actual = matcher.matches().map { it.value } | ||
actual.shouldContainAll(hashtags) | ||
} | ||
|
||
@Test | ||
fun `matches should not match unknown hashtags`() { | ||
val hashtags = listOf("#Hiking", "#Trails") | ||
val matcher = HashtagMatcher( | ||
content = "The #Hiking Trails is available in Art prints with or without frame.", | ||
hashtags = hashtags, | ||
) | ||
|
||
val actual = matcher.matches().map { it.value } | ||
actual.shouldContain(hashtags.first()) | ||
actual.shouldNotContain(hashtags.last()) | ||
} | ||
} |