pull/32/head
kunfei 5 years ago
parent 8499cc7197
commit d7f66b83e0
  1. 27
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 2
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt
  3. 37
      app/src/main/java/io/legado/app/utils/StringExtensions.kt

@ -6,10 +6,13 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefInt
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import io.legado.app.utils.similarity
import java.io.BufferedWriter import java.io.BufferedWriter
import java.io.File import java.io.File
import java.io.FileWriter import java.io.FileWriter
import java.io.IOException import java.io.IOException
import kotlin.math.max
import kotlin.math.min
object BookHelp { object BookHelp {
@ -90,8 +93,28 @@ object BookHelp {
?: "" ?: ""
} }
fun getDurChapterIndexByChapterName() { fun getDurChapterIndexByChapterTitle(
title: String,
index: Int,
chapters: List<BookChapter>
): 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 var bookName: String? = null

@ -117,9 +117,9 @@ class ChangeSourceDialog : DialogFragment(),
if (book.coverUrl.isNullOrEmpty()) { if (book.coverUrl.isNullOrEmpty()) {
book.coverUrl = oldBook.getDisplayCover() book.coverUrl = oldBook.getDisplayCover()
} }
}
activity.changeTo(book) activity.changeTo(book)
} }
}
dismiss() dismiss()
} }

@ -1,5 +1,7 @@
package io.legado.app.utils package io.legado.app.utils
import kotlin.math.min
// import org.apache.commons.text.StringEscapeUtils // import org.apache.commons.text.StringEscapeUtils
fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim()
@ -34,3 +36,38 @@ fun String.splitNotBlank(regex: Regex, limit: Int = 0): Array<String> = run {
fun String.startWithIgnoreCase(start: String): Boolean { fun String.startWithIgnoreCase(start: String): Boolean {
return if (this.isBlank()) false else startsWith(start, true) return if (this.isBlank()) false else startsWith(start, true)
} }
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)
}

Loading…
Cancel
Save