Skip to content

Instantly share code, notes, and snippets.

Alex Bowe alexbowe

View GitHub Profile
@alexbowe
alexbowe / popcount_table.erl
Created Apr 4, 2011
Generates a tuple of popcounts.
View popcount_table.erl
-module(popcount_table).
-export([gen_table/1]).
gen_table(Bits) -> gen_table(1, 2 bsl (Bits - 1), {0}).
gen_table(Stop, Stop, Table) -> Table;
gen_table(Value, Stop, Table) ->
New = (Value band 1) + erlang:element((Value bsr 1) + 1, Table),
NewTable = erlang:append_element(Table, New),
gen_table(Value + 1, Stop, NewTable).
@alexbowe
alexbowe / my_binary.erl
Created Apr 2, 2011
Provides some idiomatic tools to help when programming using Erlang's Binary primitive type.
View my_binary.erl
-module(my_binary).
-export([foreach/3]).
-export([fold/4]).
break(Blocksize, Bin) ->
<<Head:Blocksize/bits, Rest/bits>> = Bin,
{Head, Rest}.
foreach(_, _, <<>>) -> ok;
foreach(F, Blocksize, Bin) ->
@alexbowe
alexbowe / nltk-intro.py
Created Mar 21, 2011
Demonstration of extracting key phrases with NLTK in Python
View nltk-intro.py
import nltk
text = """The Buddha, the Godhead, resides quite as comfortably in the circuits of a digital
computer or the gears of a cycle transmission as he does at the top of a mountain
or in the petals of a flower. To think otherwise is to demean the Buddha...which is
to demean oneself."""
# Used when tokenizing words
sentence_re = r'''(?x) # set flag to allow verbose regexps
([A-Z])(\.[A-Z])+\.? # abbreviations, e.g. U.S.A.
@alexbowe
alexbowe / three_test.rb
Created Nov 23, 2010
Tests a regular expression for recognising binary numbers that are divisible by 3.
View three_test.rb
your_re = // # <= YOUR REGEX GOES BETWEEN THESE SLASHES*************************
#Example: /.*/
# This wraps your regex so the whole binary string has to match it
@re = /^(#{your_re})$/
# Helper code to convert numbers to binary strings
class Fixnum
def to_bin_s
return '0' if self == 0
s = ''
@alexbowe
alexbowe / num_to_bin_string.rb
Created Nov 22, 2010
Method to convert a number to a binary string
View num_to_bin_string.rb
class Fixnum
def to_bin_s
return '0' if self == 0
s = ''
n = self
while n > 0
s = (n % 2).to_s << s
n = n >> 1
end
View chunks.py
# Extracting nounphrase chunks from a parse tree
# from http://streamhacker.com/2009/02/23/chunk-extraction-with-nltk/
# for each noun phrase sub tree in the parse tree
for subtree in tree.subtrees(filter=lambda t: t.node == 'NP'):
# print the noun phrase as a list of part-of-speech tagged words
print subtree.leaves()
@alexbowe
alexbowe / instance_method_adder.py
Created Mar 28, 2010
Python function for adding an instance method to an object
View instance_method_adder.py
def add_method(obj, f, fname):
"""Adds the instance method from function f to the object obj, callable by fname (i.e. obj.fname())
example:
def func(self):
print 'test'
add_method(myObject, func, 'newmethodname')
myObject.newmethodname()
You can’t perform that action at this time.