Saturday, March 29, 2014

Greek stemmer in C#

I recently needed Greek stemmer in C# for a personal project. To my amazement, I could not find anything on the net (that, or my google skills are getting rusty). As I rarely give anything back to the community (shame alarm), I decided to share my findings with the world.

Disclaimer: actually, I've merely written any code, I tried to "borrow" bits from here and there. First, I came across a wonderfully simple description of a greek stemmer in Georgios Ntais dissertation. Looking for actual code, I came across a Java implementation included in Lucene 4.7 written in Java. Although a .NET port exists, its current version (3.0.3) did not include the Greek stemmer. As the time was around 2am and I was feeling reluctant to write any code whatsoever, I downloaded the free edition of Java to C# converter (shame alarm x2). The free edition has a 1000 lines limitation, and GreekStemmer.java from Lucene was 767 lines: hurrahhh!

I was now really close to getting what I wanted. The only thing missing was that GreekStemmer required that all letters were lower case, with diacritics removed, and small final sigma (end of word) replaced with normal small sigma. So I copied the logic found in GreekLowerCaseFilter.java (no brainer here) and typed a few lines by myself (great success!). As a last small bonus, I also "borrowed" the stop words from the Lucene distribution, I'm pretty sure they will come handy when analyzing text.

I've included all these 3 files for GreekStemmer.cs in a zip file for your download pleasure. See you in 4 years with another blog post...


Enjoy!
Themos

Tuesday, November 16, 2010

iOS Push Notifications in the C# world

Thanks to the great work down at the Novell labs, we (the poor C# programmers) can now at last put our hands on iPhone development using Monotouch. One of the most compelling features is the Apple Push Notification framework, where you can instantly display an alert message on the iPhone, put a number on that small red circle over your app icon, or even make a sound.

You currently have 2 options if you want notifications in your iPhone: a) implement your own C# client (consult the excellent apns-sharp project at http://code.google.com/p/apns-sharp/) which should consist of two applications, preferrably windows services, one for sending the notifications using a TCP client connected at Apple Push Notification servers and one for receiving feedback whenever a user uninstalls your application so you stop sending any more. b) save a lot of trouble and use the industry-grade urban airship service (http://www.urbanairship.com) which handles both tasks for you, and comes with one million free notifications per month (that's 1.000.000, yes, I know...)

If you get a free Urban Airship account, you can trigger a Push Notification with a simple API call. The following code should show you how. It assumes you have 3 entries in your application settings config file, one for notificationsKey, one for notificationsSecret and one for notificationsMasterSecret, you can find all these values in your application's settings page. Depending on the API call, sometimes you need the secret (e.g. device registration) and sometimes you need the master secret (e.g. push call).

private const string URBAN_SHIP_URL = "https://go.urbanairship.com/api/";


private void SendRequest(string command,string json,bool useMasterKey)
        {
            WebClient c = new WebClient();
            c.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["notificationsKey"], (useMasterKey ? ConfigurationSettings.AppSettings["notificationsMasterSecret"] : ConfigurationSettings.AppSettings["notificationsSecret"]));
            c.Headers.Add("Content-type", "application/json");
            byte[] data = Encoding.UTF8.GetBytes(json);
            byte[] ret = c.UploadData(URBAN_SHIP_URL + command + "?format=xml",data);
        }


public void Push(string deviceToken, string message)
        {
            string json = string.Format("{{\"device_tokens\": [\"{0}\"],\"aps\": {{\"alert\": \"{1}\"}}}}",deviceToken,message);
            SendRequest("push/",json,true);
        }




 public void Push(string deviceToken, int badge)
        {
            string json = string.Format("{{\"device_tokens\": [\"{0}\"],\"aps\": {{\"badge\": {1}}}}}", deviceToken, badge.ToString());
            SendRequest("push/", json, true);
        }


There is one method for sending alerts and one method for changing the badge, I have not included a method for making a sound on the iPhone but you should get the idea. All you have to do is call Push("xxxxxxxxxxxxxxxxxx","Hello World!"), and if you have correctly setup and installed the application on the device, it should display an alert even if the application is not running! Cool huh?

It is a bit tricky to setup eveything correctly on the Monotouch side, so I suggest you take a look at http://code.google.com/p/apns-sharp/downloads/detail?name=MonoTouch-ClientPushNotificationSample-v2.zip, all the code you need is already written for you.