Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions app/src/main/java/io/aatricks/novelscraper/util/TextUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@ object TextUtils {
private val SINGLE_NEWLINE_REGEX = Regex("(?<!\\n)\\n(?!\\n)")
private val TWO_PLUS_SPACES_REGEX = Regex("[ ]{2,}")

private val EXTRACT_CHAPTER_LABEL_REGEX_1 = Regex("(?i)(?:chapter|ch|ch\\.|c)\\s*(\\d+)")
private val EXTRACT_CHAPTER_LABEL_REGEX_2 = Regex("[\\s:\\-—–|](\\d+)\\s*$")
private val EXTRACT_CHAPTER_LABEL_REGEX_3 = Regex("\\b(\\d+)\\b")

private val EXTRACT_CHAPTER_LABEL_URL_PATTERNS = listOf(
Regex("chapter\\s*(\\d+)", RegexOption.IGNORE_CASE),
Regex("ch(?:apter)?\\D*(\\d+)", RegexOption.IGNORE_CASE),
Regex("/(\\d+)(?:/|$)"),
Regex("-" + "(\\d+)(?:\\D|$)")
)

private val JUNK_PATTERNS = listOf(
Regex("(?i)^read\\s+"),
Regex("(?i)\\s+free\\s+online.*\$"),
Regex("(?i)\\s+online\\s+free.*\$"),
Regex("(?i)\\s*\\|\\s*.*\$"),
Regex("(?i)\\s+at\\s+.*\$"),
Regex("(?i)[\\s–—\\-:]*(MangaBat|NovelFire|MangaPark|MangaKakalot).*\$"),
Regex("(?i)[\\s–—\\-:]*Scan.*\$")
)

private val CHAPTER_MARKER_PATTERNS = listOf(
Regex("[–—\\-:]?\\s*(?:chapter|ch|ch\\.)\\s*\\d+.*$", RegexOption.IGNORE_CASE),
Regex("\\s*[–—\\-]\\s*\\d+.*$"),
Regex("\\s*:\\s*\\d+.*$")
)

private val CLEAN_SEPARATORS_START_REGEX = Regex("^[\\s–—\\-:\\|]+")
private val CLEAN_SEPARATORS_END_REGEX = Regex("[\\s–—\\-:\\|]+$")
private val CLEAN_CHAPTER_TITLE_SUBTITLE_REGEX = Regex("(?i)(?:chapter|ch|ch\\.)\\s*\\d+[\\s:\\-—–|]+(.+)")

private val SENTENCE_ENDERS = setOf('.', '!', '?', '…', '"', '\'', '‘', '’', '“', '”', '»', ':', ';')
private val CONTINUATION_WORDS = setOf(
"of",
Expand Down Expand Up @@ -133,30 +164,16 @@ object TextUtils {
}

private fun removeCommonJunk(text: String): String {
val patterns = listOf(
Regex("(?i)^read\\s+"),
Regex("(?i)\\s+free\\s+online.*\$"),
Regex("(?i)\\s+online\\s+free.*\$"),
Regex("(?i)\\s*\\|\\s*.*\$"),
Regex("(?i)\\s+at\\s+.*\$"),
Regex("(?i)[\\s–—\\-:]*(MangaBat|NovelFire|MangaPark|MangaKakalot).*\$"),
Regex("(?i)[\\s–—\\-:]*Scan.*\$")
)
return patterns.fold(text) { acc, pattern -> acc.replace(pattern, "") }
return JUNK_PATTERNS.fold(text) { acc, pattern -> acc.replace(pattern, "") }
}

private fun removeChapterMarkers(text: String): String {
val patterns = listOf(
Regex("[–—\\-:]?\\s*(?:chapter|ch|ch\\.)\\s*\\d+.*$", RegexOption.IGNORE_CASE),
Regex("\\s*[–—\\-]\\s*\\d+.*$"),
Regex("\\s*:\\s*\\d+.*$")
)
return patterns.fold(text) { acc, pattern -> acc.replace(pattern, "").trim() }
return CHAPTER_MARKER_PATTERNS.fold(text) { acc, pattern -> acc.replace(pattern, "").trim() }
}

private fun cleanSeparators(text: String): String {
return text.replace(Regex("^[\\s–—\\-:\\|]+"), "")
.replace(Regex("[\\s–—\\-:\\|]+$"), "")
return text.replace(CLEAN_SEPARATORS_START_REGEX, "")
.replace(CLEAN_SEPARATORS_END_REGEX, "")
.trim()
}

Expand All @@ -166,15 +183,15 @@ object TextUtils {
fun extractChapterLabel(title: String?): String? {
if (title.isNullOrBlank()) return null

Regex("(?i)(?:chapter|ch|ch\\.|c)\\s*(\\d+)").find(title)?.let {
EXTRACT_CHAPTER_LABEL_REGEX_1.find(title)?.let {
return "Chapter " + it.groupValues[1]
}

Regex("[\\s:\\-—–|](\\d+)\\s*$").find(title)?.let {
EXTRACT_CHAPTER_LABEL_REGEX_2.find(title)?.let {
return "Chapter " + it.groupValues[1]
}

return Regex("\\b(\\d+)\\b").findAll(title).lastOrNull()?.let {
return EXTRACT_CHAPTER_LABEL_REGEX_3.findAll(title).lastOrNull()?.let {
"Chapter " + it.groupValues[1]
}
}
Expand All @@ -183,13 +200,7 @@ object TextUtils {
* Extract chapter label from URL
*/
fun extractChapterLabelFromUrl(url: String): String? {
val patterns = listOf(
Regex("chapter\\s*(\\d+)", RegexOption.IGNORE_CASE),
Regex("ch(?:apter)?\\D*(\\d+)", RegexOption.IGNORE_CASE),
Regex("/(\\d+)(?:/|$)"),
Regex("-" + "(\\d+)(?:\\D|$)")
)
return patterns.firstNotNullOfOrNull { r ->
return EXTRACT_CHAPTER_LABEL_URL_PATTERNS.firstNotNullOfOrNull { r ->
r.find(url)?.groupValues?.get(1)?.let { "Chapter " + it }
}
}
Expand Down Expand Up @@ -439,8 +450,7 @@ object TextUtils {
) {
val label = extractChapterLabel(cleaned)
if (label != null) {
val subTitleRegex = Regex("(?i)(?:chapter|ch|ch\\.)\\s*\\d+[\\s:\\-—–|]+(.+)")
val subTitle = subTitleRegex.find(cleaned)?.groupValues?.get(1)?.trim()
val subTitle = CLEAN_CHAPTER_TITLE_SUBTITLE_REGEX.find(cleaned)?.groupValues?.get(1)?.trim()
return if (!subTitle.isNullOrBlank() && subTitle.length > 2) (label + ": " + subTitle) else label
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.aatricks.novelscraper.util

import org.junit.Test
import kotlin.system.measureTimeMillis

class TextUtilsBenchmarkTest {

@Test
fun benchmarkExtractChapterLabel() {
val iterations = 100_000
val inputs = listOf(
"Read Chapter 233 Free Online | MangaBat",
"Chapter 175 - The End",
"The Great Mage Returns After 4000 Years: 150",
"Ch. 10",
"Just some title",
null,
""
)

// Warmup
for (i in 0 until 1000) {
inputs.forEach { TextUtils.extractChapterLabel(it) }
}

val timeTaken = measureTimeMillis {
for (i in 0 until iterations) {
inputs.forEach { TextUtils.extractChapterLabel(it) }
}
}

println("Baseline: extractChapterLabel took ${timeTaken}ms for ${iterations * inputs.size} invocations.")
}
}
Loading
Loading