# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Trigram_LM

from nltk import trigrams
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import collections
from collections import Counter
from nltk.corpus.reader.plaintext import PlaintextCorpusReader


your_corpus = PlaintextCorpusReader('C:\\Users\\Dell\\NLP\\2_Python_Pandas\\100_Applied_NLP\\taleof2cities.txt', '.*' )


def build_trigram_model():
    trigram_model = collections.defaultdict(lambda: collections.defaultdict(lambda: 0))
    #for sentence in gutenberg.sents("austen-emma.txt"):

    for sentence in your_corpus.sents('.')[:1200]:
        sentence = [word.lower() for word in sentence if word.isalpha()]  # get alpha only

        for w1, w2, w3 in trigrams(sentence, pad_right=True, pad_left=True):
            trigram_model[(w1, w2)][w3] += 1

        for w1_w2 in trigram_model:
            trigram_count_4_w1w2 = float(sum(trigram_model[w1_w2].values()))
            for w3 in trigram_model[w1_w2]:
                trigram_model[w1_w2][w3] /= trigram_count_4_w1w2

    return trigram_model



def predict_next_word(w1,w2):
    model = build_trigram_model()
    next_word = model[(w1,w2)]
    predicted_words = Counter(next_word).most_common(10)

    top10Predicted_words = list(zip(*predicted_words))[0]
    probability_score = list(zip(*predicted_words))[1]
    x_pos = np.arange(len(top10Predicted_words))

    # calculate slope and intercept for the linear trend line
    slope, intercept = np.polyfit(x_pos, probability_score, 1)

    plt.bar(x_pos, probability_score,align='center')
    plt.xticks(x_pos, top10Predicted_words)
    plt.title('Predicted words for  '+ w1 + ' ' + w2)
    plt.ylabel('Probability Score')
    plt.xlabel('Predicted Words')
#     plt.show()
    


predict_next_word('can', 'be')



StarcoderdataJupyterScriptsDedupFiltered