biais.org

Thursday 2 April 2009

Where the World Sees Junk, Africa Recycles: Maker Faire Africa

Maker Faire Africa (MFA), a celebration of African ingenuity, innovation and invention, will take place August 13-15 at the Ghana-India Kofi Annan Centre of Excellence in ICT in Ghana's capital, Accra.

You also should check out the Afrigadget website:

dedicated to showcasing African ingenuity. A team of bloggers and readers contribute their pictures, videos and stories from around the continent. The stories of innovation are inspiring. It is a testament to Africans bending the little they have to their will, using creativity to overcome life’s challenges.

Monday 5 January 2009

Genetic Algorithm in Python to Generate File Converters

In an old post I wrote about a metaheuristic: particle swarm optimization (PSO). It was the simplest code to demonstrate what PSO looks like. Today I'm writing about genetic algorithm, another metaheuristic inspired by evolution of species. You can read the description of the algorithm on the genetic algorithm article on Wikipedia.

I applied the algorithm to a problem that is not really one: trying to help lazy programmers to write file converters. I had to write file converters to unify all (more or less) formated input files into one kind of CSV file. Each of these converters is made using the right combination of filtering / regexp matching / line splitting.

So I wrote a program based on genetic algorithm where an individual is composed by 3 "genes":

  • a list of filters (remove consecutive spaces, replace tabs by spaces, ...)
  • a list of basics regexp (int, float, date, line separator, ...)
  • a list of cleaning functions (remove empty cell, merge cells, ...)

The fitness of an individual is calculated using a sample file to parse that goes through the genes (filters / regexp / cleaning functions). The result of this process is a CSV file that can be compared to the expected sample result (that I wrote manually). The comparison uses the SequenceMatcher of the difflib Python stantard module, it returns the fitness: a float. A fitness close to 1.0 means the results is close to the expected CSV format. When the fitness is exaclty 1.0, that means the converter works perfectly for the sample.

Here is a sample file I wanted to convert:

08/12   	 Hello world 06/12
Paris 121231231 	  	- 22,29
08/12 	Something something ... 04/12
1111 1111 1111 1111 	  	- 14,35
08/12 	something else
	  	- 12,96
26/11 	Vir AAAAA
AAAAAA 2008	  	264,51

into this csv file:

"08/12","Hello world 06/12","Paris 121231231","-22,29"
"08/12","Something something ... 04/12","1111 1111 1111 1111","-14,35"
"08/12","something else","","-12,96"
"26/11","Vir AAAAA","AAAAAA 2008","264,51"

