pull/37/head
kunfei 6 years ago
parent 74ec2e0893
commit 03f888b817
  1. 41
      app/src/main/java/io/legado/app/help/BookHelp.kt

@ -8,7 +8,6 @@ import io.legado.app.utils.getPrefInt
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import org.apache.commons.text.similarity.JaccardSimilarity import org.apache.commons.text.similarity.JaccardSimilarity
import java.io.File import java.io.File
import kotlin.math.max
import kotlin.math.min import kotlin.math.min
object BookHelp { object BookHelp {
@ -94,17 +93,37 @@ object BookHelp {
if (chapters.size > index && title == chapters[index].title) { if (chapters.size > index && title == chapters[index].title) {
return index return index
} }
var similarity = 0.0
var newIndex = index var newIndex = 0
val start = max(index - 20, 0)
val end = min(index + 20, chapters.lastIndex)
val jaccardSimilarity = JaccardSimilarity() val jaccardSimilarity = JaccardSimilarity()
if (start < end) { var similarity = if (chapters.size > index) {
for (i in start..end) { jaccardSimilarity.apply(title, chapters[index].title)
val s = jaccardSimilarity.apply(title, chapters[i].title) } else 0.0
if (s > similarity) { if (similarity == 1.0) {
similarity = s return index
newIndex = i } else {
for (i in 1..50) {
if (index - 1 in chapters.indices) {
jaccardSimilarity.apply(title, chapters[index - i].title).let {
if (it > similarity) {
similarity = it
newIndex = index - i
if (similarity == 1.0) {
return newIndex
}
}
}
}
if (index + 1 in chapters.indices) {
jaccardSimilarity.apply(title, chapters[index + i].title).let {
if (it > similarity) {
similarity = it
newIndex = index + i
if (similarity == 1.0) {
return newIndex
}
}
}
} }
} }
} }

Loading…
Cancel
Save