From fed425a3b2dbdf98623884ec79008edb053d7bfb Mon Sep 17 00:00:00 2001 From: eevee Date: Sat, 20 Jul 2024 19:03:23 +0300 Subject: [PATCH] fix for #263 --- .../Repositories/LrclibLyricsRepository.swift | 16 +++++++-------- .../Repositories/PetitLyricsRepository.swift | 9 ++++----- .../Extensions/StirngArray+Extension.swift | 20 +++++++++++++++++++ .../Models/Extensions/String+Extension.swift | 11 ---------- 4 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 Sources/EeveeSpotify/Models/Extensions/StirngArray+Extension.swift diff --git a/Sources/EeveeSpotify/Lyrics/Repositories/LrclibLyricsRepository.swift b/Sources/EeveeSpotify/Lyrics/Repositories/LrclibLyricsRepository.swift index 6719e12..6aa347d 100644 --- a/Sources/EeveeSpotify/Lyrics/Repositories/LrclibLyricsRepository.swift +++ b/Sources/EeveeSpotify/Lyrics/Repositories/LrclibLyricsRepository.swift @@ -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 ) } } diff --git a/Sources/EeveeSpotify/Lyrics/Repositories/PetitLyricsRepository.swift b/Sources/EeveeSpotify/Lyrics/Repositories/PetitLyricsRepository.swift index 824f410..2bb0f3e 100644 --- a/Sources/EeveeSpotify/Lyrics/Repositories/PetitLyricsRepository.swift +++ b/Sources/EeveeSpotify/Lyrics/Repositories/PetitLyricsRepository.swift @@ -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: diff --git a/Sources/EeveeSpotify/Models/Extensions/StirngArray+Extension.swift b/Sources/EeveeSpotify/Models/Extensions/StirngArray+Extension.swift new file mode 100644 index 0000000..11e6c61 --- /dev/null +++ b/Sources/EeveeSpotify/Models/Extensions/StirngArray+Extension.swift @@ -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 + } +} diff --git a/Sources/EeveeSpotify/Models/Extensions/String+Extension.swift b/Sources/EeveeSpotify/Models/Extensions/String+Extension.swift index a10dcd7..2a7184f 100644 --- a/Sources/EeveeSpotify/Models/Extensions/String+Extension.swift +++ b/Sources/EeveeSpotify/Models/Extensions/String+Extension.swift @@ -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)