mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
refactor(chapter): Make chapter recognition return its value
Also move it to domain module
This commit is contained in:
parent
1cd40907ef
commit
497d387c4b
3 changed files with 26 additions and 30 deletions
|
@ -0,0 +1,117 @@
|
|||
package yokai.domain.chapter.services
|
||||
|
||||
/**
|
||||
* -R> = regex conversion.
|
||||
*/
|
||||
object ChapterRecognition {
|
||||
|
||||
private const val NUMBER_PATTERN = """([0-9]+)(\.[0-9]+)?(\.?[a-z]+)?"""
|
||||
|
||||
/**
|
||||
* All cases with Ch.xx
|
||||
* Mokushiroku Alice Vol.1 Ch. 4: Misrepresentation -R> 4
|
||||
*/
|
||||
private val basic = Regex("""(?<=ch\.) *$NUMBER_PATTERN""")
|
||||
|
||||
/**
|
||||
* Example: Bleach 567: Down With Snowwhite -R> 567
|
||||
*/
|
||||
private val number = Regex(NUMBER_PATTERN)
|
||||
|
||||
/**
|
||||
* Regex used to remove unwanted tags
|
||||
* Example Prison School 12 v.1 vol004 version1243 volume64 -R> Prison School 12
|
||||
*/
|
||||
private val unwanted = Regex("""\b(?:v|ver|vol|version|volume|season|s)[^a-z]?[0-9]+""")
|
||||
|
||||
/**
|
||||
* Regex used to remove unwanted whitespace
|
||||
* Example One Piece 12 special -R> One Piece 12special
|
||||
*/
|
||||
private val unwantedWhiteSpace = Regex("""\s(?=extra|special|omake)""")
|
||||
|
||||
fun parseChapterNumber(chapterName: String, mangaTitle: String, chapterNumber: Float? = null): Float {
|
||||
// If chapter number is known return.
|
||||
if (chapterNumber != null && (chapterNumber == -2f || chapterNumber > -1f)) {
|
||||
return chapterNumber
|
||||
}
|
||||
|
||||
// Get chapter title with lower case
|
||||
var name = chapterName.lowercase()
|
||||
|
||||
// Remove manga title from chapter title.
|
||||
name = name.replace(mangaTitle.lowercase(), "").trim()
|
||||
|
||||
// Remove comma's or hyphens.
|
||||
name = name.replace(',', '.').replace('-', '.')
|
||||
|
||||
// Remove unwanted white spaces.
|
||||
name = unwantedWhiteSpace.replace(name, "")
|
||||
|
||||
// Remove unwanted tags.
|
||||
name = unwanted.replace(name, "")
|
||||
|
||||
basic.find(name)?.let { return getChapterNumberFromMatch(it) }
|
||||
|
||||
number.find(name)?.let { return getChapterNumberFromMatch(it) }
|
||||
|
||||
return chapterNumber ?: -1f
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if volume is found and return it
|
||||
* @param match result of regex
|
||||
* @return chapter number if found else null
|
||||
*/
|
||||
private fun getChapterNumberFromMatch(match: MatchResult): Float {
|
||||
return match.let {
|
||||
val initial = it.groups[1]?.value?.toFloat()!!
|
||||
val subChapterDecimal = it.groups[2]?.value
|
||||
val subChapterAlpha = it.groups[3]?.value
|
||||
val addition = checkForDecimal(subChapterDecimal, subChapterAlpha)
|
||||
initial.plus(addition)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for decimal in received strings
|
||||
* @param decimal decimal value of regex
|
||||
* @param alpha alpha value of regex
|
||||
* @return decimal/alpha float value
|
||||
*/
|
||||
private fun checkForDecimal(decimal: String?, alpha: String?): Float {
|
||||
if (!decimal.isNullOrEmpty()) {
|
||||
return decimal.toFloat()
|
||||
}
|
||||
|
||||
if (!alpha.isNullOrEmpty()) {
|
||||
if (alpha.contains("extra")) {
|
||||
return .99f
|
||||
}
|
||||
|
||||
if (alpha.contains("omake")) {
|
||||
return .98f
|
||||
}
|
||||
|
||||
if (alpha.contains("special")) {
|
||||
return .97f
|
||||
}
|
||||
|
||||
val trimmedAlpha = alpha.trimStart('.')
|
||||
if (trimmedAlpha.length == 1) {
|
||||
return parseAlphaPostFix(trimmedAlpha[0])
|
||||
}
|
||||
}
|
||||
|
||||
return .0f
|
||||
}
|
||||
|
||||
/**
|
||||
* x.a -> x.1, x.b -> x.2, etc
|
||||
*/
|
||||
private fun parseAlphaPostFix(alpha: Char): Float {
|
||||
val number = alpha.code - ('a'.code - 1)
|
||||
if (number >= 10) return 0f
|
||||
return number / 10f
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue