Algorithm for word prediction