Thursday 1 November 2007

Yoruba & Scala

Okay! I'm really bored... but then that's what this blog is about... and by the way i'll be posting a lot about scala (my new night time hobby) from now on :)

I've been playing around some with SCALA recently (for more info on scala see www.scala-lang.org). So I figured let's say some one has a free dictionary and he wants to make the search feature that's real neat. So he offers alternative spellings of the search word that the user enters. Well functional programming and SCALA in particular makes this really easy to do. Just take a look at the code below.

object YorubaOptions {
def alternative(info:String):List[String]={
if(info==null || info.length==0) List("")
else if (info.length==1)
info match {
case "e"=>List("e", "\\u232", "\\u233")
case "o"=>List("o", "\\u242", "\\u243")
case "i"=>List("i", "\\u236", "\\u237")
case "u"=>List("u", "\\u249", "\\u250")
case "a"=>List("a", "\\u224", "\\u224")
case "s"=>List("s", "\\u7779")
case _=>List(info)
}
else
combine(alternative(info.substring(0, info.length-1)), alternative(info.substring(info.length-1)))
}

def combine(lhs:List[String], rhs:List[String]):List[String]=for(word1<-lhs;word2<-rhs)yield word1+word2

def main(args : Array[String]) : Unit = {
YorubaOptions.alternative("akin").foreach(println)
}
}



It's a divide and conquer algorithm. Really straight forward... Divide the search word until it's lenght is '1'. Then either produce a list of alternatives.

Now it's true that this code only makes sense when run in a unicode environment but porting it to javascript and setting the html encoding will take care of this. On the other hand, code like this can be in the back end (PHP, JEE, ASP.NET, whatever your fancy) so that search results can be generated based on the user's search key word.

So what's next...

I've written a small script to change the keyboard so that you can practically change type yoruba characters with the normal english keyboard in an html control. I'll probably look into creating a linux xkeyboard symbols file so that I can type yoruba straight up from my OS too. Then when I'm really bored I'll look at a windows solution (let's not get me started on why poor naija students, like I was, should jump on the linux band wargon if nothing to save cost and learn practically).

Phew... I did all this typing and I've still got about 24 minutes before SUPERNATURAL starts... now what I'm going to do for the next 20 minutes :(