{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import string\n",
    "import re\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from random import choices\n",
    "import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Import the text files\n",
    "# In terms of memory it would be better only to deal with one at a time, but this is simple\n",
    "with open(\"shakespeare.txt\", \"r\") as f:\n",
    "    shakespeare = f.read()\n",
    "with open(\"whitman.txt\", \"r\") as f:\n",
    "    whitman = f.read()\n",
    "with open(\"montaigne.txt\", \"r\") as f:\n",
    "    montaigne = f.read()\n",
    "with open(\"twain.txt\", \"r\") as f:\n",
    "    twain = f.read()#\n",
    "\n",
    "text = ' '.join([shakespeare, whitman, montaigne, twain])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parsed=\" \".join(re.findall(\"[a-zA-Z]+\", text)) #Extract only words\n",
    "parsed=parsed.upper().translate(str.maketrans(\"\", \"\", string.punctuation)) #drop punctuation and capitalize\n",
    "parsed=parsed.translate(str.maketrans(\" \", \"_\")) #replace spaces with underscores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One-step ahead model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "step=5 #You can increase in theory, but you need to store a (27^step)*(27)  matrix of integers... To do this better, we need to code our data more efficiently\n",
    "alphabet = string.ascii_uppercase+ \"_\"\n",
    "tokens = alphabet\n",
    "for k in range(step-1):\n",
    "    tokens = [x+y for x in tokens for y in alphabet]\n",
    "tokens_dict = {tokens[x]:x for x in range(len(tokens))}\n",
    "alphabet_dict = {alphabet[x]:x for x in range(len(alphabet))}\n",
    "trans_mat = np.zeros((len(tokens),len(alphabet)), dtype=np.int32) #Only has the next character in the forward state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trans_mat.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in tqdm.tqdm(range(len(parsed)-step)):\n",
    "    initial = parsed[i:(i+step)]\n",
    "    terminal = parsed[i+step]\n",
    "    trans_mat[tokens_dict[initial], alphabet_dict[terminal]] = trans_mat[tokens_dict[initial], alphabet_dict[terminal]] +1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=parsed[:step]\n",
    "out_string = x\n",
    "for t in range(200):\n",
    "    x=choices(alphabet, trans_mat[tokens_dict[out_string[-step:]],:])[0]\n",
    "    out_string= out_string+x\n",
    "print(out_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
