-
-
Save anupamchugh/c13414d4064adc24b29d6823a6343d75 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
struct ReviewDetail: View { | |
var review: String | |
@State var mlPrediction = "" | |
@State var nlpResult = "" | |
let tagger = NLTagger(tagSchemes: [.sentimentScore]) | |
var reviewPredictor : NLModel? | |
var body: some View { | |
return VStack{ | |
Text(review) | |
Button(action:{ | |
self.tagger.string = self.review | |
if let string = self.tagger.string{ | |
let (sentiment,_) = self.tagger.tag(at: string.startIndex, unit: .paragraph, scheme: .sentimentScore) | |
self.nlpResult = "\(sentiment?.rawValue ?? "")" | |
} | |
}){ | |
Text("Run Sentiment Analysis") | |
} | |
Text(self.nlpResult) | |
Button(action:{ | |
self.runPrediction() | |
}){ | |
Text("Run Core ML Prediction") | |
} | |
Text(self.mlPrediction) | |
} | |
} | |
func runPrediction(){ | |
do{ | |
let reviewPredictor = try NLModel(mlModel: ReviewTextClassifier().model) | |
let label = reviewPredictor.predictedLabel(for: self.review) | |
self.mlPrediction = label ?? "" | |
}catch(let error){ | |
print("error is \(error.localizedDescription)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment