Spejman On Rails

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 bee.com.es

We hope you find them useful. Please note that the ones that are at the railsconfeurope.com 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: http://thetalkingrailsapp.sergioespeja.com/

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: festivaltts4r.rubyforge.org

Tuesday, 5 June 2007

Draw with Ruby and Scribble!

Looking at the new blog of _why (http://hackety.org) 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 http://nex3.leeweiz.net/posts/3

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://hamptoncatlin.com/scribble/trunk 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"], "http://www.bloglines.com/myblogs_display?sub=#{tree["id"]}&site=#{tree["sid"]}"]

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

agent = WWW::Mechanize.new
page = agent.get 'http://www.bloglines.com/login'

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

page = agent.submit form

# Get the bloglines sindicated feeds
menu_page = agent.get "http://www.bloglines.com/myblogs_subs"
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:

About Me

Barcelona, Barcelona, Spain