Skip to content

Commit

Permalink
fix for #263
Browse files Browse the repository at this point in the history
  • Loading branch information
whoeevee committed Jul 20, 2024
1 parent c0a5310 commit fed425a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,24 @@ 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
)
}

guard let plainLyrics = song.plainLyrics else {
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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions Sources/EeveeSpotify/Models/Extensions/StirngArray+Extension.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
11 changes: 0 additions & 11 deletions Sources/EeveeSpotify/Models/Extensions/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit fed425a

Please sign in to comment.