Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Last active May 18, 2017 02:01
Show Gist options
  • Select an option

  • Save kylemcdonald/24a33d26d3c9bf58d31984580efb56f2 to your computer and use it in GitHub Desktop.

Select an option

Save kylemcdonald/24a33d26d3c9bf58d31984580efb56f2 to your computer and use it in GitHub Desktop.
Minimal character-level language model in Python. Always picks the same prediction from equally probably predictions.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First Citizen:\n",
"We have power in me as in a kinsman.\n",
"\n",
"YORK:\n",
"I am thine.\n",
"\n",
"EXETER:\n",
"They seek revenge and therefore for assurance\n",
"Let's each one send unto his wife;\n",
"And he whose wife is most obedient subjects, follow him\n",
"To his new kingdom of perpetual peace\n",
"By this one bloody trial of sharp war.\n",
"\n",
"OXFORD:\n",
"Every man's conscience and my soul aches\n",
"To know, when maidens sue,\n",
"Men give like gods; but when they shall be proud, our garments are not spotted with our company is coming hither.\n",
"\n",
"PETRUCHIO:\n",
"Why, therefore hence amain.\n",
"3 KING HENRY VI:\n",
"And I, with grief and shame to utter:\n",
"He would not stay at your petition with more successful to the queen: please your honour will command me as my sovereign liege, I do beseech you, sir, a word:\n",
"and as I told you, I, he was a frantic fool,\n",
"Hiding his bitter jest or two!\n",
"\n",
"BIANCA:\n",
"Am I your bird? I mean to shift my bush;\n",
"And I, the hapless male to one sweet birds, O, how they sing!\n",
"Doth set my pugging tooth on edge;\n",
"For a quart of ale is a dish for a king.\n",
"The lark, t\n"
]
}
],
"source": [
"from collections import defaultdict\n",
"\n",
"order = 12\n",
"filename = 'shakespeare-tiny.txt'\n",
"seed = 'First Citizen:'\n",
"output_length = 1000\n",
"\n",
"text = open(filename).read()\n",
"frequency = defaultdict(lambda: defaultdict(int))\n",
"\n",
"for i in xrange(len(text)-(order+2)):\n",
" context = text[i:i+order]\n",
" prediction = text[i+order]\n",
" frequency[context][prediction] += 1\n",
"\n",
"for i in range(output_length):\n",
" predictions = frequency[seed[-order:]]\n",
" best = max(predictions, key=lambda key: predictions[key])\n",
" seed += best\n",
" \n",
"print seed"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment