全文搜索:搜索到内容后再启用净化
This commit is contained in:
@@ -56,7 +56,7 @@ class ContentProcessor private constructor(
|
|||||||
|
|
||||||
fun getContent(
|
fun getContent(
|
||||||
book: Book,
|
book: Book,
|
||||||
chapter: BookChapter, //已经经过简繁转换
|
chapter: BookChapter,
|
||||||
content: String,
|
content: String,
|
||||||
includeTitle: Boolean = true,
|
includeTitle: Boolean = true,
|
||||||
useReplace: Boolean = true,
|
useReplace: Boolean = true,
|
||||||
@@ -83,20 +83,7 @@ class ContentProcessor private constructor(
|
|||||||
}
|
}
|
||||||
if (useReplace && book.getUseReplaceRule()) {
|
if (useReplace && book.getUseReplaceRule()) {
|
||||||
//替换
|
//替换
|
||||||
getReplaceRules().forEach { item ->
|
mContent = replaceContent(mContent)
|
||||||
if (item.pattern.isNotEmpty()) {
|
|
||||||
try {
|
|
||||||
mContent = if (item.isRegex) {
|
|
||||||
mContent.replace(item.pattern.toRegex(), item.replacement)
|
|
||||||
} else {
|
|
||||||
mContent.replace(item.pattern, item.replacement)
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
AppLog.put("${item.name}替换出错\n${e.localizedMessage}")
|
|
||||||
appCtx.toastOnUi("${item.name}替换出错")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (chineseConvert) {
|
if (chineseConvert) {
|
||||||
//简繁转换
|
//简繁转换
|
||||||
@@ -125,4 +112,23 @@ class ContentProcessor private constructor(
|
|||||||
return contents
|
return contents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun replaceContent(content: String): String {
|
||||||
|
var mContent = content
|
||||||
|
getReplaceRules().forEach { item ->
|
||||||
|
if (item.pattern.isNotEmpty()) {
|
||||||
|
try {
|
||||||
|
mContent = if (item.isRegex) {
|
||||||
|
mContent.replace(item.pattern.toRegex(), item.replacement)
|
||||||
|
} else {
|
||||||
|
mContent.replace(item.pattern, item.replacement)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
AppLog.put("${item.name}替换出错\n${e.localizedMessage}")
|
||||||
|
appCtx.toastOnUi("${item.name}替换出错")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mContent
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -21,6 +21,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
|
|||||||
var searchResultCounts = 0
|
var searchResultCounts = 0
|
||||||
val cacheChapterNames = hashSetOf<String>()
|
val cacheChapterNames = hashSetOf<String>()
|
||||||
val searchResultList: MutableList<SearchResult> = mutableListOf()
|
val searchResultList: MutableList<SearchResult> = mutableListOf()
|
||||||
|
var mContent: String = ""
|
||||||
|
|
||||||
fun initBook(bookUrl: String, success: () -> Unit) {
|
fun initBook(bookUrl: String, success: () -> Unit) {
|
||||||
this.bookUrl = bookUrl
|
this.bookUrl = bookUrl
|
||||||
@@ -40,21 +41,20 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
|
|||||||
book?.let { book ->
|
book?.let { book ->
|
||||||
val chapterContent = BookHelp.getContent(book, chapter)
|
val chapterContent = BookHelp.getContent(book, chapter)
|
||||||
if (chapterContent != null) {
|
if (chapterContent != null) {
|
||||||
//搜索替换后的正文
|
//先搜索没有启用净化的正文
|
||||||
val replaceContent: String
|
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
chapter.title = when (AppConfig.chineseConverterType) {
|
chapter.title = when (AppConfig.chineseConverterType) {
|
||||||
1 -> ChineseUtils.t2s(chapter.title)
|
1 -> ChineseUtils.t2s(chapter.title)
|
||||||
2 -> ChineseUtils.s2t(chapter.title)
|
2 -> ChineseUtils.s2t(chapter.title)
|
||||||
else -> chapter.title
|
else -> chapter.title
|
||||||
}
|
}
|
||||||
replaceContent = contentProcessor!!.getContent(
|
mContent = contentProcessor!!.getContent(
|
||||||
book, chapter, chapterContent, chineseConvert = false, reSegment = false
|
book, chapter, chapterContent, chineseConvert = false, reSegment = false, useReplace = false
|
||||||
).joinToString("")
|
).joinToString("")
|
||||||
}
|
}
|
||||||
val positions = searchPosition(replaceContent, query)
|
val positions = searchPosition(query)
|
||||||
positions.forEachIndexed { index, position ->
|
positions.forEachIndexed { index, position ->
|
||||||
val construct = getResultAndQueryIndex(replaceContent, position, query)
|
val construct = getResultAndQueryIndex(mContent, position, query)
|
||||||
val result = SearchResult(
|
val result = SearchResult(
|
||||||
resultCountWithinChapter = index,
|
resultCountWithinChapter = index,
|
||||||
resultText = construct.second,
|
resultText = construct.second,
|
||||||
@@ -73,12 +73,16 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
|
|||||||
return searchResultsWithinChapter
|
return searchResultsWithinChapter
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun searchPosition(chapterContent: String, pattern: String): List<Int> {
|
private fun searchPosition(pattern: String): List<Int> {
|
||||||
val position: MutableList<Int> = mutableListOf()
|
val position: MutableList<Int> = mutableListOf()
|
||||||
var index = chapterContent.indexOf(pattern)
|
if (mContent.indexOf(pattern) >= 0) {
|
||||||
while (index >= 0) {
|
//搜索到内容才启用净化
|
||||||
position.add(index)
|
mContent = contentProcessor.replaceContent(mContent)
|
||||||
index = chapterContent.indexOf(pattern, index + 1)
|
var index = mContent.indexOf(pattern)
|
||||||
|
while (index >= 0) {
|
||||||
|
position.add(index)
|
||||||
|
index = mContent.indexOf(pattern, index + 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return position
|
return position
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user