Skip to content

Instantly share code, notes, and snippets.

@mohdsanadzakirizvi
Created November 27, 2019 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohdsanadzakirizvi/5434093f5eecdcaffdb96df9e1229f07 to your computer and use it in GitHub Desktop.
Save mohdsanadzakirizvi/5434093f5eecdcaffdb96df9e1229f07 to your computer and use it in GitHub Desktop.
// Sentiment Analysis
// Set up our input
let input = "I hate this apple pie."
// Feed it into the NaturalLanguage framework
let tagger = NLTagger(tagSchemes: [.sentimentScore])
tagger.string = input
// Ask for the results
let sentiment = tagger.tag(at: input.startIndex, unit: .paragraph, scheme: .sentimentScore).0
// Read the sentiment back and print it
let score = Double(sentiment?.rawValue ?? "0") ?? 0
// Print the right smiley based on sentiment
if score == 0{
print("🙂")
}
else if score < 0{
print("😢")
}
else {
print("😁")
}
// Print the final sentiment score
print("The sentiment score is: \(score)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment