OpenFst.net Update

I’m making good progress in creating a set of .net and Python wrappers for OpenFst.

Following on from my last post I have managed to create a single .net wrapper using the mergebin utility. It took a bit of hacking of the mergebin code and experimentation but I’m glad I’ve it working.

Now I want to take the integration one step further. For example what if the dll consumer wants to then merge it with their exe’s or additional libraries both managed or unmanaged.

Here is a quick C# snippet that will load an fst and iterate the states in outer loop and the arcs in the inner to print out the fst console. I’ve tried to use the the iterator support and foreach statement to make a clean looking syntax.

VectorFst fst = VectorFst.Read(@"t.ofst");

      if (fst != null)

      {

        foreach (var s in fst.States)

        {

          foreach (var a in fst.Arcs(s))

          {

            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", s, a.nextstate, a.ilabel, a.olabel, a.weight);

          }

        }

     }

DSLs seem to be the current hotness and I’m seeing T4 mentioned a lot. I haven’t looked at T4 properly yet, but I’m thinking about if a text templating system might be good approach for generating parts of wrapper, such as methods common to all FST types or for generating semiring specialized methods.

OpenFst for .net/C#

Recently I have been writing .net wrapper around OpenFst. I want to be able to OpenFst in .net and managed languages and I often prototype in C# because not only is the less typing but the compile are way faster. The heavy templated in OpenFst can sometime lead to long compilations time.

My approach is create C wrapper function then PInvoke the wrapper in C# to expose to. This have the advantage of managed c++ in that I can use the wrapper with other languages and platforms for example Python and Unix. One disadvantage is the OpenFst.net library will be distributed as two dlls, the native library and the managed wrapper. I’ve found two solutions to enable the library to distributed as a single file. 1) embed the un-managed dll as resource and then at run-time pull out the dll to a temp directory and load the library manually as describe here here. 2) Use mergebin a tool that is included in sqlite.

XBox 360 HDMI –> DVI

Recently we got an XBox 360, mostly as media player for my wife. We had a spare 20” widescreen panel and got a HDMI to DVI cable. Reading the Microsoft site it wasn’t clear if the HDMI->DVI wasn restricted to the HTML resolutions such as720p, 1080i etc and the display would have to scale a lower resolution signal.

The good news is the cable can support the native 1680x1050 (1680 1050) resolution of the panel. The below image shows all the resolutions that are offered when connect via HDMI->DVI.

IMG_0690

I knew the monitor wouldn’t support Hi-def playback because of it’s lack off HDCP, this wasn’t a problem because HD-DVD is dead and there isn’t any netflix in Japan. However, no the lame part came when I tried to play the DVD I got “HDCP secure link” error. Now apparently the XBox 360 needs a HDCP compliant display to play up scaled DVDs.

The lameness continues the HDMI socket is right below the standard AV connector and can't both inserted at the same time, I presume so you have to buy XBox HDMI cables, my solution was to just pop the bottom half of the case of the standard xbox connector.

Generalized Callbacks in C++ using TR1

Recently, I had situation where a class (say called B) aggregated a class (say A). Class A would fire messages during a computation and I wanted to be able to subscribe to these messages in class B by passing a pointer to member function . The syntax of pointer to member functions is a little strange and seen often. I also wanted arbitrary global functions or other classes to be valid subscribers.

TR1 provides two useful classes to achieve this std::tr1::function which allows us to create to create a call wrapper . And std::tr1:bind which allows for us to bind to an object.

The below code implements the scenario described in the first paragraph.

#include <iostream>

#include <functional>

#include <vector>

 

using namespace std;

using namespace std::tr1;

 

void foo(int i)

{

  cout << "foo " << i << endl;

}

 

 

class A

public:

  typedef function< void(int) > Delegate;

  void addSubscriber(Delegate func)

  {

    _subscribers.push_back(func);

  }

 

  void action()

  {

    for(vector<Delegate>::iterator it = _subscribers.begin(); it!=_subscribers.end(); ++it)

    {

      (*it)(10);

    }

  }

 

private:

  vector< Delegate > _subscribers;

};

 

 

class B

{

  A  a;

  void callback(int i)

  {

    cout << "B::callback() " << i << endl;

  }

 

public:

  B(int i)

  {

  }

 

  void connect()

  {     

    A::Delegate memfunc = std::tr1::bind(&B::callback,*this,std::tr1::placeholders::_1);

    a.addSubscriber(memfunc);

 

    A::Delegate globalfunc = &foo;

    a.addSubscriber(globalfunc);   

  }

 

  void action()

  {

    a.action();

  }

 

private:

  B()

  {

  }

 

};

 

 

int main()

{

  B b(10);

  b.connect();

  b.action();

  return 0;

}

 

Tested on Window using Visual Studio 2008, GCC should no be a problem (might just need the includes changing).

Windows 7 Sendto and some different shortcuts

I wanted to add notepad2 to the sendto menu so I can quickly view source files and files with unix style newlines.

Access is denied to c:\users\username\sendto

Fortunately this blog post explains how to customize and the shift+sendto feature (which I don’t think I will use). After opening shell:sendto I removed all the defaults, but the sento menu still shows the drives by default.

Two shortcut annoying changes that have come from Vista, are F6 to select the address bar has changed to ALT+D and backspace to move up a directory has changed to ALT+UPARROW.

Some C# snippets I Use a Lot

Quick and dirty code with no error checking

Often I want to count occurrences or have a set like function to list unique labels, for this task I use a simple wrapper around the dictionary.

  class Bag<T> : Dictionary<T, int>

  {

    public void Add(T t)

    {

      if (!this.ContainsKey(t))

      {

        this.Add(t, 1);

      }

      this[t]++;

    }

  }

Splitting on whitespaces whilst removing empty fields is another frequent task,  I include this extension method in my projects.

public static class StringEx

  {

    public static String[] SplitOnWhiteSpace(this String s)

    {

      return s.Split(new String[] { "\t", " " },  StringSplitOptions.RemoveEmptyEntries);

    }

  }

I formatted the code using the CopySourceAsHtml Pluing