Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unigram tokenizer to handle NSNumber score values #94

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Sources/Tokenizers/UnigramTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2024 Hugging Face. All rights reserved.
//

import Foundation
import Hub

class UnigramTokenizer: PreTrainedTokenizerModel {
Expand Down Expand Up @@ -37,8 +38,20 @@ class UnigramTokenizer: PreTrainedTokenizerModel {
}

vocab = try configVocab.map { piece in
guard let token = piece.first as? String else { throw TokenizerError.malformedVocab }
guard let score = piece.last as? Float else { throw TokenizerError.malformedVocab }
guard let token = piece.first as? String,
let scoreValue = piece.last else {
throw TokenizerError.malformedVocab
}

let score: Float
if let floatScore = scoreValue as? Float {
score = floatScore
} else if let numberScore = scoreValue as? NSNumber {
score = numberScore.floatValue
} else {
throw TokenizerError.malformedVocab
}

return SentencePieceToken(token: token, score: score)
}

Expand Down
Loading