diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 9fc9206d1..c121e1781 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -6,10 +6,13 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.ReplaceRule import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefString +import io.legado.app.utils.similarity import java.io.BufferedWriter import java.io.File import java.io.FileWriter import java.io.IOException +import kotlin.math.max +import kotlin.math.min object BookHelp { @@ -90,8 +93,28 @@ object BookHelp { ?: "" } - fun getDurChapterIndexByChapterName() { - + fun getDurChapterIndexByChapterTitle( + title: String, + index: Int, + chapters: List + ): Int { + if (chapters.size > index && title == chapters[index].title) { + return index + } + var similarity = 0F + var newIndex = index + val start = max(index - 10, 0) + val end = min(index + 10, chapters.size - 1) + if (start < end) { + for (i in start..end) { + val s = title.similarity(chapters[i].title) + if (s > similarity) { + similarity = s + newIndex = i + } + } + } + return newIndex } var bookName: String? = null diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt index 15e830033..b4ff6b8ba 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt @@ -117,8 +117,8 @@ class ChangeSourceDialog : DialogFragment(), if (book.coverUrl.isNullOrEmpty()) { book.coverUrl = oldBook.getDisplayCover() } + activity.changeTo(book) } - activity.changeTo(book) } dismiss() } diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index 0100b5f9f..856a6c2d9 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -1,5 +1,7 @@ package io.legado.app.utils +import kotlin.math.min + // import org.apache.commons.text.StringEscapeUtils fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() @@ -33,4 +35,39 @@ fun String.splitNotBlank(regex: Regex, limit: Int = 0): Array = run { fun String.startWithIgnoreCase(start: String): Boolean { return if (this.isBlank()) false else startsWith(start, true) -} \ No newline at end of file +} + + +fun String.similarity(target: String): Float { + //计算两个字符串的长度。 + val len1 = this.length + val len2 = target.length + //建立上面说的数组,比字符长度大一个空间 + val dif = Array(len1 + 1) { IntArray(len2 + 1) } + //赋初值,步骤B。 + for (a in 0..len1) { + dif[a][0] = a + } + for (a in 0..len2) { + dif[0][a] = a + } + //计算两个字符是否一样,计算左上的值 + var temp: Int + for (i in 1..len1) { + for (j in 1..len2) { + temp = if (this[i - 1] == target[j - 1]) { + 0 + } else { + 1 + } + //取三个值中最小的 + dif[i][j] = min( + min(dif[i - 1][j - 1] + temp, dif[i][j - 1] + 1), + dif[i - 1][j] + 1 + ) + } + } + //计算相似度 + return 1 - dif[len1][len2].toFloat() / Math.max(length, target.length) +} +