<jupyter_start><jupyter_text>***Purpose of this notebook.******Understanding Text Generation without Neural Network*** ***Simple Text Generation on the logical applocation of Markov's Chain .***[Markov model](https://en.wikipedia.org/wiki/Markov_model:~:targetText=In%20probability%20theory%2C%20a%20Markov,it%20assumes%20the%20Markov%20property). Understanding use of defaultdict function from collections .[Default Dict](https://docs.python.org/3.3/library/collections.html)<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_pickle("/content/combined_genre.pkl")
df.head()
from collections import defaultdict
def markov_dict(data):

  word = data.split()

  my_dict  = defaultdict(list)

  for current_word, next_word in zip(word[0:-1],word[1:]):
    my_dict[current_word].append(next_word)
  my_dict = dict(my_dict)
  return my_dict
my_vocab = markov_dict(df['Complete Story'][4])
my_vocab
len(my_vocab)
type(my_vocab)
def markov_chains(vocab,max_length=30):
  # vocab is any dictionary which contains the keyword as the current word and values of those
  # words which wwe can get from that current word 
  # similar to markov model 
  # Check this link for more info on markov model : 
  # https://en.wikipedia.org/wiki/Markov_model#:~:targetText=In%20probability%20theory%2C%20a%20Markov,it%20assumes%20the%20Markov%20property).
  import random

  first_word = random.choice(list(vocab.keys()))
  sentence = first_word.capitalize()

  for i in range(max_length):
      
    second_word = random.choice(vocab[first_word])
    first_word = second_word

    sentence+= " "+ first_word

  sentence+="."
  return sentence


action_generation = markov_chains(my_vocab,50)
print(action_generation)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11328125, 'fraction_numerical': 0.024553571428571428, 'mean_word_length': 5.869731800766283, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '538617', 'license': 'MIT', 'n_tokens_mistral': 639, 'n_tokens_neox': 592, 'n_words': 135, 'path': 'Text_Generation_using_Markov_Chains.ipynb', 'repo_name': 'AjayJohnAlex/NaturalLanguageProcessing', 'size': '1792'}