This commit is contained in:
eevee
2024-07-20 19:03:23 +03:00
parent c0a5310afa
commit fed425a3b2
4 changed files with 31 additions and 25 deletions

View File

@@ -101,12 +101,11 @@ struct LrcLibLyricsRepository: LyricsRepository {
}
if let syncedLyrics = song.syncedLyrics {
let lines = Array(syncedLyrics.components(separatedBy: "\n").dropLast())
return LyricsDto(
lines: mapSyncedLyricsLines(
syncedLyrics.components(separatedBy: "\n").dropLast()
),
lines: mapSyncedLyricsLines(lines),
timeSynced: true,
romanization: syncedLyrics.canBeRomanized ? .canBeRomanized : .original
romanization: lines.canBeRomanized ? .canBeRomanized : .original
)
}
@@ -114,13 +113,12 @@ struct LrcLibLyricsRepository: LyricsRepository {
throw LyricsError.DecodingError
}
let lines = Array(plainLyrics.components(separatedBy: "\n").dropLast())
return LyricsDto(
lines: plainLyrics
.components(separatedBy: "\n")
.dropLast()
.map { content in LyricsLineDto(content: content) },
lines: lines.map { content in LyricsLineDto(content: content) },
timeSynced: false,
romanization: plainLyrics.canBeRomanized ? .canBeRomanized : .original
romanization: lines.canBeRomanized ? .canBeRomanized : .original
)
}
}

View File

@@ -116,20 +116,19 @@ struct PetitLyricsRepository: LyricsRepository {
)
},
timeSynced: true,
romanization: lyrics.lines.map { $0.linestring }.joined().canBeRomanized
romanization: lyrics.lines.map { $0.linestring }.canBeRomanized
? .canBeRomanized
: .original
)
case .plain:
let stringLyrics = String(data: lyricsData, encoding: .utf8)!
let lines = stringLyrics.components(separatedBy: "\n")
return LyricsDto(
lines: stringLyrics
.components(separatedBy: "\n")
.map { LyricsLineDto(content: $0) },
lines: lines.map { LyricsLineDto(content: $0) },
timeSynced: false,
romanization: stringLyrics.canBeRomanized ? .canBeRomanized : .original
romanization: lines.canBeRomanized ? .canBeRomanized : .original
)
default:

View File

@@ -0,0 +1,20 @@
import Foundation
import NaturalLanguage
extension Array where Element == String {
var canBeRomanized: Bool {
var languageList: [NLLanguage] = []
for line in self {
if let language = NLLanguageRecognizer.dominantLanguage(for: line) {
languageList.append(language)
}
}
let canBeRomanizedLanguageCount = languageList.filter {
[.japanese, .korean, .simplifiedChinese, .traditionalChinese].contains($0)
}.count
return Double(canBeRomanizedLanguageCount) / Double(languageList.count) > 0.15
}
}

View File

@@ -50,17 +50,6 @@ extension String {
["ja", "ko", "z1"].contains(self) || self.contains("zh")
}
var canBeRomanized: Bool {
let languageRecognizer = NLLanguageRecognizer()
languageRecognizer.processString(self)
if let code = languageRecognizer.dominantLanguage?.rawValue {
return code.isCanBeRomanizedLanguage
}
return false
}
var hexadecimal: Data? {
var data = Data(capacity: count / 2)