<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: dictionary code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 07:53:45 GMT</pubDate>
    <description>DZone Snippets: dictionary code</description>
    <item>
      <title>Ruby dictionary username generation</title>
      <link>http://snippets.dzone.com/posts/show/4536</link>
      <description>Generate a new random name from dictionary words.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DICT_PATH = '/usr/share/dict/words'&lt;br /&gt;DICT_SIZE = 234936&lt;br /&gt;&lt;br /&gt;def self.generated_name words = 2, length = 23&lt;br /&gt;  name = 'a'*(length+1)&lt;br /&gt;  while name.length &gt; length&lt;br /&gt;    name = (1..words).map{%x[sed -n '#{rand(DICT_SIZE)} {p;q;}' '#{DICT_PATH}'].chomp.capitalize}.join&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Sep 2007 13:57:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4536</guid>
      <author>elliottcable (elliott cable)</author>
    </item>
    <item>
      <title>BloominSimple: Ultra-easy, pure Ruby Bloom filter library</title>
      <link>http://snippets.dzone.com/posts/show/4235</link>
      <description>Requires &lt;a href="http://snippets.dzone.com/posts/show/4234"&gt;BitField.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#        NAME: BloominSimple&lt;br /&gt;#      AUTHOR: Peter Cooper&lt;br /&gt;#     LICENSE: MIT ( http://www.opensource.org/licenses/mit-license.php )&lt;br /&gt;#   COPYRIGHT: (c) 2007 Peter Cooper&lt;br /&gt;# DESCRIPTION: Very basic, pure Ruby Bloom filter. Uses my BitField, pure Ruby&lt;br /&gt;#              bit field library (http://snippets.dzone.com/posts/show/4234).&lt;br /&gt;#              Supports custom hashing (default is 3).&lt;br /&gt;#&lt;br /&gt;#              Create a Bloom filter that uses default hashing with 1Mbit wide bitfield&lt;br /&gt;#                bf = BloominSimple.new(1_000_000)&lt;br /&gt;#              &lt;br /&gt;#              Add items to it&lt;br /&gt;#                File.open('/usr/share/dict/words').each { |a| bf.add(a) }&lt;br /&gt;#&lt;br /&gt;#              Check for existence of items in the filter&lt;br /&gt;#                bf.includes?("people")     # =&gt; true&lt;br /&gt;#                bf.includes?("kwyjibo")    # =&gt; false&lt;br /&gt;#&lt;br /&gt;#              Add better hashing (c'est easy!)&lt;br /&gt;#                require 'digest/sha1'&lt;br /&gt;#                b = BloominSimple.new(1_000_000) do |item|&lt;br /&gt;#                  Digest::SHA1.digest(item.downcase.strip).unpack("VVVV")&lt;br /&gt;#                end&lt;br /&gt;#&lt;br /&gt;#              More&lt;br /&gt;#                %w{wonderful ball stereo jester flag shshshshsh nooooooo newyorkcity}.each do |a|&lt;br /&gt;#                  puts "#{sprintf("%15s", a)}: #{b.includes?(a)}"&lt;br /&gt;#                end&lt;br /&gt;#&lt;br /&gt;#                 #  =&gt;   wonderful: true&lt;br /&gt;#                 #  =&gt;        ball: true&lt;br /&gt;#                 #  =&gt;      stereo: true&lt;br /&gt;#                 #  =&gt;      jester: true&lt;br /&gt;#                 #  =&gt;        flag: true&lt;br /&gt;#                 #  =&gt;  shshshshsh: false&lt;br /&gt;#                 #  =&gt;    nooooooo: false&lt;br /&gt;#                 #  =&gt; newyorkcity: false&lt;br /&gt;&lt;br /&gt;require 'benchmark'&lt;br /&gt;require 'bitfield'&lt;br /&gt;&lt;br /&gt;class BloominSimple&lt;br /&gt;  attr_reader :bitfield, :hasher&lt;br /&gt;  &lt;br /&gt;  def initialize(bitsize, &amp;block)&lt;br /&gt;    @bitfield = BitField.new(bitsize)&lt;br /&gt;    @size = bitsize&lt;br /&gt;    @hasher = block || lambda do |word|&lt;br /&gt;      word = word.downcase.strip&lt;br /&gt;      [h1 = word.sum, h2 = word.hash, h2 + h1 ** 3]&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def add(item)&lt;br /&gt;    @hasher[item].each { |hi| @bitfield[hi % @size] = 1 }&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def includes?(item)&lt;br /&gt;    @hasher[item].each { |hi| return false unless @bitfield[hi % @size] == 1 } and true&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 02 Jul 2007 02:14:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4235</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Shell script to look up words at dictionary.com from the command line</title>
      <link>http://snippets.dzone.com/posts/show/875</link>
      <description>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.)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/sh &lt;br /&gt;lynx -dump -nolist -pseudo_inlines                \&lt;br /&gt;  'http://dictionary.reference.com/search?q='$1'&amp;r=67'  \&lt;br /&gt;  | tail -n +13 | less -r&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 09 Nov 2005 05:54:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/875</guid>
      <author>madphilosopher (Darren Paul Griffith)</author>
    </item>
    <item>
      <title>Counting characters in string</title>
      <link>http://snippets.dzone.com/posts/show/511</link>
      <description>&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; s = 'a;jfkd;aflhakfhaskfjalghlakfhfnkjafyksd'&lt;br /&gt;&gt;&gt;&gt; cnt = {}&lt;br /&gt;&gt;&gt;&gt; for c in s:&lt;br /&gt;	cnt[c] = cnt.get(c,0) + 1&lt;br /&gt;&lt;br /&gt;&gt;&gt;&gt; print cnt&lt;br /&gt;{'a': 7, 'd': 2, 'g': 1, 'f': 7, 'h': 4, 'k': 6, 'j': 3, 'l': 3, 'n': 1, 's': 2, 'y': 1, ';': 2}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This can be used to count any distribution.&lt;br /&gt;Note the use of dict.get(key,default) to set to 0&lt;br /&gt;if the key is not avaiable. &lt;br /&gt;If this were perl, I would just do a&lt;br /&gt;&lt;code&gt;cnt[c] += 1&lt;/code&gt;&lt;br /&gt;But python will give an error instead of returning 0.&lt;br /&gt;It's not too bad, though.</description>
      <pubDate>Tue, 26 Jul 2005 18:39:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/511</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
