Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 36 total  RSS 

Searching through your Twitter archive

This Ruby code downloads previous twitter entries as html files to a local file directory.
(1..5).each {|i| `wget --user=jrobertson --password=secret http://twitter.com/account/archive?page=#{i}`}

then using grep -i <keyword> * you can search for anything you've twittered in the past.

Note: Use Wget sparingly.

Disjoint Set Class in Ruby

A disjoint Set can be used for creating minimal spanning trees.
The union-find algorithm is fast, faster than linked lists for some uses.
See: Wikipedia Disjoint Set

It's fast and simple. Just add a DisjointSetNode object to the
objects that you want to create a tree of.

# This is a disjoint set implementing union-find
# It can be used for generating a minimal spanning
# tree using Kruskal's algorithm.
# http://en.wikipedia.org/wiki/Kruskal's_algorithm

class DisjointSetNode
  attr_accessor :parent, :rank

  def initialize
    @parent = self
    @rank = 0
  end

  # An optimised union by rank. Attaches smaller tree to larger.
  def union(other)
    self_root = self.find
    other_root = other.find
    if self_root.rank > other_root.rank
      other_root.parent = self_root
    elsif self_root.rank < other_root.rank
      self_root.parent = other_root
    elsif self_root != other_root
      other_root.parent = self_root
      self_root.rank += 1
    end
  end

  def find
    if self.parent === self
      self
    else
      self.parent = self.parent.find
    end
  end

end   # class DisjointSetNode



Some simple test code:
require 'disjointsetnode'
require 'test/unit'
require 'test/unit/ui/console/testrunner'

    class DisjointSetTest < Test::Unit::TestCase
      N_Nodes = 1000

      def setup
        @nodes = Array.new
        (1..N_Nodes).each do |node|
        @nodes << DisjointSetNode.new
        end
      end

      # def teardown
      # end

      def test_creation
        @nodes.each do |node|
          assert !node.nil?, "A Node is nil!"
        end
      end

      def test_initial_values
        @nodes.each do |node|
          assert node.parent === node, "self #{node}, parent #{node.parent} not equal."
          assert node.rank == 0, " A node's rtank is not 0."
        end
      end

      def test_union
        # Join all nodes in sequence
        @nodes.each do |node|
          if !@lastnode.nil?
            node.union(@lastnode)
            node_f = node.find
            lnode_f = @lastnode.find
            assert node_f == lnode_f, "Join not completed: node_f: #{node_f}, lnode_f: #{lnode_f}"
          end
          @lastnode = node
        end
      end

      def test_random_union
        (1..N_Nodes-1).each do |n|
          node = @nodes[n]
          other = @nodes[rand(N_Nodes-1)]
          node.union(other)
          node_f = node.find
          onode_f = other.find
          assert node_f == onode_f, "Join not completed: node_f: #{node_f}, onode_f: #{onode_f}"
        end
      end
    end

# This runs the tests for ya! 
Test::Unit::UI::Console::TestRunner.run(DisjointSetTest)

Add a Google custom search facility to your website

This HTML page displays a basic search page similar to http://www.google.com/ however this search page will only search for results from http://snippets.dzone.com.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<title>abc</title>
<meta http-equiv="Content-Type" content="text/html; charset: UTF-8" />

</head>
<body>

    <form action="http://www.google.com/search">
      <div><input name="query" type="hidden" value="site:snippets.dzone.com"></input></div>
      <div><input maxlength="2048" name="q" size="55" title="Google Search" value="" ></input></div>
      <div><input name="btnG" type="submit" value="Google Search"></input></div>
    </form>
</body>
</html>

I stumbled across the hidden query field while I was looking for ways to perform custom search without the need for JavaScript. If you are interested in Google's official custom search engine on your site go to http://www.google.com/cse

