Using raspell with your own custom dictionary digg delicious su

Posted by daniel
on Wednesday, August 08

A good search engine should be smart enough to detect spelling errors and provide suggestions. For example if you misspelled a word when using Google search, it will ask Do you mean:.... It will be cool to have such feature for our own search engine. So I started to explore possible solutions what works will rails and came upon this blog post by Evan Weaver. He has implemented a ruby gem raspell to interface with Aspell. That sounds like a good solution and I decided to check it out.

1. Installing Aspell

As mentioned in Evan’s post to install Aspell just type the following command from terminal:


sudo port install aspell aspell-dict-en

This will install both the aspell library and aspell English dictionary.

2. Install raspell

This is straight forward.


sudo gem install raspell -- --with-opt-dir=/opt/local

3. Setting up your own dictionary

For my case I needed aspell to check against my own custom dictionary. I create a text file containing the list of words with each word on a new line save it as wordlist. e.g.


John
Simon
Mae
Tim
Louis

Then run it with the following command.


aspell --lang=en create master ./en_SG-name.rws < wordlist

Note that for the language option I’m using ‘en’ which is the standard code for English. If you are generating a word list in other languages you will have to change lang option accordingly. The name of the output file is important because Aspell only pick up dictionary file in a certain naming format. The dictionary file should start with the language code then follow by a country code which is optional. For my case I have chosen SG because I do not want to override the default en dictionaries from Aspell. The ‘name’ after the ’-’ is actually the jargon. So if you are generating a word list of street names in US you might want to name you dictionary as en_US-street.rws. Once you have generated the dictionary you will have to place it in Aspell default dictionary directory.


sudo mv en_SG-name.rws /opt/local/share/aspell/

Because Aspell will only look for dictionaries with .multi file extension in the default directory. I’ll need to create a en_SG-name.multi file for my new dictionary

sudo vi /opt/local/share/aspell/en_SG-name.multi

and add the following line.


add en_SG-name.rws

You can add multiple dictionary to this file. If you needed the common English dictionary together with your custom one. Just add it accordingly.

4. Using your own dictionary in ruby.

Finally to use the dictionary I have just created.


require 'rubygems'
require 'raspell'

sp = Aspell.new('en_SG', 'name')
sp.suggest('Mea')
=> ["Mae"]

The first parameter is the language part of the file name and the second is the jargon. So if you have named your dictionary as en_US-street.multi then you will need to initialize it with


sp = Aspell.new('en_US', 'street')

Rails plugin to combine CSS and Javascript files digg delicious su

Posted by jason
on Friday, July 27

While multiple CSS and Javascript files aid manageability and facilitate a modular style of development, HTTP performance takes a hit because multiple requests are sent to the server for each of these files.

When we first launched HBO Asia’s recently revamped site, we found that the response time for pages to load was somewhat slow and thought that the many separated Javascript and CSS assets we were using might be a contributing factor.

David Heinemeier Hansson’s keynote at Railsconf 2007 mentioned that the upcoming Rails 2.0 will have a feature to combine all Javascript and CSS assets to help with this problem. In the meantime, however, we made do by manually combining the assets.

True enough, after doing so, the site’s performance was a little better. However, maintainability was compromised and every time I updated the combined files, I would have to update the individual files as well (as other parts of the site that do not need all the assets might be referencing the individual files).

Tired of such unproductivity, I decided to search for a Rails plugin that can automate this process and found CachedAssets. I’m currently testing this on development and will push this out to production soon if all goes well.

Setting up Rspec, Autotest and Growl on Mac OS X digg delicious su

Posted by daniel
on Sunday, July 22
p> We have recently decided to use BDD for our upcoming projects. To make testing more fun and efficient I have hook up Growl with ZenTest’s autotest to display test results from Rspec. Some of the following instructions is taken from http://wincent.com/knowledge-base/Setting_up_autotest_to_use_Growl

What follows are the instructions to install the software to setup your own if you like to try out.

  1. Growl
  2. ZenTest
  3. Rspec

Installing Growl

  1. Download the dmg package from Growl
  2. Open the disk image, and double click in the Growl.prefPane icon.
  3. Next is to install growlnotify . Double click on the Growl disk image again if you have close it and execute the following commands from a terminal
 cd /Volumes/Growl/Extras/growlnotify
 sudo ./install.sh
 cd
 hdiutil detach /Volumes/Growl 

