Archive for the 'Ruby' Category

Ruby.new

Thursday, November 22nd, 2007

I gave a talk on Ruby to TELECOM Lille 1’s LUG last Thursday. It was entitled “Ruby.new” and lasted 30 minutes. It was an introduction to Ruby without being a tutorial. I emphasized on the cool features that make Ruby apart. Apparently, it was pretty well received and there were about 15 people attending it.

You can download my slides in ODP and PDF (French).

Multiple dictionary sources in Fantasdic

Thursday, November 1st, 2007

Over the past few weeks, I have slowly but surely been adding multiple dictionary sources support to Fantasdic. Until recently, Fantasdic had been a DICT client only, that is, Fantasdic connected to DICT servers (as configured by the user in the settings) in order to retrieve definitions. I thought it would always be like that and I had even objected to change that in gnome-dictionary but I’ve finally changed my mind. As I said some time ago, a great deal of Fantasdic’s source code is only user interface source code. If making a dictionary application means spending so much time on user interface, it’s best to make it general-purpose…

Currently, Fantasdic includes two new kinds of source, in addition to DICT servers:

- Google Translate
- EDICT files

Basically, it works like a plugin system. Source plugins can either be distributed and installed with Fantasdic or installed manually in $HOME/.fantasdic/sources/ for third-party plugins. Writing a new source plugin is merely a matter of extending a base class and implementing a few required methods. Plugins are written in Ruby.

Hopefully, the user interface remained as simple as it was.

Fantasdic screenshot
Fantasdic searching in an EDICT file. EDICT is a famous dictionary format for anyone learning Japanese.

Some sources may require additional fields to be configured by the user. For example, the DICT server source requires a server host and port. The EDICT file source requires a file path to be specified. The user interface for those additional fields is defined directly in the source plugins.

Fantasdic screenshot

Fantasdic screenshot
For this source, a file must be selected…

Fantasdic screenshot
With the Google Translate source, you need to select your languages for the translations.

Fantasdic screenshot
Fantasdic, using Google Translate.

I hope more and more sources can be added :) Ideally all source plugins should be multi platform. Here are a few suggestions (of course, I’m counting on you to implement them ;-)):

- dictd file: search directly in files aimed for the dictd server. See “man dictd” for a description of the format and tools/ in Fantasdic’s source code for some starters.

- Stardict file. There’s a file describing the format in Stardict’s source code. Likewise, tools/ has a script to convert stardict files, it may be a good starter.

- Stardict server. Stardict authors have created their own protocol and they’re running a server with quite some dictionaries. Directly see Stardict’s source code or use a packet sniffer.

- Epwing dictionaries. You’ll need to use rubyeb, the Ruby bindings to the excellent libeb.

- Wikpedia/Wiktionary. This source plugin would simply perform an HTTP request to the appropriate site. Greg Hewgill kindly accepted to share his code to clean mediawiki syntax and make it more readable. I’m quoting an email he sent to me:

The current state of my code can be found at:
http://hewgill.com/viewvc/wiktiondict/trunk/

Feel free to use any of my code (or the algorithms therein) to format
mediwiki data. I imagine you already know this, but you can fetch the
raw output for individual pages using a url like this:
http://en.wiktionary.org/w/index.php?title=test&action=raw

In fact, you can also add &templates=expand to that url and mediawiki
does all the hard template work! I found the docs at:
http://www.mediawiki.org/wiki/Manual:Parameters_to_index.php

Waiting for your comments and your source plugins!

Translating Wikipedia articles more easily

Wednesday, April 11th, 2007

It is not easy to explain the following with plain sentences so let’s take an example. Say I want to translate the following paragraph from English to French:

Tokyo is known for its many museums. Located in [[Ueno Park]] are the [[Tokyo National Museum]], the country's largest museum and specializing in traditional [[Japanese art]]; the National Museum of Western Art; and the Tokyo Metropolitan Art Museum, which contains collections of Japanese [[modern art]] as well as over 10,000 Japanese and foreign films.

In order to complete the translation, I will need the French article name for [[Ueno Park]], [[Tokyo National Museum]], [[Japanese art]] etc. Seeking all those names is quite boring and time-consuming, isn’t it ? So I have written a little tool in Ruby that does that for us. In this very example, the program would have output:


----------
Ueno_Park: interwiki link to fr found (Parc de Ueno)
Tokyo_National_Museum: interwiki link to fr found (Musée national de Tōkyō)
Japanese_art: interwiki link to fr found (Art japonais)
modern_art: interwiki link to fr found (Art moderne)
----------
Tokyo is known for its many museums. Located in [[Parc de Ueno]] are the [[Musée national de Tōkyō]], the country's largest museum and specializing in traditional [[Art japonais]]; the National Museum of Western Art; and the Tokyo Metropolitan Art Museum, which contains collections of Japanese [[Art moderne]] as well as over 10,000 Japanese and foreign films.

More explanations and download here.

nihongobenkyo.org launched

Saturday, March 3rd, 2007

I spent quite some time this week working on a public dictionary server focusing on Japanese language. The idea is that you just have to install a dictionary client, such as Fantasdic, in order to get a full-featured Japanese dictionary. Add “nihongobenkyo.org” in the settings and voilà ! I have decided to abandon the former Nihongo Benkyo project because I think that the Fantasdic + nihongobenkyo.org server approach advantageously replace it. See nihongobenkyo.org for more details.

今週一般向け日本語辞書サーバーを作るために、時間をけっこう費した。目的はFantasdicっていった辞書クライアントをインストールして、セッティングで ”nihongobenkyo.org”を書けば、ほら!日本語辞書が手に入ること。この方が便利だと思うから、昔の Nihongo Benkyoプロジェクトはもう止めると決めた。詳細はnihongobenkyo.orgを見てください。日本語のバージョンもあるよ。

