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
import SwiftUI | |
import Combine | |
class ViewModel: ObservableObject { | |
@Published var sentence = "" | |
} | |
struct ContentView: View { | |
@ObservedObject var viewModel = ViewModel() | |
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
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 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 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 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 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
let jsCode = try? String.init(contentsOf: Bundle.main.url(forResource: "Sentimentalist.bundle", withExtension: "js")!) |
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
var path = require('path') | |
module.exports = { | |
mode: "production", | |
entry: { Sentimentalist: "./index.js" }, | |
output: { | |
path: path.resolve(__dirname, "dist"), | |
filename: "[name].bundle.js", | |
library: "[name]", | |
libraryTarget: "var" |
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
const Sentiment = require('sentiment') | |
export class Analyzer { | |
static analyze(phrase) { | |
let sentiment = new Sentiment() | |
let result = sentiment.analyze(phrase) | |
return result['score'] | |
} | |
} |
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
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var textField: UITextField! | |
@IBOutlet weak var sentimentLabel: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.sentimentLabel.text = SentimentAnalyzer.shared.emoji(forScore: 0) | |
} |
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
import UIKit | |
import JavaScriptCore | |
/// An analyzer of sentiments | |
class SentimentAnalyzer: NSObject { | |
/// Singleton instance. Much more resource-friendly than creating multiple new instances. | |
static let shared = SentimentAnalyzer() | |
private let vm = JSVirtualMachine() | |
private let context: JSContext | |
NewerOlder