* update 8-Jan-08 2:01pm *
Here's a more advanced example from someone's blog titled 'Fine-tuning Custom Google Search' http://snipr.com/1wppb [blogs.salon.com]
<FORM method=GET action=http://www.google.com/custom>;
<A HREF=http://www.google.com/search>;
<IMG SRC=http://www.google.com/logos/Logo_40wht.gif border=0 ALT=Google align=middle></A> <INPUT TYPE=text name=q size="10" maxlength=255 value="">
<INPUT type=submit name=sa VALUE="Just Goog It!">
<INPUT type=hidden name=cof VALUE="GIMP:darkblue;LW:380;ALC:red;L:http://blogs.salon.com/0001111/images/rfbanner.jpg;GFNT:lightblue;LC:red;LH:100;AH:left;VLC:gray;S:http://blogs.salon.com/0001111/;GALT:blue;AWFID:ba6ecf243ae3d16f;"><br />
>span class="small">
<input type=radio name=sitesearch value="blogs.salon.com" checked>Search blogs.salon.com
<input type=radio name=sitesearch value="">Search the Web</span><br />
</FORM>

Tutor search

// Search results page - formatting html has been omited.

<?php include 'db.inc.php';

		// The basic SELECT statement
		$select = 'SELECT id, title, forename, surname, userlevel';
		$from = ' FROM users';
		$where = " WHERE userlevel='2'";

		$forename = $_POST['forename'];
		if ($forename !='') { //Forename was specified
		$where .= " AND forename LIKE '%$forename%'";

		$surname = $_POST['surname'];
		if ($surname !='') { //Surnamename was specified
		$where .= " AND surname LIKE '%$surname%'";
		}
		?>

	<?php
	$users = @mysql_query($select . $from . $where);
	if (!$users) {
	exit ('<p>Unable to abtain user list from the database');
	}

	?>
	<table>
	<tr><th>Username</th><th>Title</th><th>Forename</th><th>Surname</th><th>Options</th></tr>

	<?php
	while ($user = mysql_fetch_array($users)) {
	echo "<tr vlaign='top'>\n";
	$id = $user['id'];
	$username = htmlspecialchars($user['username']);
	$title = htmlspecialchars($user['title']);
	$forename = htmlspecialchars($user['forename']);
	$surname = htmlspecialchars($user['surname']);

	echo "<td>$username</td><td>$title</td><td>$forename</td><td>$surname</td>\n";
	echo "<td><a href='edituser.php?id=$id'>Edit</a> | <a href='deleteuser.php?id=$id' onClick=\"return confirm('WARNING: Deleted users cannot be retrieved. All files associated with that user will be deleted. Continue?');\">Delete</a></td>\n";
	echo "</tr>\n";
	}
	?>
	</table>

Babelfish translate

Ok, so maybe some of you might think that Babelfish's the best translator out there. I don't know about that , but I do know where my towel is ;)
So, boys and girls, here's the bookmarklet that allows to translate your current web-page using the babelfish translation engine.

javascript:location.href='http://babelfish.altavista.com/babelfish/tr?trurl='+encodeURIComponent(location.href)+'&lp=%s&btnTrUrl=Translate'


To make it useful, save it as a bookmark in FF (or as a new search engine in Opera) and give it a keyword/shortcut by editing the bookmark's properties in FF (or the search properties in Opera). Let's call him, say 'bf', that should do it.

Here are some examples:
bf en_fr
bf fr_en
bf en_ja


Enjoy!
G.R.

Fast stop word detection in Ruby

Requires BloominSimple (a pure Ruby Bloom filter class).

List of stop words obtained from http://www.dcs.gla.ac.uk/idom/ir_resources/linguistic_utils/stop_words

# Detect stop words QUICKLY
# Uses a bloom filter instead of searching literally through a list of stopwords
# for > 3x speed increase
# 
#    using bloom filter: 2.580000   0.030000   2.610000 (  2.698829)
#  using literal search: 7.850000   0.120000   7.970000 (  8.181684)


require 'bloominsimple'
require 'digest/sha1'
require 'pp'

