This file contains hidden or 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
/** | |
Analyze the sentiment of a given English sentence. | |
- Parameters: | |
- sentence: The sentence to analyze | |
- completion: The block to be called on the main thread upon completion | |
- score: The sentiment score | |
*/ | |
func analyze(_ sentence: String, completion: @escaping (_ score: Int) -> Void) { | |
// Run this asynchronously in the background |
This file contains hidden or 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
private override init() { | |
let jsCode = try? String.init(contentsOf: Bundle.main.url(forResource: "Sentimentalist.bundle", withExtension: "js")!) | |
// The Swift closure needs @convention(block) because JSContext's setObject:forKeyedSubscript: method | |
// expects an Objective-C compatible block in this instance. | |
// For more information check out https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/doc/uid/TP40014097-CH35-ID350 | |
let nativeLog: @convention(block) (String) -> Void = { message in | |
NSLog("JS Log: \(message)") | |
} | |
This file contains hidden or 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
const Sentiment = require('sentiment') | |
export class Analyzer { | |
static analyze(phrase) { | |
// Make sure nativeLog is defined and is a function | |
if (typeof nativeLog === 'function') { | |
nativeLog(`Analyzing '${phrase}'`) | |
} | |
let sentiment = new Sentiment() |
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( |
This file contains hidden or 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
import SwiftUI | |
import Combine | |
class ViewModel: ObservableObject { | |
@Published var sentence = "" | |
} | |
struct ContentView: View { | |
@ObservedObject var viewModel = ViewModel() | |
OlderNewer