Created
November 27, 2019 05:40
-
-
Save mohdsanadzakirizvi/5434093f5eecdcaffdb96df9e1229f07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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