File size: 853 Bytes
e088236
efde5e9
 
e088236
 
 
 
 
112363a
 
e088236
 
112363a
 
 
 
e088236
112363a
e088236
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Import libraries
import os
os.system('pip install nltk numpy')
import nltk
import numpy as np
import pickle
nltk.download('punkt')
# Define predict_word function
from collections import Counter

def predict_word(model, last_word):
    if last_word in model:
        next_words = model[last_word]
        freq_dist = Counter(next_words)
        most_common_word = freq_dist.most_common(1)[0][0]
        return most_common_word
    else:
        return ""
# Load the model
with open("model.pkl", "rb") as f:
    model = pickle.load(f)

# Run the prediction for 10 words
input_words = input('Input words: ')
for i in range(10):
    input_words_list = nltk.word_tokenize(input_words)
    last_word = input_words_list[-1]
    predicted_word = predict_word(model, last_word)
    input_words = f"{input_words}" + " " + f'{predicted_word}'

print(input_words)