Skip to content

Instantly share code, notes, and snippets.

@jaydonnell
jaydonnell / gist:61718
Created February 11, 2009 00:22 — forked from pete/gist:61715
#!/usr/bin/env ruby
require 'socket'
localhost = Socket.gethostbyname(Socket.gethostname).first
npackets = ARGV[0] ? ARGV[0].to_i : 1_000
def top_n(name, data)
data.map { |h,stats|
[h, stats[name]]
# "local" methods are those defined directly on a constant,
# as opposed to those inherited or mixed in
class Module
def local_class_methods
self.methods.select { |m| self.method(m).owner == self }
end
def local_instance_methods
self.instance_methods.select { |m| self.instance_method(m).owner == self }
require 'java'
module JavaLang
include_package "java.lang"
end
module JavaConncurrent
include_package "java.util.concurrent"
end
desc "run some tests"
task :test do
options = ENV['options']
files = ARGV[1,ARGV.length]
if files.empty?
files = FileList['test/**/*.rb'].exclude('test/test_helper.rb')
end
puts cmd = "jruby #{options} -S spec -c #{ files.join(' ') }"
system cmd
end
private List<String> itemsWithAllTokens(String phrase, Map<String, List<String>> map) {
List<String> tokens = tokenize(phrase);
List<String> items = map.get(tokens.get(0));
if (items == null) {
return new ArrayList<String>();
}
for (int i=1; i<tokens.size(); i++) {
List<String> tempListings = map.get(tokens.get(i));
if (tempListings == null) return new ArrayList<String>(); //As soon as we have a token with nothing in it, we can return an empty list...
List<String> toDelete = new ArrayList<String>();
public class WordDistance {
public static ArrayList<String> getPossibleEdits(String word) {
// returns set of all candidate words that are one edit away from entered word
ArrayList<String> possibleEdits = new ArrayList<String>();
for(int i=0; i < word.length(); ++i) possibleEdits.add(word.substring(0, i) + word.substring(i+1));
for(int i=0; i < word.length()-1; ++i){
String tmp = word.substring(0, i) + word.substring(i+1, i+2) + word.substring(i, i+1) + word.substring(i+2);
if(!tmp.equals(word)) possibleEdits.add(tmp);
}
for(int i=0; i < word.length(); ++i) {
module Edits
def deletes1
map_transforms do |word, i|
word.delete_at(i)
end
end
def transposes
map_transforms do |word, i|
# gradient descent in ruby
def find_min(x_old, x_new, precision, eps, &block)
while (x_new - x_old).abs > precision
x_old = x_new
tmp = yield x_new
x_new = x_new - eps * tmp
end
x_new
end
int count = Files.readLines(
new File("/path/to/file"),
Charsets.UTF_8, // remember that? :)
new LineProcessor<Integer>() {
int count = 0;
public boolean processLine(String line) {
count++;
}
public Integer getResult() { return count; }
});
XPath xpath = XPathFactory.newInstance().newXPath();
String xmlFile = "Wikipedia-Category-GlobalWarming-20090919043526.xml";
InputStream is = WikipediaSeederTest.class.getClassLoader().getResourceAsStream(xmlFile);
InputSource inputSource = new InputSource( is );
NamespaceContext context = new NamespaceContextMap("wp", "http://www.mediawiki.org/xml/export-0.3/");
xpath.setNamespaceContext(context);
NodeList elements = (NodeList)xpath.evaluate(
"//wp:page", inputSource, XPathConstants.NODESET );