If you are interested in creating your own dictionaries for the dictd server, I have written a class in Ruby language that does just that. Compared to the dictfmt utility distributed with dictd, its main advantage is that it is possible to associate more than one index entry with one definition. In this process, I also learned a useful trick. The following code allows to write a file and sort it “on the fly”. Easy but useful.

自分自身のdictdの辞書を作ることに興味があったら、ぼくはそのためにルビーのクラスを書いた。dictdのdictfmtツールよりいいのは、いくつかのインデックスのエントリーを一つの説明にまとめることが出来る。またそれを調べながら、いい勉強が出来た。下のコードはファイルに書いて、同時にそのファイルをソートするためのもの。簡単で便利。(^^ )


f = IO.popen("sort > file_name", "w")
["d","b","a","c"].each do |line|
f.write(line + "\n")
end
f.close

will output :

a
b
c
d

TTF/Ruby, first release!

Thursday, November 9th, 2006

I am pleased to announce the first release (version 0.1) of TTF/Ruby, under the terms of the GNU GPL.

TTF/Ruby is a pure Ruby library to read and write TrueType fonts.

Tables supported are :

- Cmap *
- Cvt *
- Fpgm *
- Gasp
- Glyf
- Head
- Hhea
- Hmtx
- Kern *
- OS/2
- Post *
- Prep *
- Vhea
- Vmtx

(Tables marked with an * are only partially supported)

API documentation is written directly in the source code and may be generated with the following command-line:

$ rdoc –main “Font::TTF::File”

As you noticed, this release is marked 0.1 so do not expect API compatibility for the next releases.

The tarball also ships some useful tools (and proofs of concept) based on TTF/Ruby.

ttfdump: a command-line tool to extract informations about a font.

ttfsubset: a tool which from a font and an input file generates a subset from this font containing only characters in the input file. Maybe useful to embed a lighter version of a font in a document or in an embedded system.

ttfcairoglyphviewer: renders a selected glyph using Ruby/GTK, Rcairo and TTF/Ruby. It also displays markers for corner points, curve control points, and implicit points.

ttfglyph2svg: prints to stdout a selected glyph in SVG format.

Comments are of course welcome. And there is a large TODO list for the braves ;-)

I would like to thank Evermore Software, China, where I am currently an intern, for giving me the permission to release this project (which started as a prototype for a Java program).

Download: http://www.mblondel.org/files/ttf-ruby/ttf-ruby-0.1.tar.gz


The 愛 japanese Kanji rendered thanks to Ruby/GTK, RCairo and TTF/Ruby (i.e. without FreeType).

Transparent caching in Ruby

Tuesday, September 19th, 2006

Found the following code in the ruby-talk archives. It allows to cache what a method returns.

class Class
    alias_method :__new__, :new

    def cached_method(*methods)
        @cached_methods ||= []
        @cached_methods += methods
        @cached_methods.uniq!
    end

    def new(*args, &block)
        obj = __new__(*args, &block)
        klass = obj.class
        @cached_methods ||= []

        @cached_methods.each do |name|
            meth = obj.method(name)
            i = 1

            while klass.instance_variables.member?("@_defc_#{i}")
                i += 1
            end

            wrapped_name = "_defc_#{i}"
            cache = klass.class_eval("@#{wrapped_name} = {}")

            wrapper = Proc.new do |*args_a|
                arg_hash = args_a.hash

                if cache.has_key?(arg_hash)
                    cache[arg_hash]
                else
                    cache[arg_hash] = meth.call(*args_a)
                end
            end

            klass.send(:alias_method, wrapped_name, name)
            klass.send(:define_method, name, wrapper)
        end

        return obj
    end
end

if __FILE__ == $0
    class Klass
        def foo; end
        def bar; end
        cached_method :foo, :bar
    end
end

New tools for the DICT protocol

Monday, June 26th, 2006

So you are interested in the DICT protocol and would like to know where to find some interesting tools and data to use with your favourite DICT client (e.g. Fantasdic…)? I have made a bunch of tools that you should have a look at!

(more…)

Encoding detection in Ruby

Thursday, May 11th, 2006

I’ve just discovered Universal Encoding Detector. You give it a string and it returns its encoding and the confidence in the result! It is very useful. From what I’ve tested, it works very well.

To install it:

gem install chardet

Example :


require 'rubygems'
require 'UniversalDetector'
require 'net/http'
Net::HTTP.version_1_2
Net::HTTP.start( 'yahoo.co.jp' ) {|http|
data = http.get("/").body
p UniversalDetector::chardet(data)
#=> {"encoding"=>"EUC-JP", "confidence"=>0.99}
}

Fantasdic now in the GNOME CVS

Thursday, March 9th, 2006

After a few weeks of evaluation, Fantasdic has finally been accepted by the GNOME accounts team and is now in the GNOME CVS.

To retrieve the source code from the anonymous mirror repository:

cvs -d:pserver:anonymous@anoncvs.gnome.org:/cvs/gnome login
cvs -z3 -d:pserver:anonymous@anoncvs.gnome.org:/cvs/gnome co -P fantasdic

I hope it will help Fantasdic attract more contributors, translators and above all package maintainers. As far as I know, this is the first Ruby program to be hosted on the GNOME CVS. Maybe a first step toward more adoption of that fabulous language in the GNOME community?

Fantasdic 1.0-beta1.1 (fix for Windows)

Sunday, February 19th, 2006

This release fixes a bug that prevented Fantasdic from running under Windows (thanks to Gabriele Renzi for telling me) and some user interface tweaks (thanks to John Spray for the patch).

Screenshot : Fantasdic under Windows

Download : fantasdic-1.0-beta1.1.tar.gz