Skip to content

Instantly share code, notes, and snippets.

Johannes Fahrenkrug jfahrenkrug

Block or report user

Report or block jfahrenkrug

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View index.js
import * as sentiment from 'sentiment'
export class Analyzer {
static analyze(phrase) {
// Make sure nativeLog is defined and is a function
if (typeof nativeLog === 'function') {
nativeLog(`Analyzing '${phrase}'`)
}
let result = sentiment(phrase)
View SentimentAnalyzer.swift
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)")
}
View SentimentAnalyzer.swift
/**
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
View SentimentAnalyzer.swift
let jsCode = try? String.init(contentsOf: Bundle.main.url(forResource: "Sentimentalist.bundle", withExtension: "js")!)
View webpack.config.js
var path = require('path')
module.exports = {
entry: { Sentimentalist: "./index.js" },
output: {
path: path.resolve(__dirname, 'dist'),
filename: "[name].bundle.js",
library: "[name]",
libraryTarget: "var"
}
View index.js
import * as sentiment from 'sentiment'
export class Analyzer {
static analyze(phrase) {
let result = sentiment(phrase)
return result['score']
}
};
View ViewController.swift
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)
}
View SentimentAnalyzer.swift
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
@jfahrenkrug
jfahrenkrug / extract_wwdc_samplecode.js
Created Jun 7, 2017
Extract the WWDC 2017 Sample Code URL from the sessions page. Just paste it into the JS console on https://developer.apple.com/wwdc/schedule/
View extract_wwdc_samplecode.js
$.getJSON( "https://devimages-cdn.apple.com/wwdc-services/h8a19f8f/049CCC2F-0D8A-4F7D-BAB9-2D8F5BAA7030/contents.json", function( data ) {
var items = [];
$.each( data.contents, function( key, val ) {
if (val.type == 'Session' && val.eventId == 'wwdc2017' && val.related && val.related.resources) {
$.each(val.related.resources, function (resKey, resId) {
var resource = data.resources[resId]
if (resource && resource['resource_type'] == 'samplecode') {
items.push(resource);
View keybase.md

Keybase proof

I hereby claim:

  • I am jfahrenkrug on github.
  • I am jfahrenkrug (https://keybase.io/jfahrenkrug) on keybase.
  • I have a public key whose fingerprint is BE25 078F A534 3D3F E12D B915 3105 2A52 D4CA A081

To claim this, I am signing this object:

You can’t perform that action at this time.