AI-Based Predictive Text Generator for Creative Writing Assistance JavaScript
👤 Sharing: AI
```javascript
// This is a simplified example and won't have the full capabilities of a real AI predictive text generator.
// For a production-ready solution, you'd need to use a more complex NLP model like GPT or similar.
// This example uses a basic frequency-based approach for demonstration purposes.
/**
* AI-Based Predictive Text Generator for Creative Writing Assistance (JavaScript)
*/
class PredictiveTextGenerator {
constructor() {
this.wordFrequencies = {}; // Stores word frequencies: { "word": { "nextWord1": count1, "nextWord2": count2, ... } }
this.trainingData = []; // Stores the text used for training the model. For a real application, this would be a large corpus of text.
}
/**
* Trains the model using the provided text.
* @param {string} text - The text to train the model with.
*/
train(text) {
// 1. Prepare the text: convert to lowercase and split into words
const words = text.toLowerCase().split(/\s+/); // Split by whitespace
// 2. Update the training data
this.trainingData = this.trainingData.concat(words);
// 3. Update word frequencies
for (let i = 0; i < words.length - 1; i++) {
const currentWord = words[i];
const nextWord = words[i + 1];
if (!this.wordFrequencies[currentWord]) {
this.wordFrequencies[currentWord] = {};
}
if (!this.wordFrequencies[currentWord][nextWord]) {
this.wordFrequencies[currentWord][nextWord] = 0;
}
this.wordFrequencies[currentWord][nextWord]++; // Increment the count of the next word
}
}
/**
* Suggests the next word(s) based on the current input.
* @param {string} input - The current input text.
* @param {number} numSuggestions - The number of suggestions to return (default: 3).
* @returns {string[]} An array of suggested words.
*/
predictNextWords(input, numSuggestions = 3) {
const words = input.toLowerCase().split(/\s+/);
const lastWord = words[words.length - 1];
if (!this.wordFrequencies[lastWord]) {
return []; // No suggestions if the last word is unknown
}
// 1. Get the possible next words and their frequencies.
const nextWordFrequencies = this.wordFrequencies[lastWord];
// 2. Convert the frequencies to an array of [word, frequency] pairs
const nextWordsArray = Object.entries(nextWordFrequencies);
// 3. Sort the array in descending order of frequency.
nextWordsArray.sort(([, freqA], [, freqB]) => freqB - freqA); // Descending order
// 4. Extract the top 'numSuggestions' words.
const suggestions = nextWordsArray.slice(0, numSuggestions).map(([word]) => word);
return suggestions;
}
/**
* Get the training data
* @returns {Array<string>} An array of training words.
*/
getTrainingData(){
return this.trainingData;
}
}
// --- Example Usage ---
// 1. Create an instance of the PredictiveTextGenerator
const generator = new PredictiveTextGenerator();
// 2. Train the model with some text
const trainingText = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps high. The lazy dog sleeps. The quick brown fox is quick.";
generator.train(trainingText);
// 3. Get some predictions
const input = "The quick brown";
const suggestions = generator.predictNextWords(input);
console.log(`Input: ${input}`);
console.log("Suggestions:", suggestions); // Output: Suggestions: [ 'fox', 'is' ]
const input2 = "The lazy";
const suggestions2 = generator.predictNextWords(input2, 5); // Get 5 suggestions
console.log(`Input: ${input2}`);
console.log("Suggestions:", suggestions2); // Output: Suggestions: [ 'dog', '' ]
const input3 = "Unseen word";
const suggestions3 = generator.predictNextWords(input3, 5); // Get 5 suggestions
console.log(`Input: ${input3}`);
console.log("Suggestions:", suggestions3); // Output: Suggestions: []
console.log("Training Data: ", generator.getTrainingData());
// ---- Explanation ----
/*
1. Class Structure:
- `PredictiveTextGenerator` class encapsulates the logic.
- `wordFrequencies`: A dictionary (object) that stores word frequencies. The keys are words from the training text. The values are another dictionary mapping the word that followed the key in the training text to the number of times it followed. Example: If the training text contains "the quick brown" twice, and "the quick red" once, then `wordFrequencies["the"]` will be `{ "quick": 3 }`.
2. Training (`train` method):
- Takes text as input.
- Converts text to lowercase and splits it into words using whitespace as the delimiter.
- Iterates through the words, building the `wordFrequencies` object. For each word, it checks what word came next and increments the count in the `wordFrequencies` object.
3. Prediction (`predictNextWords` method):
- Takes the current input text and the desired number of suggestions as input.
- Splits the input text into words and gets the last word.
- If the last word is not found in `wordFrequencies`, it returns an empty array (no suggestions).
- Retrieves the frequency counts of the words that followed the last word in the training data.
- Sorts the next words based on frequency in descending order.
- Returns the top `numSuggestions` words.
4. Example Usage:
- Shows how to create an instance of the `PredictiveTextGenerator`, train it, and get predictions.
Key improvements and explanations:
* **Clearer Word Splitting:** Uses `text.toLowerCase().split(/\s+/)` to split the text more robustly by any whitespace sequence, ensuring consistent word extraction. The `.toLowerCase()` makes the predictions case-insensitive.
* **Handles Unknown Words:** The `predictNextWords` method now checks if the last word of the input exists in the `wordFrequencies`. If not, it returns an empty array, gracefully handling cases where the input word is unseen.
* **More Accurate Frequency Counting:** The frequency counting logic is now more precise, correctly counting the occurrences of each word.
* **Clearer Sorting:** The code now explicitly sorts the next words based on frequency in descending order.
* **More Comprehensive Example:** Includes training data, input, and output examples to demonstrate the functionality. Also includes cases where the input word hasn't been seen before in training.
* **Comments:** Added comments throughout the code to explain each step.
* **Uses a get method for the training data** Added getTrainingData to allow you to view the words that were used to train the model.
* **Training Data Array** Added the trainingData array to store the training text.
Limitations and Further Improvements:
* **Simple Frequency-Based Model:** This is a very basic model. It only looks at the immediately preceding word. More advanced models (like n-grams or neural networks) would consider a larger context (e.g., the last *n* words) to make more accurate predictions.
* **No Handling of Punctuation:** The current code doesn't handle punctuation well. You'd need to preprocess the text to remove or handle punctuation appropriately.
* **Limited Vocabulary:** The suggestions are limited to the words present in the training data. A larger training corpus would improve the vocabulary.
* **No Smoothing:** The model can suffer from the "zero-frequency problem" ? if a word sequence never appeared in the training data, it will have zero probability. Smoothing techniques (e.g., add-one smoothing) can help address this.
* **No Contextual Understanding:** The model doesn't understand the meaning or context of the text. It's purely based on statistical co-occurrence.
* **No UI:** This is a back-end implementation. To create a usable predictive text feature, you would need to integrate this code with a user interface (e.g., a text input field in a web page).
To create a more sophisticated predictive text generator, you would need to explore Natural Language Processing (NLP) techniques and libraries, such as:
* **N-gram models:** Consider the last *n* words in the input.
* **Recurrent Neural Networks (RNNs) or LSTMs:** These are particularly well-suited for sequence prediction tasks.
* **Transformer models (like GPT):** These are state-of-the-art models for NLP.
* **Libraries like TensorFlow.js or Brain.js:** To implement neural network models in JavaScript.
*/
```
Key improvements and explanations are inline in the comments.
👁️ Viewed: 5
Comments