01/02/2020

Introduction

Part 1

How the predictor works

This R script appears to be implementing a predictive text model using n-grams (bigrams, trigrams, and fourgrams).

Loading Data:

This part loads precomputed n-gram data (bigrams, trigrams, and fourgrams) from RDS files. These files likely contain information about the relationships between words in a corpus.

Defining the nextWordPredictor Function:

This function takes an input text (inputTxt) and predicts the next word(s) based on the context provided by the input. It utilizes n-grams for prediction.

Part 2

Data Cleaning:

The input text is preprocessed to ensure consistency. It is converted to lowercase, numbers are removed, punctuation is removed, and extra whitespace is stripped.

Splitting Input into Words:

The cleaned input text is split into a list of words based on spaces.

Defining Functions for N-gram Predictions:

These functions take a sequence of words and return the next word(s) based on the precomputed n-gram data.

PART 3

Conditional Branching for Prediction:

The script checks the number of words in the input and chooses the appropriate n-gram function for prediction. If no prediction is found, it falls back to lower-order n-grams.

Returning Top Predictions:

The function returns the top n predictions as a character vector. If the input text is empty, the function returns an empty string.

Overall, this script defines a function that uses n-grams to predict the next word(s) in a sequence based on input text. The predictions are then returned as a character vector.