Skip to content

Instantly share code, notes, and snippets.

@klappy
klappy / Tiny JavaScript tokenizer.js
Last active September 16, 2019 20:51 — forked from borgar/Tiny JavaScript tokenizer.js
A compact tokenizer written in JavaScript.
/**
* Tiny tokenizer - https://gist.github.com/borgar/451393
* @param {String} string - string to be tokenized
* @param {Object} parsers - { word:/\w+/, whitespace:/\s+/, punctuation:/[^\w\s]/ }
* @param {String} deftok - type to label tokens that are not classified with the above parsers
* @return {Array} - array of objects => [{ token:"this", type:"word" },{ token:" ", type:"whitespace" }, Object { token:"is", type:"word" }, ... ]
**/
export const classifyTokens = (string, parsers, deftok) => {
string = (!string) ? '' : string; // if string is undefined, make it an empty string
if (typeof string !== 'string') {
@klappy
klappy / sentence_tokens.js
Last active December 1, 2019 20:45
Wrap unicode word tokens with html spans in a sentence, without losing any punctuation.
let XRegExp = require('xregexp');
let nonUnicodeLetter = XRegExp('[^\\pL\\pM]+?');
var sentence = "This is a sentence-with some punctuation, and it will be split-up."
console.log("sentence: ", sentence)
var tokens = sentence.split(nonUnicodeLetter)
console.log("tokens: ", tokens)
_sentence = sentence
var assignments = {
"Hindi-ULB" : {
"Matthew": {
"Milestone 1": {
"assigned": [1,2,3,4,5,6,7,8,9,10,11,12,13],
"completed": [1,2,3,4,5]
},
"Milestone 2": {
"assigned": [1,2,3,4,5],
"completed": [1,2]
@klappy
klappy / SKMultilineLabel.swift
Last active October 11, 2017 03:21 — forked from kevinwo/SKMultilineLabel.swift
Multi line label in Sprite Kit in Swift
//
// SKMultilineLabel.swift
//
// Created by Craig on 10/04/2015
// Modified by Christopher Klapp on 11/21/2015 for line breaks \n for paragraphs
// Copyright (c) 2015 Interactive Coconut. All rights reserved.
//
/* USE:
(most component parameters have defaults)
@klappy
klappy / Follow That Star - Privacy Policy.txt
Created November 8, 2015 01:05
Follow That Star Privacy Policy
Privacy Policy
Last updated: November 08, 2015
Church Muse ("us", "we", or "our") operates the Follow That Star iOS Application (the "Service").
This page informs you of our policies regarding the collection, use and disclosure of Personal Information when you use our Service.
We will not use or share your information with anyone except as described in this Privacy Policy.
@klappy
klappy / heads_or_tails.rb
Created December 3, 2012 22:08
Heads or Tails
samples = 100000.times.with_object([]){|i,a|a.push ["heads","tails"].sample}
#always choose heads
samples.map{|coin|coin=="heads"}.group_by{|o|o}.map.with_object({}){|h,o|o[h[0]]=h[1].count}[true].to_f/1000
#alternate every other call
samples.map.with_index{|coin,i|coin==(i.even? ? "heads" : "tails")}.group_by{|o|o}.map.with_object({}){|h,o|o[h[0]]=h[1].count}[true].to_f/1000
#randomly choose
samples.map{|coin|coin==["heads","tails"].sample}.group_by{|o|o}.map.with_object({}){|h,o|o[h[0]]=h[1].count}[true].to_f/1000