generalized FunctionalNGram to handle more than bigrams