Code for building text matrix