Skip to content

Instantly share code, notes, and snippets.

View longouyang's full-sized avatar

Long Ouyang longouyang

View GitHub Profile
<html>
<head>
<script type="text/javascript">
var pID;
var i = 0;
var startTime, endTime;
function start_test() {
i = 0;
Hello World!
@longouyang
longouyang / turk.js
Created February 26, 2011 19:30
Small helper file for doing external HITs on Mechanical Turk
/*
-----------------------------------------------------------------
-----------------------------------------------------------------
NOTE: this file has been superseded by mmturkey:
https://github.com/longouyang/mmturkey
-----------------------------------------------------------------
-----------------------------------------------------------------
*/
var turk = {};
@longouyang
longouyang / quine.rb
Created April 10, 2011 03:29
A small quine in Ruby
def p(x) puts x + 'p ' + 34.chr + x + 34.chr end; p "def p(x) puts x + 'p ' + 34.chr + x + 34.chr end; "
@longouyang
longouyang / gist:3180917
Created July 26, 2012 08:14
PCFG unfold in Mathematica
TerminalQ[x_] := MemberQ[{a, b, c, d}, x];
Rules = {S -> {{{X, Y}, 1}, {{X, X, Y}, 5}},
X -> {{{a}, 1/2}, {{b}, 1/2}}, Y -> {{{c}, 1/3}, {{d}, 2/3}}};
PCFGUnfold[sym_, Rules_, TerminalQ_] :=
If[TerminalQ[sym],
{{{sym}, 1}},
Block[{lookup, rules, probabilities},
(*for some reason,I can't specify the last two in the {} above*)
@longouyang
longouyang / log-add.py
Created August 28, 2012 22:36
Adding log probabilities
from math import log, exp
"""
sources:
http://windowoffice.tumblr.com/post/33548509/logsum-underflow-trick-re-discovery
https://facwiki.cs.byu.edu/nlp/index.php/Log_Domain_Computations
"""
def log_add(x,y):
logx = log(x)
logy = log(y)
@longouyang
longouyang / gist:3688502
Last active April 23, 2022 20:22
Random combinations in Church (Buckles-Lybanon '77)
(define (pn x) (for-each display (list x "\n")))
(define (! n)
(let iter ((product 1)
(counter 1))
(if (> counter n)
product
(iter (* counter product)
(+ counter 1)))))
@longouyang
longouyang / gist:6369737
Created August 28, 2013 18:47
emacs regexp to capitalize heads of sentences
\\(^\\|\\. \\)\\([a-z]\\)
\1\,(upcase \2)
@longouyang
longouyang / gist:8815976
Created February 5, 2014 01:37
Big Scheme command
(length (list 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
@longouyang
longouyang / gist:d2963f3d095e3b79a3f8
Created September 4, 2014 00:01
retrospective mcmc crp
var rDPmem = $b({
name: 'rDPmem',
desc: 'Retrospective implementation of DPmem',
params: [{name: 'alpha', type: 'positive real', desc: 'Concentration parameter'},
{name: 'f', type: 'function', desc: 'Function to stochastically memoize'}],
fn: function(alpha, f) {
var allStickSets = {};
var cumulativeStickSums = {};
var caches = {};