Installing ZenTest

ZenTest provides 4 different tools and 1 library: zentest, unit_diff, autotest, multiruby, and Test::Rails. For our purpose we will be using autotest. The latest version of ZenTest has added rspec support and it’ll auto detect rspec and run tests in the background whenever there’s any changes to the specs.

Installing ZenTest is easy just run the following command:

 sudo gem install ZenTest 

Installing Rspec

To test rails with rspec you will need to install rspec and rspec_on_rails plugins . Run the following command from your project root directory.

 script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec
 script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails 

After you have installed the plugins you will need to run following command once to bootstrap your Rails app with RSpec.

 script/generate rspec 

Customize Autotest

The last step is to customize Autotest. Download the following 2 images and save them under ~/.autotest_images (create this directory if it does not exist). You can also create your own images if you want.

Open your favorite editor and copy and paste the code, then save the file as .autotest under your user home directory.

 module Autotest::Growl

   def self.growl title, msg, img, pri=0, sticky=""
     system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{sticky}"
   end

  Autotest.add_hook :ran_command do |at|
    results = [at.results].flatten.join("\n")
    output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+not implemented)?/) 
      if output
        if $~[2].to_i > 0
          growl "FAIL", "#{output}", "~/.autotest_images/fail.png", 2
       else
         growl "Pass", "#{output}", "~/.autotest_images/pass.png" 
       end
     end
   end 
 end 

Configure Growl

Finally you can configure growl to with different display style via System Preference -> Growl. Try out all the different display style to see which one you prefer. Personally I like “Music Video”.

Once you have setup all the above. Run autotest under your project root directory and you will see Growl in action. Have fun!!

Naturally sort strings with numbers using Rails digg delicious su

Posted by jason
on Monday, July 16

A recent project required natural sorting of strings with numbers in them, i.e. in the order:

Series 1 
Series 2 
Series 3 
Series 10 
Series 23 
Series 35 

Sorting these strings 'unnaturally' would result in the following:

Series 1 
Series 10 
Series 2 
Series 23 
Series 3 
Series 35 

I was kind of surprised that Ruby nor Rails had this feature built-in. However, thankfully, the open-source community helped me solve my problem without cracking my brains too much. What I found were these:

The first solution extends the Array object with two methods: natcmp and natcmp!. Both of which will naturally sort an array of strings:

array_of_strings = ["Series 10", "Series 23", "Series 3", "Series 35", "Series 2", "Series 1"] 
array_of_strings.natcmp! # => ["Series 1", "Series 2", "Series 3", "Series 10", "Series 23", "Series 35"] 

The second piece of code adds a class method to the String object, accepting two strings as parameters to be sorted, returning either 1 or -1 (similar to the <=> method). I eventually used this solution, as what I needed to sort was an array of ActiveRecord objects, which I could do as follows:

array_of_records = Series.find(:all) 
array_of_records.sort! {|x,y| String.natcmp(x.name, y.name)} 

Sending emails using GMail smtp with ActionMailer digg delicious su

Posted by daniel
on Wednesday, July 11

If you have been following our blog you should have read the post Swiss Amry Knife 2.0 . We have recently switch our mail server to Google Apps. This is nice but that also means we have to change our ActionMailer server settings to GMail’s smtp server and this pose a problem. Because default ActionMailer does not support SSL authentication which is required by GMail. I tried searching for a solution and lucky enough I managed to came across this post and got to work! Check out the link if you are facing the same problem.

Install Gem in Local User Directory for Back Up Purpose digg delicious su

Posted by chardy
on Wednesday, July 11

It seems not maintainable when we have too many gems installed on your local machine gem repositories, indeed in UNIX machine, our access is granted by user group itself. How if one day, my harddisk crashes and I loss all my data, BTW, I only backup my home folder in this case. So due to this incident, I need to reinstall all my ruby gems again to get Ruby on Rails development environment ready to use. Too much work.

Try this to solve the issue, by installing all your gem inside you home folder.

$ cd ~ 
$ mkdir .gem 
$ export GEM_PATH=~/.gem 
$ gem install -i ~/.gem haml 

You can also put this export GEM_PATH=~/.gem to your ~/.profile or ~/.bash_profile.
Whats the .gem directory look like:

$ ls ~/.gem 
./ ../ bin/ cache/ doc/ gems/ source_cache specifications/