update n-gram algorithm