The program prints for each generation the 5 bests individual (I'm using elitism, so the best individual is always kept from a generation to the next one). A sample run:

$ python ga.py tests/sample1.txt tests/sample1.expected result1.pickled
Generation: 0 (mutation rate=10)
0.53772
0.42507
0.30406
0.28598
0.26389
Generation: 1 (mutation rate=10)
0.53772
0.42507
0.32684
0.30406
0.26799

...

Generation: 271 (mutation rate=15)
1.0
0.96025
0.95107
0.91779
0.87886
"08/12","Hello world 06/12","Paris 121231231","-22,29"
"08/12","Something something ... 04/12","1111 1111 1111 1111","-14,35"
"08/12","something else","","-12,96"
"26/11","Vir AAAAA","AAAAAA 2008","264,51"

filters: str_remove_consecutive_spaces, str_remove_somespaces, str_strip, str_remove_consecutive_spaces
regex: ([0-9]{2}/[0-9]{2})(.+?)(
)(.+?)(-?[0-9]+[.,]+[0-9]+)
cleaners: clean_strip, _in, _in, _in
Conclusion

The good:

  • It's fun to see your program evolve ;)
  • You are lazy and don't want to write _many_ of this kind of file converters
  • With a good interface and if the basic functions and genes size are well defined, a non-programmer can create his own parser (I think this is the only argument to use it).

The bad:

  • The resulted parsers are not optimal

The ugly:

  • Of course it's quite easy to write such genes by hand
  • It may never converge to 1.0 and generated parsers of fitness != 1.0 are useless (this may not be the case for other kind of GA applications)
  • You need many well chosen primitives to cover a wide set of solutions
Download

gaparser-0.1.tar.bz2: source code and examples.

Saturday 6 December 2008

OpenCalais: Semantic Analysis Web Service

OpenCalais is a free web service that can perform semantic analysis on any English text. It processes the text sent in your request and respond with extracted concepts and relationships. It's a great tool if you want to play with semantics and if you want to add some nice features to your website / blog.

As an example, I tried to send the text from a this small article about Ruby and Python. Note : For readability I kept only interesting data from the response :

<!-- 
Relations: 
ProgrammingLanguage: Python, Ruby
--> 
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:c="http://s.opencalais.com/1/pred/">
  <rdf:Description rdf:about="...">
    <!-- ProgrammingLanguage: Python; --> 
    <c:detection>[similarities and differences between Ruby and ]Python[ but I didn't find any idioms list in Ruby, so if]</c:detection> 
    <c:prefix>similarities and differences between Ruby and</c:prefix> 
    <c:exact>Python</c:exact> 
    <c:suffix>but I didn't find any idioms list in Ruby, so if</c:suffix> 
    <c:relevance>0.543</c:relevance> 
  </rdf:Description>
 
  <rdf:Description rdf:about="...">
    <!-- ProgrammingLanguage: Ruby; --> 
    <c:detection>[ list in Ruby, so if you know one or if you are a ]Ruby[ programmer, please post a]</c:detection> 
    <c:prefix>list in Ruby, so if you know one or if you are a</c:prefix> 
    <c:exact>Ruby</c:exact> 
    <c:suffix>programmer, please post a</c:suffix> 
    <c:relevance>0.386</c:relevance> 
  </rdf:Description>
</rdf:RDF>

The analyzed text is quite small but the results seems OK : 2 programming languages detected here, no animal, no gemtone...

Friday 5 December 2008

DBPedia 3.2 Including DBpedia Ontology

If you like semantics or if you work on NLP projects, you should already know DBPedia. Database plus a set of tools that allow you to ask sophisticated queries against Wikipedia. Some days ago, DBPedia 3.2 was released and now, it includes DBpedia Ontology, a manually created cross-domain ontology based on the most commonly used infoboxes within Wikipedia.

Read more on the official announcement

Note: I'm a bit late on this news, I will try to update the blog more often.

Wednesday 17 September 2008

Ruby For a Python Programmer

I'm looking for a website comparing Ruby and Python idioms. I'm a Python programmer and I always use the same idioms to write programs (list and dict comprehension, loops on slices, ...). I found a good resource that describes some similarities and differences between Ruby and Python but I didn't find any idioms list in Ruby, so if you know one or if you are a Ruby programmer, please post a comment.

Friday 29 August 2008

EVOL-ution : Darwin's graffiti

Brilliant idea, thanks to Kriebel

Friday 13 June 2008

Russian Word Stress Dictionary

I'm trying to learn russian since a few weeks. I was looking on the Internet for a Russian-English dictionary with stress on Russian words because this is the only tool I needed to learn spoken russian by myself. I found the eSpeak Project, they worked on a russian dictionary with stress associated to each word. It's great but it's annoying to look for a word in a big text file... That's why I wrote a small django+jquery frontend to query the dictionary easily. Also I don't have russian keyboard so I add a small transliteration tool to the interface.

You can access it here: http://www.biais.org/russian-stress/

EDIT: It doesn't work in IE, Get Firefox

EDIT2: It's now working in IE, anyhow Get Firefox

Note: The dictionary is not perfect but it contains about 220000 entries.

Monday 26 May 2008

Screened Emacs Launcher

I'm used to run emacs from my shell and my mind is not able to switch from the command emacs to emacs-client when I have an opened windows. This is why I wrote this simple shell script that:

  • run emacs (and force server-start) in detached screen with a particular id (emax) if this screen doesn't already exist
  • run emacs-client (with the -n option : don't wait for the server to return) else
[shell]
#!/bin/bash

screen -list |grep emax > /dev/null
if [ $? -eq 1 ]; then
	echo "screening -- emacs $@"
	screen -S emax -d -m emacs -f 'server-start' $@
else
	echo "connect to emacs server and detach -- emacs $@"
	emacsclient -n $@
fi

I prefer to get a separate emacs instance when I'm writing mail because I can focus on it. You may want to have special cases for this, use this script instead :

[shell]
#!/bin/bash

# special case for mutt mail edition
if [[ "$1" =~ "/tmp/mutt"  ]]; then
    echo "attached"
    detach=0
else
     echo "detached"
    detach=1
fi

screen -list |grep emax > /dev/null
if [ $? -eq 1 ]; then
    if [ $detach -eq 1 ]; then
	echo "screening -- emacs $@"
	screen -S emax -d -m emacs -f 'server-start' $@
    else
	echo "normal mode -- emacs $@"
	emacs -f 'mail-mode' $@
    fi
else
    if [ $detach -eq 1 ]; then
	echo "connect to emacs server and detach -- emacs $@"
	emacsclient -n $@
    else
	echo "connect to emacs server -- emacs $@"
	emacsclient $@
    fi
fi

Note: I also set a zsh alias to emacs on this script

Saturday 5 April 2008

Machine Translation Techniques and Open Source

Today there two main approaches to Machine Translation (MT)

  • Rules based MT (used by numbers of companies working in the domain: Systran, Reverso, etc.). The only open source software I know that works with this approach is Apertium.
  • Statistical based MT (used by Google and Language Weaver). Moses is an open source implementation of this approach. Also, the learning process is supported by other open source layers. (for example giza++ is an open source word aligner needed by moses to prepare the corpus).
Pros and cons of rules based machine translation
  • It needs rules, dictionaries (general and contextual) and people with the know how (linguists) to write this rules and fill dictionaries.
  • Translation costs (CPU and memory) are fairly low
Pros and cons of statistical based machine translation
  • It needs big bilingual corpus and computer ressources to run the learning process
  • The bilingual corpus have to be clean (automatic pre process and human checking)
  • Translation costs are heavy
  • You can translate in all pair languages you want if you got the corpus
Resources:

Notes: there is other less used techniques; word to word substitution (Linguaphile, example based translation (I didn't find open source implementation of this one), of course, you can imagine mixed techniques.

Monday 24 March 2008

ack: a better grep for programmers

ack is a grep like for programmers. I'm used to run grep -R and find ... -exec grep to search for something in my code or in others code. But since I found ack, I definitely switched to ack when I code. ack website.

My favourites features:

  • Color highlighting of search results
  • Searches recursively through directories by default, while ignoring .svn, CVS and other VCS directories
  • Many command-line switches are the same as in GNU grep, so the transition is nothing

ack 1.78 is out