Skip to content

Commit

Permalink
fixed ruby string generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Bertz committed May 14, 2024
1 parent f0abf60 commit 2dc1904
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Sources/Mecab-Swift/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,20 @@ public class Tokenizer{
*/
public func addRubyTags(to htmlText:String, transliteration:Transliteration = .hiragana, options:[Annotation.AnnotationOption] = [.kanjiOnly])->String{
let furigana=self.furiganaAnnotations(for: htmlText, transliteration: transliteration, options: options)
var searchRange:Range<String.Index> = htmlText.startIndex ..< htmlText.endIndex
var outString=htmlText
var outString=""
var endIDX = htmlText.startIndex

for annotation in furigana{
outString += htmlText[endIDX..<annotation.range.lowerBound]

if let tokenRange=outString.range(of: htmlText[annotation.range], options: [], range: searchRange, locale: nil){
let htmlRuby="<ruby>\(htmlText[annotation.range])<rt>\(annotation.reading)</rt></ruby>"
outString.replaceSubrange(tokenRange, with: htmlRuby)
searchRange = outString.index(tokenRange.lowerBound, offsetBy: htmlRuby.count) ..< outString.endIndex

}
let original = htmlText[annotation.range]
let htmlRuby="<ruby>\(original)<rt>\(annotation.reading)</rt></ruby>"
outString += htmlRuby
endIDX = annotation.range.upperBound
}

outString += htmlText[endIDX..<htmlText.endIndex]

return outString

}
Expand All @@ -193,6 +196,13 @@ public class Tokenizer{
}


//if let lowerBound = outString.index(tokenRange.lowerBound, offsetBy: htmlRuby.count, limitedBy: outString.endIndex){
// searchRange = lowerBound ..< outString.endIndex
//}
//else{
// continue
//}




Expand Down

0 comments on commit 2dc1904

Please sign in to comment.