Skip to content

Instantly share code, notes, and snippets.

View jfahrenkrug's full-sized avatar

Johannes Fahrenkrug jfahrenkrug

View GitHub Profile
import SwiftUI
import Combine
class ViewModel: ObservableObject {
@Published var sentence = ""
}
struct ContentView: View {
@ObservedObject var viewModel = ViewModel()
@jfahrenkrug
jfahrenkrug / main.dart
Created August 4, 2022 08:54
exquisite-rainbow-1517
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(
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()
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)")
}
/**
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
let jsCode = try? String.init(contentsOf: Bundle.main.url(forResource: "Sentimentalist.bundle", withExtension: "js")!)
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"
const Sentiment = require('sentiment')
export class Analyzer {
static analyze(phrase) {
let sentiment = new Sentiment()
let result = sentiment.analyze(phrase)
return result['score']
}
}
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)
}
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