How to Extract Entities using NLP in Swift

Cloudmersive
2 min readJul 6, 2021

Entities are one of the more complex components of code; they’re characterized not only by relationships, but also by additional attributes, including various identifiers. Due to this complex nature, they can often be difficult to identify; in order to extract named entities without extensive research, you can use the following API in Swift, which leverages Natural Language Processing to do the hard work for you.

All you need to do to kick off the process is add your input string and API key into the below example code:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = "InputString=%3Cstring%3E"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/nlp-v2/extract-entities")!,timeoutInterval: Double.infinity)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

This will return all identified entity types and entity text. If you need to obtain an API key, you can do so by registering for a free account on the Cloudmersive website; this will provide 800 monthly calls across our entire library of APIs.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.