Greetings, traveler!
Imagine that you want to create a search function that works for the specific word and words related to the same context. Apple has a handy tool for this. Unfortunately, it does not work with all languages at the time of writing this article. So, we will check out the example in English.
First, import the NaturalLanguage framework. Next, create an NLEmbedding instance using the wordEmbedding method.
import NaturalLanguage
let embedding = NLEmbedding.wordEmbedding(for: .english)
Then, specify the word which will be used for mapping.
let neighbors = embedding?.neighbors(for: "car", maximumCount: 5)
Now, let’s print out the result.
Optional([("vehicle", 0.7984305024147034), ("motorcycle", 0.7994856834411621), ("motorbike", 0.8515805006027222), ("truck", 0.8550092577934265), ("hummer", 0.8706213235855103)])
As you can see, we have an array of tuples with words similar to the original one and the distance from it. This array is sorted by distance, so the first is the closest result.
Conclusion
The NLEmbedding is an interesting tool, but we should remember that it will provide more than just synonyms. This mechanism was created to expand the search area while staying in a similar context.