Skip to content

Instantly share code, notes, and snippets.

View agarie's full-sized avatar

Carlos Agarie agarie

View GitHub Profile
@agarie
agarie / poly.rb
Created July 14, 2014 20:12
DSL for symbolic integration of polynomials
def polynomial(&block)
varX = Variable::Local.new('x')
x = Polynomial.valueOf(Real::ONE,varX)
yield x
end
# Creating a polynomial fx = 1(x^5) + 6(x^2).
fx = polynomial do |x|
x.pow(5).plus(x.pow(2).times(Real.valueOf(6)))
end
@agarie
agarie / gist:8048725
Created December 20, 2013 00:41
Ruby webserver one-liner.
ruby -rwebrick -e 'WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd).start'
@agarie
agarie / fix_encoding.rb
Created December 18, 2013 03:47
Fixes issues when dealing with ISO-8859-1 ("LATIN-1") encoded files, which are very common with government data and such.
def fix_encoding(fn)
q = File.read(fn)
File.open(fn, 'w') { |f| f << q.encode("UTF-8", "ISO-8859-1") }
end
@agarie
agarie / matthews.rb
Created December 10, 2013 18:20
Matthews correlation coefficient. I'll never have to reimplement this!
def matthews(tp, tn, fp, fn)
den = tp * tn - fp * fn
num = (tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)
den / Math.sqrt(num)
end
@agarie
agarie / stop_words.pt
Created December 9, 2013 20:45
Portuguese (pt-PT) stop words for NLP. This sample is from https://gist.github.com/lorn/995615, but I formatted it in a nicer way.
abaixo
aca
acaso
acerca
acima
acola
acula
ademais
adentro
adiante
@agarie
agarie / stop_words.en
Last active December 30, 2015 20:28
English stop words for NLP.
a
about
above
across
after
afterwards
again
against
all
almost
@agarie
agarie / bootable-flashdrive.sh
Last active December 18, 2015 05:29
How to create a USB bootable flash drive in OS X. I *always* forget this.
# From: http://www.ubuntu.com/download/desktop/create-a-usb-stick-on-mac-osx
hdiutil convert -format UDRW -o ~/path/to/target.img ~/path/to/iso.iso
# Run diskutil list and determine the device node assigned to your flash media (e.g. /dev/disk2).
diskutil list
# Replace N with the disk number from the last command.
diskutil unmountDisk /dev/diskN
@agarie
agarie / tokyotosho.rb
Last active July 1, 2022 15:07
Utility to download torrent files from tokyotosho.info. It uses a similar heuristic to my own when looking for torrents, but could use some improvements.
#!/usr/bin/env ruby -w
require 'open-uri'
require 'uri'
require 'nokogiri'
DEFAULT_DIR = "/Users/carlosagarie/torrents"
CATEGORY = 1 # Anime.
# Regular expressions and constants for extraction.
@agarie
agarie / composite.rb
Created March 26, 2013 04:15
Just a small experiment after reading "Design Patterns in Ruby", by Ross Olsen.
# Small experiment in which I try to implement the composite pattern without
# resorting to two classes.
class Composite
attr_accessor :action, :subcomposites
# Cool thing: if we don't provide a block, it'll be passed nil. So there's no
# need to make it a "default parameter" (you can't do it, actually).
def initialize(&action)
@action = action
@agarie
agarie / f1-score.rb
Created March 25, 2013 02:31
F1 score in Ruby. I use this snippet a lot, but usually rewrite it for each project... not anymore.
# The F1 score is a measure of how accurate an algorithm is.
#
# Precision is the probability that a (randomly selected) retrieved document is relevant:
# precision = true positives / (true positives + false positives)
#
# Recall is the probability that a (randomly selected) relevant document is retrieved in a search:
# recall = true positives / (true positives + false negatives)
def f1_score(precision, recall)
(2 * (precision * recall)) / (precision + recall)