Language¶
The Language service analyses text with Apple's NaturalLanguage framework, on the device. Access it via app.language. Language codes are BCP 47, such as "en", "it" or "zh-Hans".
app.language.detect("Ciao, come stai?") # "it"
app.language.sentiment("This is wonderful!") # close to 1.0
Methods¶
| Method | Returns | Description |
|---|---|---|
detect(text) |
str \| None |
The most likely language, or None when it cannot tell |
hypotheses(text, count=3) |
dict[str, float] |
The most likely languages with their probabilities |
tokens(text, unit="word") |
list[str] |
Split into "word", "sentence" or "paragraph". Words come without punctuation, also for languages written without spaces |
sentiment(text) |
float |
From -1.0 (negative) to 1.0 (positive) |
distance(text, other, language=None) |
float |
How far apart two sentences are in meaning, with the system's sentence embedding: 0 is the same meaning, about 2 the farthest. The language is detected from text by default |
distance raises nib.ServiceError for a language the system has no sentence embedding for.