Trying to recognize the language of the Toot to set it correctly if none is provided (#633)
Probability threshold arbitrary set to 85%. Seems to work enough for French, English and Spanish. Also tested and with only a few words, probability is low unless there is a very language specific word
This commit is contained in:
parent
61cd21d28b
commit
2a1d1fc697
|
@ -1,6 +1,7 @@
|
|||
import DesignSystem
|
||||
import Env
|
||||
import Models
|
||||
import NaturalLanguage
|
||||
import Network
|
||||
import PhotosUI
|
||||
import SwiftUI
|
||||
|
@ -126,6 +127,21 @@ public class StatusEditorViewModel: ObservableObject {
|
|||
multiple: pollVotingFrequency.canVoteMultipleTimes,
|
||||
expires_in: pollDuration.rawValue)
|
||||
}
|
||||
|
||||
if !hasExplicitlySelectedLanguage {
|
||||
// Attempt language resolution using Natural Language
|
||||
let recognizer = NLLanguageRecognizer()
|
||||
recognizer.processString(statusText.string)
|
||||
// Use languageHypotheses to get the probability with it
|
||||
let hypotheses = recognizer.languageHypotheses(withMaximum: 1)
|
||||
// Assert that 85% probability is enough :)
|
||||
// A one word toot that is en/fr compatible is only ~50% confident, for instance
|
||||
if let (language, probability) = hypotheses.first, probability > 0.85 {
|
||||
// rawValue return the IETF BCP 47 language tag
|
||||
selectedLanguage = language.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
let data = StatusData(status: statusText.string,
|
||||
visibility: visibility,
|
||||
inReplyToId: mode.replyToStatus?.id,
|
||||
|
|
Loading…
Reference in New Issue