Skip to content

Instantly share code, notes, and snippets.

@sslotin
Last active April 3, 2018 21:54
Show Gist options
  • Save sslotin/2f7756dd8800f3aec76e0473826253c3 to your computer and use it in GitHub Desktop.
Save sslotin/2f7756dd8800f3aec76e0473826253c3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from random import randint, shuffle\n",
"from simanneal import Annealer\n",
"import kenlm\n",
"model = kenlm.LanguageModel('wiki.arpa')"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" Temperature Energy Accept Improve Elapsed Remaining\n",
" 2.50000 102.38 74.20% 24.40% 0:00:02 0:00:00"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"presidential elections\n",
"critical problem\n",
"sufficient time\n",
"beautiful woman\n",
"scary movie\n",
"sexual harassment\n",
"tasty hamburger\n",
"trivial task\n"
]
}
],
"source": [
"first = 'presidential critical sufficient beautiful scary sexual tasty trivial'.split()\n",
"second = 'elections problem time woman movie harassment hamburger task'.split()\n",
"shuffle(b)\n",
"\n",
"class Matching(Annealer):\n",
" def move(self):\n",
" a = randint(0, len(self.state) - 1)\n",
" b = randint(0, len(self.state) - 1)\n",
" self.state[a], self.state[b] = self.state[b], self.state[a]\n",
" \n",
" def energy(self):\n",
" return -sum(model.score(first[i] + ' ' + w)\n",
" for i, w in enumerate(self.state))\n",
"\n",
"matching = Matching(second)\n",
"result, score = matching.anneal()\n",
"\n",
"for i, w in enumerate(result):\n",
" print(first[i], w)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" Temperature Energy Accept Improve Elapsed Remaining\n",
" 2.50000 46.70 63.00% 14.20% 0:00:02 0:00:00"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mastering horse riding takes a lot of time and also may be very dangerous.\n"
]
}
],
"source": [
"#text = 'Fuck this _ test! Why don\\'t we go drink some _ and watch some _? This sound like a _ to me.'\n",
"#entries = 'math beer movies plan'.split()\n",
"text = 'Mastering horse _ takes a lot of _ and also may be very _.'\n",
"entries = 'riding time dangerous'.split()\n",
"shuffle(entries)\n",
"\n",
"def fill(s):\n",
" _text = text\n",
" for w in s:\n",
" _text = _text.replace('_', w, 1)\n",
" return _text\n",
"\n",
"class Placing(Annealer):\n",
" def move(self):\n",
" x = randint(0, len(self.state) - 1)\n",
" y = randint(0, len(self.state) - 1)\n",
" self.state[x], self.state[y] = self.state[y], self.state[x]\n",
" \n",
" def energy(self):\n",
" return -model.score(fill(self.state))\n",
"\n",
"placing = Placing(entries)\n",
"result, score = placing.anneal()\n",
"\n",
"print(fill(result))"
]
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment