Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// 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{
else if score < 0{
else {
// 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