# Create a simple bloom filter that uses a SHA1 hash (more effective than BloominSimple's default hashing)
b = BloominSimple.new(50000) do |word|
  Digest::SHA1.digest(word.downcase.strip).unpack("VVV")
end

# Add stopwords to the bloom filter!
stopwords = []
File.open('stopwords').each { |a| b.add(a); stopwords << a.downcase.strip }

# Read in a whole dictionary of regular words
words = File.open('/usr/share/dict/words').read.split.collect{|a| a.downcase.strip }

# Define two ways to detect stopwords for comparison..
using_filter = lambda { |word| b.includes?(word) }
using_array = lambda { |word| stopwords.include?(word.downcase.strip) }
techniques = [using_filter, using_array]

# Run stopword comparisons with both techniques
t = techniques.collect { |l| words.collect { |a| l[a] } }

# See how effective the bloom filter has been compared to the literal search
if t[0] == t[1]
  puts "GOOD"
else
  words.zip(t[0],t[1]).each do |x|
    puts x.first if x[1] != x[2]
  end
end

# Now do speed benchmarks..
techniques.each { |l| puts Benchmark.measure { words.each { |a| l[a] } } }

Array Search Prototype

This prototype extends the Array object to allow for searches
within the Array. It will return false if nothing is found. If
item(s) are found you'll get an array of indexes back which matched
your search request. It accepts strings, numbers, and regular expressions as search
criteria. 35 is different than '35' and vice-versa.
// Examples
var test=[1,58,'blue','baby','boy','cat',35,'35',18,18,104]
result1=test.find(35);    //returns 6
result2=test.find(/^b/i); //returns 2,3,4
result3=test.find('35');  //returns 7
result4=test.find(18);    // returns 8,9
result5=test.find('zebra'); //returns false


Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}

J2ME - Create Service Bluetooth

// Create Service Bluetooth

import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class ServerChat
{
	private static final String CHATTANDO_UUID = "A1A2A3A4A5A6A7A8A9A0B1B2B3B4B5B6";
	private static final String CHATTANDO_SERVICE = "Chattando";
	
	private boolean isReady = false;
	
	private StreamConnection stream_connection;
	private StreamConnectionNotifier stream_connection_notifier;
	
	public ServerChat()
	{
		startServerChatBluetooth();
	}
	
	// Apre il servizio per la Chat
	public void startServerChatBluetooth()
	{
		new Thread()
		{
			public void run()
			{
				try
				{
					LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);
				}
				catch(Exception error)
				{
					error.printStackTrace();
				}
				
				try
				{
					stream_connection_notifier = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + CHATTANDO_UUID + ";name=" + CHATTANDO_SERVICE);
				}
				catch(Exception error)
				{
					error.printStackTrace();
				}
				
				stopServerChatBluetooth();
				
				// Mette in ascolto il Server della Chat
				isReady = true;
				
				try
				{
					while(isReady)
					{
						System.out.println("Sono in ascolto...");
						
						stream_connection = stream_connection_notifier.acceptAndOpen();
						
						System.out.println("Client Connected");
					}
				}
				catch(Exception error)
				{
					error.printStackTrace();
				}
			}
			
		}.start();
	}

	// Chiude il servizio per la Chat
	public void stopServerChatBluetooth()
	{
		if(isReady)
		{
			isReady = false;
			
			try
			{
				stream_connection_notifier.close();
			}
			catch(Exception error)
			{
				error.printStackTrace();
			}
		}
	}
}

J2ME - Search Service Bluetooth

// Example Search Service Bluetooth

import java.util.Vector;

import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;

public class ClientChat implements DiscoveryListener
{
	private static final String CHATTANDO_UUID = "A1A2A3A4A5A6A7A8A9A0B1B2B3B4B5B6";
	private static final String CHATTANDO_SERVICE = "Chattando";

	protected Chattando midlet;
	
	private boolean searchDone = false;
	
	private DiscoveryAgent discovery_agent;
	
	private Vector remote_device;
	private Vector device_found;
	
