Guest User

Untitled

a guest
Nov 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. /**
  2. Analyze the sentiment of a given English sentence.
  3.  
  4. - Parameters:
  5. - sentence: The sentence to analyze
  6. - completion: The block to be called on the main thread upon completion
  7. - score: The sentiment score
  8. */
  9. func analyze(_ sentence: String, completion: @escaping (_ score: Int) -> Void) {
  10. // Run this asynchronously in the background
  11. DispatchQueue.global(qos: .userInitiated).async {
  12. var score = 0
  13. let jsModule = self.context.objectForKeyedSubscript("Sentimentalist")
  14. let jsAnalyzer = jsModule?.objectForKeyedSubscript("Analyzer")
  15.  
  16. // In the JSContext global values can be accessed through `objectForKeyedSubscript`.
  17. // In Objective-C you can actually write `context[@"analyze"]` but unfortunately that's
  18. // not possible in Swift yet.
  19. if let result = jsAnalyzer?.objectForKeyedSubscript("analyze").call(withArguments: [sentence]) {
  20. score = Int(result.toInt32())
  21. }
  22.  
  23. // Call the completion block on the main thread
  24. DispatchQueue.main.async {
  25. completion(score)
  26. }
  27. }
  28. }
Add Comment
Please, Sign In to add comment