Skip to content

Instantly share code, notes, and snippets.

View louismullie's full-sized avatar

L louismullie

View GitHub Profile
@louismullie
louismullie / random_maxima.rb
Created January 8, 2012 20:57
Benchmark: Randomly selecting between N maximal values and returning key in Ruby hash.
Benchmark.bm do |x|
types = {
banana: 0.2,
lemon: 0.4,
orange: 0.4
}
x.report("select") do
1.upto(10000) do
@louismullie
louismullie / gist:1707180
Created January 30, 2012 22:23
Benchmark: Interning strings and string variables in Ruby
# :"test" is faster than "test".intern ...
Benchmark.bm do |x|
test = 'test'
x.report { 1000000.times { "test".intern } }
x.report { 1000000.times { :"test" } }
end
# user system total real
# 0.430000 0.000000 0.430000 ( 0.429735)
# 0.080000 0.000000 0.080000 ( 0.082169)
@louismullie
louismullie / gist:1707218
Created January 30, 2012 22:31
Benchmark: Array.empty? vs. Array.size == 0
require 'benchmark'
Benchmark.bm do |x|
empty = []
not_empty = [:foo, :bar, :baz]
# (size == 0) is faster than empty? for both empty
# and non-empty arrays.
x.report { 1000000.times { empty.size == 0 } }
x.report { 1000000.times { not_empty.size == 0 } }
# user system total real
# 0.100000 0.000000 0.100000 ( 0.098931)
@louismullie
louismullie / gist:1707242
Created January 30, 2012 22:37
Benchmark: Array.include? vs. Hash.include?
require 'benchmark'
array = (1..1_000_000).to_a
hash = Hash[array.map {|x| [x, nil]}]
Benchmark.bm(15) do |x|
x.report("Array.include?") { 1000.times { array.include?(500_000) } }
x.report("Hash.include?") { 1000.times { hash.include?(500_000) } }
end
# user system total real
@louismullie
louismullie / tag_alignments.rb
Created February 5, 2012 08:00
The Alignment Game - Tag Alignments For Five Treebanks
# ClawsC5 = 0
# Brown = 1
# Penn = 2
# Negra = 3
# Penn Chinese = 4
# "Simplified" Stanford French Tags = 5
[
'Adjective', ['AJ0', 'JJ', 'JJ', '', 'JJ', 'A'],
@louismullie
louismullie / treat-xml-output-example.xml
Created February 5, 2012 23:11
Treat XML Output Example
<?xml version="1.0" encoding="us-ascii" standalone="no" ?>
<treat>
<sentence id='70222715664980' language='eng' tag_set='penn' dependencies=''> <word id='70222715690540' stem='Happi' tag='NN' tag_opt='' tag_set='penn' category='noun' dependencies=''>Happiness</word>
<word id='70222715689240' stem='is' tag='VBZ' tag_opt='' tag_set='penn' category='verb' dependencies=''>is</word>
<word id='70222715688060' stem='not' tag='RB' tag_opt='' tag_set='penn' category='adverb' dependencies=''>not</word>
<word id='70222715686880' stem='an' tag='DT' tag_opt='' tag_set='penn' category='determiner' dependencies=''>an</word>
<word id='70222715685640' stem='ideal' tag='NN' tag_opt='' tag_set='penn' category='noun' dependencies=''>ideal</word>
<word id='70222715684460' stem='of' tag='IN' tag_opt='' tag_set='penn' category='preposition' dependencies=''>of</word>
<word id='70222715683260' stem='reason' tag='NN' tag_opt='' tag_set='penn' category='noun' dependencies=''>reason</word>
<punctuation id='70222715698440'
@louismullie
louismullie / treat-date-time-colorized.rb
Created February 6, 2012 00:41
Treat - Date and Time Extraction With Graph Colorization
require 'date'
Treat.sweeten!
s = Section "2011/12/23 \n - Obama and Sarkozy met on January 1st to investigate the possibility of a new rescue plan. President Sarkozy is to meet Merkel next Tuesday in Berlin."
s.do(
:chunk, :segment, :parse, :time, :date,
:visualize => [
:dot, {
:file => 'test-date-time-extraction.dot',
:colors => {
@louismullie
louismullie / treat-coref-colorization.rb
Created February 6, 2012 03:02
Treat - Coreference Extraction With Graph Colorization
require 'treat'
Treat.sweeten!
p = Paragraph "Obama and Sarkozy met on January 1st to investigate the possibility of a new rescue plan." +
"President Sarkozy is to meet Merkel next Tuesday in Berlin."
p.do(
:coreferences,
:visualize => [
:dot, {
:file => 'ner-coref-extraction.dot',
@louismullie
louismullie / http-post-php.php
Created February 7, 2012 23:57
HTTP Get/Post Send
<?php
function send($host, $method, $path, $data, $useragent = null)
{
$str_data = '';
foreach ($data as $key => $value)
{
if ($str_data != '') $str_data .= '&';
$str_data .= rawurlencode($key) . '=' . rawurlencode($value);
}
// Supply a default method of GET if the one passed was empty
@louismullie
louismullie / benchmark-include.rb
Created February 14, 2012 15:48
Benchmark: Array.include? vs Set.include? vs Hash[]
require 'benchmark'
require 'set'
a = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
h = {'a' => 1, 'b' => 1, 'c' => 1, 'd' => 1,
'e' => 1, 'f' => 1, 'g' => 1}
s = Set.new(a)
Benchmark.bm do |x|