Ruby, Ruby on Rails, Artificial Intelligence and something more ...

Ver y Cocinar, descubre la cocina y disfrútala a simple vista

RailsConf Europe 2007 Speaker

RailsConf Europe 2007 Speaker

Spejman Thumblog!

Friday, 21 September 2007

Making Rails More (Artificially) Intelligent slides & source code

I have just posted the session slides and source code of the RailsConf Europe talk Making Rails More (Artificially) Intelligent that Santiago Bel and me did last Tuesday. You can download them all at

We hope you find them useful. Please note that the ones that are at the web are out of date, now I will send the updated slides to o'reilly.

Friday, 14 September 2007

Railsconf Europe talk: Making Rails More (Artificially) Intelligent

Next Tuesday, my colleague Santiago Bel and me will talk about Artificial Intelligence (AI) at the RailsConf Europe. Should you attend the conference, you are invited to our talk too.

We will show you how easily you can introduce AI algorithms into your rails applications in order to improve them. We will talk about 3 kinds of AI algorithms: Bayesian Networks to make predictions, Naïve Bayes Classifier to classify data, and Genetic Algorithms to solve complex problems as automatical optimization of web ads placement.

We hope you find this session interesting and that you enjoy it!

Monday, 3 September 2007

The Talking Rails Application

After a long time without publishing anything, I have finished an example of what you can do with festivaltts for Ruby: A Ruby on Rails application that talks!

You can test it at:

I hope it gives you ideas for your RoR applications!

Tuesday, 12 June 2007

Text to speech for Ruby on Rails applications

I just published the first usable festivaltts4r version, it comes with its plugin for Ruby on Rails, festivalttsOnRails. With this library and this plugin you can make talk your Ruby and your Ruby on Rails applications.

The rails plugin is so easy to use in Ubuntu linux:

  1. Install tts and mp3 generation libraries:

    sudo apt-get install festival lame

  2. Install the festivalttsOnRails plugin for Ruby on Rails:

    script/plugin install \

  3. Use text_to_flash_player(text) method in your views:

    <%= text_to_flash_player "Talk me!" %>

At the moment the plugin works with a simple english voice but can be very useful as a proof of concept. If people found it interesting it could be improved.

It works so well in Ubuntu Linux, testing in other platforms will be appreciated.

You can also use the festivaltts4r gem in order to make local voice applications with Ruby:
  1. Install tts and mp3 generation libraries:

    sudo apt-get install festival lame

  2. Install festivaltts4r gem:

    sudo gem install festivaltts4r

  3. Include required gems and call to_speech method defined into the String class by festivaltts4r:

    require "rubygems"
    require "festivaltts4r"

    "I'm talking".to_speech

This project has been developed using Festival TTS and lame libraries.

The flash mp3 player used to play the voice has been developed by dew under Creative Commons Attribution-ShareAlike License France license.

More info about festivaltts4r and festivalttsOnRails:

Tuesday, 5 June 2007

Draw with Ruby and Scribble!

Looking at the new blog of _why ( I found Scribble! that it's a Ruby version of NodeBox. Its aim is to make cool graphics using Ruby.

If you want to test it using windows, you can follow the instructions at

I wanted to test it in Linux but I didn't found related information, then I did some research on how to install it. Finally, I made it work in Ubuntu following these steps:

  1. Install cairo and gtk2 ruby libraries and get the scribble code:

    sudo apt-get install libcairo-ruby1.8 libgtk2-ruby
    svn co svn:// scribble

  2. Execute scribble:

    cd scribble

With these steps we'll execute Scribble! and we can do graphs like this:

Scribble! Screenshot

This drawing is uniq because it is randomly generated from this code:

brush.fill = rand(0.1) + 0.9, rand(0.4) + 0.6, rand(0.1) + 0.9, rand(0.1)+0.1

brush.fill = rand(0.1) + 0.9, rand(0.4) + 0.6, rand(0.1) + 0.9, rand(0.1)+0.1
brush.stroke = rand(0.4) + 0.6, 0, 1, 0.2
brush.width = 2

100.times do
circle :center => [rand(size[0]), rand(size[1])], :radius => rand(50) + 10

If you like Scribble! and make some cool graphics, share it putting its code as a comment in this post ;)

Monday, 28 May 2007

Using MySQL reserved words as model names

The generation of a model with a migration in a ruby on rails application lets to the creation of a database table with the pluralization form of the desired model name. In MySQL, a migration will generate a sql statement like:

CREATE TABLE model_name_pluralized (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
`created_on` date DEFAULT NULL, `name` varchar(255) DEFAULT NULL) ENGINE=InnoDB

As you can notice column names are quoted but table name doesn't. If you use as a model name a sigularized form of a MySQL reserved word, the migration that creates this model will generate a statement that will lead to an error like:

Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'databases (`id` int(11) DEFAULT NULL
auto_increment PRIMARY KEY, `created_on` da' at line 1: CREATE TABLE databases (`id` int(11)
DEFAULT NULL auto_increment PRIMARY KEY, `created_on` date DEFAULT NULL, `name` varchar(255)

