lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mar 20, 2013, at 11:49 AM, Chris Datfung <chris.datfung@gmail.com> wrote:

> Thank you, this is exactly what I'm looking for. Can I trouble you to explain how the function works as well?

The n-grams can be viewed as numbers in a base-M number system, where M is the length of the alphabet. 

The size of the n-gram is N, so the number of values is M ^ N. 

We can map these values to integers i from 0 to  M ^ N. The first n-gram-digit is i % M. The next digit is ((i / M) % M) where division is floor. The code uses an accumulator for the repeated divisions. This is the same way we'd compute the digits of the base M representation of a number.

The codes uses string sub to access the n-gram-digits. It terminates after it produces the n-gram corresponding to M ^ N - 1, the n-gram equivalent of 999…9 in base 10.

e