Detect Hate Speech from Text in Swift

Cloudmersive
1 min readMay 21, 2021

--

Hate speech detection tools have proven to be useful for limiting the amount of hate speech that appears on a given platform. If your application or website has been seeing a rise in hate speech, integrating one of these tools into your system will assist with the issue. The following API can be used in Swift to perform advanced hate speech analysis on any English text input by employing Natural Language Processing.

All you will need for the operation is the text you wish to perform the analysis on and your API key:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = "TextToAnalyze=%3Cstring%3E"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/nlp-v2/analytics/hate-speech")!,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()

And that’s it! To retrieve your API key, visit the Cloudmersive website to register for a free account; this will provide access to 800 calls/month across any of our APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet