"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Friday 30 January 2009

Names generators with MySQL

There are on the net several names generator. theese programs staring from an input name generate some kind of funny phrase.
Here is an example of a "nickname generator":
The algorithm behind these programs is quite simple: it's just matter of using your name as a key to select phrase components from one or more tables. Here is a simple way to do this using MySQL:
First let's create a couple of tables:
CREATE TABLE ADJECTIVES(
              TERM VARCHAR(30));

CREATE TABLE ANIMALS(
              NAME VARCHAR(30));
Then fill the tables with some value
INSERT INTO ADJECTIVES VALUES('Lazy');
INSERT INTO ADJECTIVES VALUES('Fat');
INSERT INTO ADJECTIVES VALUES('Crazy');
INSERT INTO ADJECTIVES VALUES('Ugly');

INSERT INTO ANIMALS VALUES('Bull');
INSERT INTO ANIMALS VALUES('Cow');
INSERT INTO ANIMALS VALUES('Monkey');
INSERT INTO ANIMALS VALUES('Chicken');
To implement the selection we can use MySQL RAND()  function. Rand function can generate a pseudo-random number sequence based on a seed integer passed as parameter. The same seed number ever generates the same sequence.
Another function HEX() let us easily convert our input string to a number we can use as seed.
The number generated from RAND() function can eventually be used to select a row in the table.
Let's put all together:
SELECT
(SELECT TERM FROM ADJECTIVES ORDER BY RAND(HEX('musante.i.ph')) LIMIT 1) AS Adjective,
(SELECT NAME FROM ANIMALS ORDER BY RAND(HEX(REVERSE('musante.i.ph'))) LIMIT 1) AS Animal;
(Update) since RAND() ever generates the same number sequence from the same seed number we must use a different seed for every word we need to extract. To do this we neeed to change the source string in order to obtain a different seed number. In my example I used the REVERSE() function to implement it (not the best choice indeed) but even simpler solutions, like adding a different constant to each, should work also well.
Here is the result:

Adjective Animal 
Crazy     Chicken

Does it fit with my blog? Probably no more than "Ultimate Monk"!
What makes the difference, in this kind of programs, is the database of names used. A wider database generates, of course , a less repetitive set of phrases but what really matters is a good choice of the words database.

Thursday 15 January 2009

Experimenting with Linux: Jinzora

Because of my job I've been too busy to post anything for a while (even at my very slow posting rate) but now here I am again with another experiment on my little Linux server.
 I was interested in experimenting with video and audio streaming, after a little of Google searching I found  this how to about Jinzora installation. Jinzora is a web based media streaming and management system completely written in PHP. It gives you a graphic user interface to search, organize and view your media files simply from your web browser. What triggered me to choose Jinzora was mainly the limited requirements It has. It helped also that i had the computer almost ready with Apache2 and MySQL already installed.
 I started my installation directly from how to's part 2 by installing some missing  component:
sudo apt-get install libapache2-mod-auth-mysql
sudo apt-get install php5-gd
and then I edited my php configuration to fit Jinzora requirements by typing:
sudo pico /etc/php5/apache2/php.ini
 and adjusting some parameters
 max_execution_time = 300
memory_limit = 128M
post_max_size = 32M
file_uploads = On
upload_max_filesize = 32M 
I then downloaded Jinzora and installed it in Apache
wget http://downloads.sourceforge.net/jinzora/jz275.tar.gz
sudo tar xvzf jz275.tar.gz
sudo mv jinzora2 /var/www/jinzora2
sudo chmod -R 777 /var/www/jinzora2
 And after restarting Apache server as usual ...
sudo /etc/init.d/apache2 restart
I've been able to access to Jinzora using my browser at the server address:
http://192.168.30.100/jinzora2/
Jinzora asks you to configure some initial configuration parameters like MySQL user and password in order to complete installation. Everthing is done with the help of a simple and well explained web based user interface. After initial configuration Jinzora asks you to delete the installation directory before proceeding with normal use. This was easily done ...
sudo rm -r /var/www/jinzora2/install
After this I finally managed to enter in Jinzora.
As my first impression Jinzora works fine: video streaming is smooth, and this is what really matters, and the user interface is clean and effective (you can choose many GUI layout and skins). I installed VLC media player on my Windows client (Sempron 2400) in order to play streaming while on my Linux laptop (EEEPC with Xandros) everything worked fine using Real player (RAM) streaming mode.
 If I'm going to continue with this media server and transform it to a fully operative one I'll need some better hardware platform with a bigger hard-disk and a quieter fan. But, at the moment, it's still just another Linux experiment.