	public ClientChat(Chattando midlet)
	{
		this.midlet = midlet;
		
		startScanBluetoothDevices();
	}
	
	// Avvia la ricerca dei dispositivi Bluetooth
	public void startScanBluetoothDevices()
	{
		try
		{
			remote_device = new Vector();
			device_found = new Vector();
			
			discovery_agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
			discovery_agent.startInquiry(DiscoveryAgent.GIAC, this);
		}
		catch(Exception error)
		{
			error.printStackTrace();
		}
	}
	
	// Stoppa la ricerca dei dispositivi Bluetooth
	public void stopScanBluetoothDevices()
	{
		discovery_agent.cancelInquiry(this);
	}

	public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) 
	{
		// Aggiungo il dispositivo solo se e' un computer (0x0100) o un cellulare (0x0200)
		if(cod.getMajorDeviceClass() == 0x0100 || cod.getMajorDeviceClass() == 0x0200)
			remote_device.addElement(btDevice);
	}

	public void inquiryCompleted(int discType)
	{
		switch(discType)
		{
			case DiscoveryListener.INQUIRY_COMPLETED:
														System.out.println("Device Search Completed");
														
														break;
				
			case DiscoveryListener.INQUIRY_ERROR:
														System.out.println("Device Search Error");
														
														break;
				
			case DiscoveryListener.INQUIRY_TERMINATED:
														System.out.println("Device Search Terminated");
														
														break;
		}
		
		try
		{
			for(int i=0, cnt=remote_device.size(); i<cnt; i++)
			{
				discovery_agent.searchServices(new int[]{ 0x0100, 0x0200 }, new UUID[]{ new UUID(0x0003), new UUID(CHATTANDO_UUID, false) }, (RemoteDevice) remote_device.elementAt(i), this);
				waitForSearchDone();
			}
		}
		catch(Exception error)
		{
			error.printStackTrace();
		}
	}

	// Aspetta che la ricerca dei servizi per il dispositivo sia terminata
	private void waitForSearchDone()
	{
		searchDone = false;
		
		try
		{
			while(!searchDone)
			{
				synchronized(this)
				{
					this.wait();
				}
			}
		}
		catch(Exception error)
		{
			
		}
	}
	
	public void serviceSearchCompleted(int transID, int respCode)
	{
		switch(respCode)
		{
			case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
																		System.out.println("Service Search Completed");
																		
																		break;
				
			case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
																		System.out.println("Service Search Device not Reachable");
																		
																		break;
				
			case DiscoveryListener.SERVICE_SEARCH_ERROR:
																		System.out.println("Service Search Error");
																		
																		break;
				
			case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
																		System.out.println("Service Search No Records");
																		
																		break;
				
			case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
																		System.out.println("Service Search Terminated");
																		
																		break;
		}
		
		searchDone = true;
		
		// Risveglia il processo in attesa del completamento della ricerca dei servizi per un dispositivo
		synchronized(this)
		{
			this.notifyAll();
		}
	}

	public void servicesDiscovered(int transID, ServiceRecord[] servRecord)
	{
		for(int i=0, cnt=servRecord.length; i<cnt; i++)
		{
			if(((String) servRecord[i].getAttributeValue(0x0100).getValue()).equalsIgnoreCase(CHATTANDO_SERVICE))
			{
				device_found.addElement(servRecord[i].getHostDevice());
			}
		}
	}
}

binarysearch.scm

// Binary Search

; Andrew Pennebaker
; 18 Feb 2007
; License: GPL
; URL: http://snippets.dzone.com/posts/show/3535

(define binary-search
	(lambda (ls value low high)
		(let ((mid (floor (/ (+ low high) 2))))
			(cond
				((> low high) -1)
				((= (list-ref ls mid) value) mid)
				((> (list-ref ls mid) value) (binary-search ls value low (- mid 1)))
				((< (list-ref ls mid) value) (binary-search ls value (+ mid 1) high))))))
« Newer Snippets
Older Snippets »
Showing 1-10 of 36 total  RSS