refactor viterbi to support crf inference