Last week I read the Josh Susser Laying Tracks slides who
encourages me to write a patch for this issue.

Before writing anything I tried to find if someone has made something related and I found some tickets related in Rails trac:

The most interesting of this tickets is #4905 which fix all MySQL statements to prevent reserved words crash, but I don't know why isn't included in the code because it's last history is from 05/25/2006. #7850 is closed as duplicated because of #4905. And #3631 history finishes with "don't use reserved words" what in my opinion isn't the best solution.

In brief, the problem exists (I can't name my models with names like "database", "exist", ...) and the patch too (#4905). Then, what should we do to fix this problem?

Monday, 14 May 2007

Backup of Bloglines keep new items

If you are a Bloglines user, you probably save the interesting posts using "keep new" feature.

If you have use this reader for a time, the quantity of post saved could be large... Some day I thought that won't be funny to loose all this data, then I build a ruby script to backup bloglines "keep new" items into a xml file.

With this script I learned better Mechanize and Hpricot Ruby libraries.
Este script me ha servido para probar dos librerías muy útiles de Ruby: mechanize y hpricot.

You need these libraries in order to use the script:

gem install json
gem install activesupport
gem install hpricot
gem install mechanize

And here you have the script. I think is very understandable. I hope it helps you to backup your bloglines account (remember to change EMAIL and PASSWORD values with yours) or to understand better how mechanize and hpricot works.

require "rubygems"
require "hpricot"
require "json"
require "mechanize"
require "active_support"

# Reads a bloglines javascript tree structure that has all
# feeds data.
def read_tree( tree_base, label = "" )
tree_base.each do |tree|
if tree["kids"]
read_tree tree["kids"], label + "/" + tree["n"]
@feeds << [tree["n"], label, tree["kn"], "{tree["id"]}&site=#{tree["sid"]}"]

# Add more memory to hpricot otherwise couldn't load some webs.
Hpricot.buffer_size = 262144

agent =
page = agent.get ''

form = page.forms[1] = 'EMAIL'
form.password = 'PASSWORD'

page = agent.submit form

# Get the bloglines sindicated feeds
menu_page = agent.get ""
start_text = "var initTreeData = "
end_text = "\n;\n"
js_feeds_tree_str = menu_page.content[menu_page.content.index(start_text)+start_text.size..menu_page.content.index(end_text)]
feeds_tree = JSON.parse js_feeds_tree_str.gsub("\\","")
@feeds = []

puts "<bloglines_saves>"
@feeds.each do |feed|

page = agent.get feed[3]
doc= Hpricot(page.content)

# get the content of all saved feed posts
content = ((doc/"body")/"td.article")
next if content.empty?
puts "<feed name=\"#{feed[0].strip}\" folder=\"#{feed[1].strip}\">"

# Iterate each saved feed post
((doc/"body")/"a.bl_itemtitle").each_with_index do |title, index|
puts "<feed_save title=\"#{title.inner_html.strip}\" href=\"#{title.attributes["href"]}\">"
puts content[index].inner_html.to_xs
puts "</feed_save>"
puts "</feed>"

puts "</bloglines_saves>"

Más información:

Thursday, 29 March 2007

General Genetic Algorithms for Ruby (gga4r) release 0.9

General Genetic Algorithms for Ruby (gga4r) is a library for executing generic algorithms easily.

Only 3 steps are needed in order to work with gga4r:

  1. Choose a clase to evolve and define for her the fitness, combine and mutate methods.

  2. With an array of last defined class instances (initial population), create a GeneticAlgorithm object.

  3. Call GeneticAlgorithm's evolve method as many times as you want.

More info:

Thursday, 8 March 2007

DEISA Sessions (Distributed European Infrastructure for Supercomputing Applications)

This week I'm taking DEISA training sessions in Barcelona, they are teaching us how to submit tasks into most powerful supercomputers of Europe (DEISA Architecture).

Within this supercomputers we can meet Marenostrum, nowadays is the most powerful supercomputer in Europe and the World's fifth most powerful as you can see in It's made of 10.240 CPUs Power PCs, 20 TeraBytes of RAM and 370 TeraBytes of disk. It's placed in a old chapel which gives it a special feeling.

Today I've been lucky and I could visit inside Marenostrum as you can see in this photos:



Marenostrum Core

More photos:

Monday, 22 January 2007

Bayesian Networks for Ruby release 0.9

Bayesian Networks for Ruby (bn4r) permits create, modify, solve, import and export Bayesian Networks, give it a try and infer some probabilities with them!

This release includes suport for discrete variable nodes, now you can use nodes with values like {small, normal, big} instead of {true, false}.

Export to Microsoft Belief Network has been improved.

As previous releases you can solve your networks with enumeration_ask, prior_sample, rejection_sampling and likelihood_weighting algorithms.

Testing work will be appraised, if you are interested please contact me in or at sergio.espeja (you know what ...)

More info:

About Me

Barcelona, Barcelona, Spain