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-4 of 4 total  RSS 

Ruby dictionary username generation

Generate a new random name from dictionary words.

DICT_PATH = '/usr/share/dict/words'
DICT_SIZE = 234936

def self.generated_name words = 2, length = 23
  name = 'a'*(length+1)
  while name.length > length
    name = (1..words).map{%x[sed -n '#{rand(DICT_SIZE)} {p;q;}' '#{DICT_PATH}'].chomp.capitalize}.join
  end
end

BloominSimple: Ultra-easy, pure Ruby Bloom filter library

Requires BitField.

#        NAME: BloominSimple
#      AUTHOR: Peter Cooper
#     LICENSE: MIT ( http://www.opensource.org/licenses/mit-license.php )
#   COPYRIGHT: (c) 2007 Peter Cooper
# DESCRIPTION: Very basic, pure Ruby Bloom filter. Uses my BitField, pure Ruby
#              bit field library (http://snippets.dzone.com/posts/show/4234).
#              Supports custom hashing (default is 3).
#
#              Create a Bloom filter that uses default hashing with 1Mbit wide bitfield
#                bf = BloominSimple.new(1_000_000)
#              
#              Add items to it
#                File.open('/usr/share/dict/words').each { |a| bf.add(a) }
#
#              Check for existence of items in the filter
#                bf.includes?("people")     # => true
#                bf.includes?("kwyjibo")    # => false
#
#              Add better hashing (c'est easy!)
#                require 'digest/sha1'
#                b = BloominSimple.new(1_000_000) do |item|
#                  Digest::SHA1.digest(item.downcase.strip).unpack("VVVV")
#                end
#
#              More
#                %w{wonderful ball stereo jester flag shshshshsh nooooooo newyorkcity}.each do |a|
#                  puts "#{sprintf("%15s", a)}: #{b.includes?(a)}"
#                end
#
#                 #  =>   wonderful: true
#                 #  =>        ball: true
#                 #  =>      stereo: true
#                 #  =>      jester: true
#                 #  =>        flag: true
#                 #  =>  shshshshsh: false
#                 #  =>    nooooooo: false
#                 #  => newyorkcity: false

require 'benchmark'
require 'bitfield'

class BloominSimple
  attr_reader :bitfield, :hasher
  
  def initialize(bitsize, &block)
    @bitfield = BitField.new(bitsize)
    @size = bitsize
    @hasher = block || lambda do |word|
      word = word.downcase.strip
      [h1 = word.sum, h2 = word.hash, h2 + h1 ** 3]
    end
  end
  
  def add(item)
    @hasher[item].each { |hi| @bitfield[hi % @size] = 1 }
  end
  
  def includes?(item)
    @hasher[item].each { |hi| return false unless @bitfield[hi % @size] == 1 } and true
  end
end

Shell script to look up words at dictionary.com from the command line

The following will look up a word on dictionary.com from the command line, where $1 is the word you want to look up, entered in as a parameter on the command line. (Lynx is a text-based browser that handles the html-to-text conversion for us.)

#!/bin/sh 
lynx -dump -nolist -pseudo_inlines                \
  'http://dictionary.reference.com/search?q='$1'&r=67'  \
  | tail -n +13 | less -r

Counting characters in string

>>> s = 'a;jfkd;aflhakfhaskfjalghlakfhfnkjafyksd'
>>> cnt = {}
>>> for c in s:
	cnt[c] = cnt.get(c,0) + 1

>>> print cnt
{'a': 7, 'd': 2, 'g': 1, 'f': 7, 'h': 4, 'k': 6, 'j': 3, 'l': 3, 'n': 1, 's': 2, 'y': 1, ';': 2}

This can be used to count any distribution.
Note the use of dict.get(key,default) to set to 0
if the key is not avaiable.
If this were perl, I would just do a
cnt[c] += 1

But python will give an error instead of returning 0.
It's not too bad, though.
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS