Skip to content

Instantly share code, notes, and snippets.

@brockmanmatt
Last active July 7, 2020 14:24
Show Gist options
  • Save brockmanmatt/e7e9bb3baa701bd9c36583e19378cba0 to your computer and use it in GitHub Desktop.
Save brockmanmatt/e7e9bb3baa701bd9c36583e19378cba0 to your computer and use it in GitHub Desktop.
Rhyming.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"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.7.4"
},
"colab": {
"name": "Rhyming.ipynb",
"provenance": [],
"collapsed_sections": [
"0sIXXvMKqI2l",
"9UanLMk0qI2s",
"IL6YtwUSqI2z",
"9PaeJjbKqI24",
"zCHDVorOqI2_",
"LVVCXv77qI3Y",
"IRxz1kT5qI3h",
"o69GgdwFqI3m",
"H7HOcsQ8qI33",
"YF5wqVrEqI36"
],
"include_colab_link": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/brockmanmatt/19f1b7e48a699278f11c7b5c6e862ecf/rhyming.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "H9o5H4nxqI10",
"colab_type": "code",
"colab": {}
},
"source": [
"import openai, json, pandas as pd"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZoJiFIP_qI14",
"colab_type": "text"
},
"source": [
"# Wrapper to hold my arguments and stuff"
]
},
{
"cell_type": "code",
"metadata": {
"id": "fG3OSX-qqI15",
"colab_type": "code",
"colab": {}
},
"source": [
"class poemWrapper:\n",
" \n",
" def __init__(self, *args, key = \"\", oldWrapper:poemWrapper=None,**kwargs):\n",
" \"\"\"\n",
" Not sure what I want this to do yet\n",
" can pass a poemWrapper and take its key and kwargs so that way don't need to re-run to build\n",
" \"\"\"\n",
" if oldWrapper:\n",
" self.kwargs = oldWrapper.kwargs\n",
" return\n",
"\n",
" openai.api_key = key\n",
" self.set_default_kwargs()\n",
" \n",
" \n",
" def set_default_kwargs(self):\n",
" self.kwargs = {\n",
" \"engine\":\"davinci\",\n",
" \"temperature\":0,\n",
" \"max_tokens\":10,\n",
" \"stop\":\"\\n\",\n",
" }\n",
"\n",
" def set_new_kwargs(self, kwargs):\n",
" \"\"\"\n",
" set kwargs to kwargs\n",
" \"\"\"\n",
" self.kwargs = kwargs\n",
"\n",
" def set_single_kwarg(self, key, value):\n",
" \"\"\"\n",
" set single key word argument\n",
" \"\"\"\n",
" self.kwargs[key] = value\n",
"\n",
" def query(self, prompt):\n",
" \"\"\"\n",
" query API with prompt and current kwargs\n",
" \"\"\"\n",
" r = openai.Completion.create(prompt=prompt, **self.kwargs)\n",
" self.last_request = prompt\n",
" self.last_response = r\n",
" return r\n",
" "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "SAYBRTouqI18",
"colab_type": "code",
"colab": {}
},
"source": [
"test = poemWrapper(key=json.load(open(\"../gpt3/key.json\", \"r\"))[\"key\"])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "jGSRBclpqI1-",
"colab_type": "code",
"colab": {},
"outputId": "fdbce437-087c-490b-d5b5-45f7ff858ab6"
},
"source": [
"test.query(\"q: what is the capital of france\\na:\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<OpenAIObject text_completion id=cmpl-4lIsTF8SF5fYdWbjtHeKqDBY at 0x1141c5350> JSON: {\n",
" \"choices\": [\n",
" {\n",
" \"finish_reason\": \"stop\",\n",
" \"index\": 0,\n",
" \"logprobs\": null,\n",
" \"text\": \" paris\"\n",
" }\n",
" ],\n",
" \"created\": 1594090653,\n",
" \"id\": \"cmpl-4lIsTF8SF5fYdWbjtHeKqDBY\",\n",
" \"model\": \"davinci:2020-05-03\",\n",
" \"object\": \"text_completion\"\n",
"}"
]
},
"metadata": {
"tags": []
},
"execution_count": 29
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "8WlsPDetqI2C",
"colab_type": "code",
"colab": {}
},
"source": [
"test.set_single_kwarg(\"test\", \"test\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "4nn-lTSnqI2E",
"colab_type": "code",
"colab": {},
"outputId": "f5eef61b-641e-471c-bbe4-eda68d9ee807"
},
"source": [
"test.kwargs"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'engine': 'davinci',\n",
" 'temperature': 0,\n",
" 'max_tokens': 10,\n",
" 'stop': '\\n',\n",
" 'test': 'test'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 35
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Bje6GlATqI2H",
"colab_type": "code",
"colab": {}
},
"source": [
"test2=poemWrapper(oldWrapper=test)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "j-bIVD0uqI2K",
"colab_type": "code",
"colab": {},
"outputId": "55a89f6d-bbdd-4f99-bc91-57845020d6a2"
},
"source": [
"test2.kwargs"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'engine': 'davinci',\n",
" 'temperature': 0,\n",
" 'max_tokens': 10,\n",
" 'stop': '\\n',\n",
" 'test': 'test'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 37
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BiqC_6wDqI2M",
"colab_type": "text"
},
"source": [
"# Inherit myself, see if I can rhyme single words"
]
},
{
"cell_type": "code",
"metadata": {
"id": "LvCbXyXCqI2N",
"colab_type": "code",
"colab": {}
},
"source": [
"class poemWrapper(poemWrapper):\n",
" \n",
" def getRhymingWord(self, word):\n",
" \"\"\"\n",
" gets a word from the API that rhyms with word\n",
" \"\"\"\n",
"\n",
" prompt=\"A word rhymes with a word if their endings sound the same. Words do not rhyme with themselves\\n\"\n",
" prompt+=\"q: what is a word that rhymes with 'dog'\\n\"\n",
" prompt+=\"a: smog\\n\\n\"\n",
" prompt+=\"q: what is a word that rhymes with 'ham'\\n\"\n",
" prompt+=\"a: spam\\n\\n\"\n",
" prompt+=\"q: what is a word that rhymes with '{}'\\na:\".format(word) \n",
" return self.query(prompt.strip())[\"choices\"][0][\"text\"].strip()\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Fw0tBkXgqI2P",
"colab_type": "code",
"colab": {}
},
"source": [
"test = poemWrapper(key=json.load(open(\"../gpt3/key.json\", \"r\"))[\"key\"])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9zhgEvCOqI2R",
"colab_type": "code",
"colab": {},
"outputId": "8488d971-908a-4530-ec5b-ce5f39bc8004"
},
"source": [
"test.getRhymingWord(\"small\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'tall'"
]
},
"metadata": {
"tags": []
},
"execution_count": 61
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YuVV9GbBqI2U",
"colab_type": "text"
},
"source": [
"# Inherit myself, see if I can replace words in a sentence - THIS IS HARD"
]
},
{
"cell_type": "code",
"metadata": {
"id": "zcIgPE2CqI2V",
"colab_type": "code",
"colab": {}
},
"source": [
"class poemWrapper(poemWrapper):\n",
" \n",
" def rephraseToEndWithWord(self, sentence, word):\n",
" \"\"\"\n",
" rephrase a sentence to end with a word\n",
" \"\"\"\n",
" prompt=\"I want to rephrase each sentence to end with the new ending.\\n\"\n",
" prompt+=\"Each sentence should be re-worded to generally have the same meaning but end with the new ending\\n\"\n",
" \n",
" prompt+=\"Sentence: I was at the store when I got the call\\n\"\n",
" prompt+=\"New Ending: McDonalds\\n\"\n",
" prompt+=\"Rephrased Sentence: I got the call when I was at McDonalds.\\n\\n\"\n",
" \n",
" prompt+=\"Sentence: playing with my daughter\\n\"\n",
" prompt+=\"New Ending: kid\\n\"\n",
" prompt+=\"Rephrased Sentence: playing with my kid.\\n\\n\"\n",
"\n",
" prompt+=\"Sentence: I tripped and fell into a puddle\\n\"\n",
" prompt+=\"New Ending: fall\\n\"\n",
" prompt+=\"Rephrased Sentence: I tripped and into a puddle took a fall.\\n\\n\"\n",
" \n",
" prompt+=\"Sentence: eating at the restaurant\\n\"\n",
" prompt+=\"New Ending: dining\\n\"\n",
" prompt+=\"Rephrased Sentence: at the restaurant dining.\\n\\n\"\n",
"\n",
" prompt+=\"Sentence: {}\\nNew Ending: {}\\nRephrased Sentence:\".format(sentence, word) \n",
" return self.query(prompt.strip())[\"choices\"][0][\"text\"].strip()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "IDQzVP6IqI2X",
"colab_type": "code",
"colab": {}
},
"source": [
"test = poemWrapper(key=json.load(open(\"../gpt3/key.json\", \"r\"))[\"key\"])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "e2fYv1EnqI2a",
"colab_type": "code",
"colab": {},
"outputId": "c3a079ad-0873-4e41-a28b-eb6f35eb97c0"
},
"source": [
"test.rephraseToEndWithWord(\"I want to go to sleep\", \"slumber\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'I want to go to slumber.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 294
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "AEJdUKgBqI2c",
"colab_type": "code",
"colab": {},
"outputId": "d93bb3ce-4206-41b2-9483-053508395413"
},
"source": [
"test.rephraseToEndWithWord(\"I saw a scary goat\", \"animal\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'I saw a scary animal.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 295
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "7k7qzRdkqI2e",
"colab_type": "code",
"colab": {},
"outputId": "a73c9cdf-4252-4e59-f77f-b96e2e94f9b5"
},
"source": [
"test.rephraseToEndWithWord(\"I saw a scary animal\", \"goat\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'I saw a scary goat.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 296
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "xayo7YxGqI2g",
"colab_type": "code",
"colab": {},
"outputId": "d64e12d7-8c60-4c6d-ef8f-c857e9e80080"
},
"source": [
"test.rephraseToEndWithWord(\"rapping at my chamber door\", \"gate\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'rapping at my gate.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 297
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "uk_oCM_oqI2j",
"colab_type": "code",
"colab": {},
"outputId": "5fef17ec-a872-4918-bbe4-2a2ff79b0d44"
},
"source": [
"test.rephraseToEndWithWord(\"I heard rapping at my chamber door\", \"tapping\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'I heard tapping at my chamber door.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 298
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0sIXXvMKqI2l",
"colab_type": "text"
},
"source": [
"# This didn't work so leaving it in"
]
},
{
"cell_type": "code",
"metadata": {
"id": "wU8MQ3btqI2m",
"colab_type": "code",
"colab": {}
},
"source": [
"class poemWrapper(poemWrapper):\n",
" \n",
" def getRhymingWord(self, sentence, word):\n",
" \"\"\"\n",
" rephrase a sentence to end with a word\n",
" So far, this doesn't work!\n",
" Instead, I'll try to just get a bunch of words that are similar to the target, use search to find the most similar\n",
" \"\"\"\n",
" prompt=\"I want to know a rhyming word that is related to the sentence that rhymes with the target word\\n\"\n",
" prompt+=\"Sentence: I went to the store\\n\"\n",
" prompt+=\"Target Word: fly\\n\"\n",
" prompt+=\"Rhyming Word: 'fly' rhymes with buy'\\n\\n\"\n",
" \n",
" prompt+=\"Sentence: I want a BLT\\n\"\n",
" prompt+=\"Target Word: stitch\\n\"\n",
" prompt+=\"Rhyming Word: 'stitch' rhymes with sandwhich\\n\\n\"\n",
"\n",
" prompt+=\"Sentence: I don't know what time it is\\n\"\n",
" prompt+=\"Target Word: sour\\n\"\n",
" prompt+=\"Rhyming Word: 'sour' rhymes with hour\\n\\n\"\n",
" \n",
" prompt+=\"Sentence: I tripped and fell\\n\"\n",
" prompt+=\"Target Word: call\\n\"\n",
" prompt+=\"Rhyming Word: 'call' rhymes with fall\\n\\n\"\n",
" \n",
" prompt+=\"Sentence: James went to the doctor\\n\"\n",
" prompt+=\"Target Word: purse\\n\"\n",
" prompt+=\"Rhyming Word: 'purse' rhymes with nurse\\n\\n\"\n",
"\n",
" prompt+=\"Sentence: {}\\nTarget Word: {}\\nRhyming Word:\".format(sentence, word) \n",
" return self.query(prompt.strip())[\"choices\"][0][\"text\"].strip()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9hoG9ro0qI2o",
"colab_type": "code",
"colab": {}
},
"source": [
"test = poemWrapper(key=json.load(open(\"../gpt3/key.json\", \"r\"))[\"key\"])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "cjjGWm9OqI2q",
"colab_type": "code",
"colab": {},
"outputId": "8077fbac-78bb-44a7-b525-b9007730c33c"
},
"source": [
"test.getRhymingWord(\"I want to go to sleep\", \"cap\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"\"'cap' rhymes with sleep\""
]
},
"metadata": {
"tags": []
},
"execution_count": 91
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9UanLMk0qI2s",
"colab_type": "text"
},
"source": [
"# I'll see if I can get a list of all the rhyming words it can think of, then later select the one that's closest to the target sentence"
]
},
{
"cell_type": "code",
"metadata": {
"id": "50HhRwC4qI2t",
"colab_type": "code",
"colab": {}
},
"source": [
"class poemWrapper(poemWrapper):\n",
" \n",
" def getManyRhymingWord(self, word):\n",
" \"\"\"\n",
" gets a word from the API that rhyms with word\n",
" \"\"\"\n",
"\n",
" prompt=\"A word rhymes with a word if their endings sound the same. Words do not rhyme with themselves\\n\"\n",
" prompt+=\"q: what are words that rhymes with 'dog'\\n\"\n",
" prompt+=\"a: smog, cog, nog, blog, bog, fog, hog, jog, log, pog, sog\\n\\n\"\n",
" prompt+=\"q: what is a word that rhymes with 'ham'\\n\"\n",
" prompt+=\"a: spam, blam, am, cam, lamb, damn, fam, glam, gram, jam, nam, pam, spam, slam, scam, wham\\n\\n\" \n",
" prompt+=\"q: what is a word that rhymes with 'oaf'\\n\"\n",
" prompt+=\"a: loaf, goaf, oath, growth, behove, clove, drove, loaves, loathe\\n\\n\" \n",
" prompt+=\"q: what is a word that rhymes with '{}'\\na:\".format(word) \n",
" return self.query(prompt.strip())[\"choices\"][0][\"text\"].strip()\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "VNZXGUwKqI2u",
"colab_type": "code",
"colab": {}
},
"source": [
"test = poemWrapper(key=json.load(open(\"../gpt3/key.json\", \"r\"))[\"key\"])\n",
"test.set_single_kwarg(\"max_tokens\", 150)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "-whf_03MqI2w",
"colab_type": "code",
"colab": {},
"outputId": "f31e8bc1-87fb-44d7-a6be-c4ba4f27624b"
},
"source": [
"test.getManyRhymingWord(\"moat\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'boat, coat, coat, goat, boat, coat, coat, goat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat, coat,'"
]
},
"metadata": {
"tags": []
},
"execution_count": 127
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IL6YtwUSqI2z",
"colab_type": "text"
},
"source": [
"# Take my list of rhyming words and find the one closest to a sentence"
]
},
{
"cell_type": "code",
"metadata": {
"id": "1Fe382HQqI2z",
"colab_type": "code",
"colab": {}
},
"source": [
"class poemWrapper(poemWrapper):\n",
" \n",
" def mostSimilarWord(self, sentence, rhymeWord):\n",
" \"\"\"\n",
" gets a word from the API that rhyms with word\n",
" \"\"\"\n",
"\n",
" possibleRhymes = list(set([x.strip() for x in self.getManyRhymingWord(rhymeWord).split(\",\") if len(x) > 0]))\n",
" scores = openai.Engine(\"davinci\").search(\n",
" documents=[x for x in possibleRhymes],\n",
" query=sentence\n",
" )\n",
"\n",
" return max([(scores[\"data\"][i][\"score\"], possibleRhymes[i]) for i in range(len(possibleRhymes))])[1]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ucYUSWD7qI21",
"colab_type": "code",
"colab": {},
"outputId": "48b43342-e7dd-4d9e-e9f8-071cc940df25"
},
"source": [
"test = poemWrapper(key=json.load(open(\"../gpt3/key.json\", \"r\"))[\"key\"])\n",
"test.set_single_kwarg(\"max_tokens\", 120)\n",
"test.mostSimilarWord(\"Sailing on the seven seas\", \"moat\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'boat'"
]
},
"metadata": {
"tags": []
},
"execution_count": 128
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9PaeJjbKqI24",
"colab_type": "text"
},
"source": [
"# Take a word that's similar to a sentence and replace it at the end"
]
},
{
"cell_type": "code",
"metadata": {
"id": "TSD-RiyxqI24",
"colab_type": "code",
"colab": {}
},
"source": [
"class poemWrapper(poemWrapper):\n",
" \n",
" def replaceWithRhymingWord(self, sentence, word):\n",
" \"\"\"\n",
" imprved version of finding rhyming word;\n",
" finds the word rhyming with word that fits sentence and rephrases it around the word\n",
" \"\"\"\n",
"\n",
" replacementWord = self.mostSimilarWord(sentence, word)\n",
" return self.rephraseToEndWithWord(sentence, replacementWord)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "yDsCxptJqI2-",
"colab_type": "code",
"colab": {},
"outputId": "14236c58-ec8e-42a5-bd03-0ab75bea9d99"
},
"source": [
"test = poemWrapper(key=json.load(open(\"../gpt3/key.json\", \"r\"))[\"key\"])\n",
"test.set_single_kwarg(\"max_tokens\", 120)\n",
"test.replaceWithRhymingWord(\"Sailing on the seven seas\", \"moat\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Sailing on a boat'"
]
},
"metadata": {
"tags": []
},
"execution_count": 130
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zCHDVorOqI2_",
"colab_type": "text"
},
"source": [
"# Take 2 sentences, find all words that ends with the last word of the sentence, see which of those fits the next sentence, and replace it in it"
]
},
{
"cell_type": "code",
"metadata": {
"id": "bnHyHtYfqI3A",
"colab_type": "code",
"colab": {}
},
"source": [
"class poemWrapper(poemWrapper):\n",
" \n",
" def rhymeSentences(self, firstSentence, secondSentence):\n",
" \"\"\"\n",
" imprved version of finding rhyming word;\n",
" rhyme the second sentence to the first sentence\n",
" \"\"\"\n",
" \n",
" word = firstSentence.split()[-1].strip()\n",
" replacementWord = self.mostSimilarWord(secondSentence, word)\n",
" print(\"Found {} for {}\".format(replacementWord, word))\n",
" return self.rephraseToEndWithWord(secondSentence, replacementWord)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "0CZJzT6eqI3C",
"colab_type": "code",
"colab": {}
},
"source": [
"test = poemWrapper(key=json.load(open(\"../gpt3/key.json\", \"r\"))[\"key\"])\n",
"test.set_single_kwarg(\"max_tokens\", 200)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "F8wPjLeVqI3E",
"colab_type": "code",
"colab": {}
},
"source": [
"# https://www.cnn.com/2020/07/06/politics/georgia-governor-state-of-emergency/index.html"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "F8duPimzqI3H",
"colab_type": "code",
"colab": {},
"outputId": "b1840abf-c542-48db-c8ca-2b7ea18ec9e5"
},
"source": [
"s1 = \"Georgia Gov. Brian Kemp on Monday declared a state of emergency and activated as many as 1,000 National Guard members\"\n",
"s2 = \"\"\"Kemp, a Republican, said the executive order follows \"weeks of dramatically increased violent crime and property destruction in the City of Atlanta\"\"\"\n",
"test.rhymeSentences(s1, s2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Found members for members\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Kemp, a Republican, said the executive order follows \"weeks of dramatically increased violent crime and property destruction by members of the Democratic Party in the City of Atlanta'"
]
},
"metadata": {
"tags": []
},
"execution_count": 302
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "0kzIsdXzqI3J",
"colab_type": "code",
"colab": {},
"outputId": "a2ddd510-189a-4ddd-ddce-3cd28742caa0"
},
"source": [
"s1 = \"Once upon a midnight dreary, while I pondered, weak and weary\"\n",
"s2 = \"Over many a quaint and curious volume of forgotten lore\"\n",
"test.rhymeSentences(s1, s2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Found cheery for weary\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Over many a quaint and curious volume of forgotten lore, I cheery.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 303
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "nuuNPzPDqI3L",
"colab_type": "code",
"colab": {},
"outputId": "bda448df-52a6-4d8a-eb2a-8f1dac20d5cb"
},
"source": [
"s1 = \"While I nodded, nearly napping, suddenly there came a tapping\"\n",
"s2 = \"As of some one gently rapping, rapping at my chamber door.\"\n",
"test.rhymeSentences(s1, s2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Found tapping for tapping\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'As of some one gently tapping, tapping at my chamber door.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 306
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "xWJhNnTzqI3N",
"colab_type": "code",
"colab": {},
"outputId": "4063daac-cf59-4b43-f972-79d17439cd6b"
},
"source": [
"s1=\"\"\"“’Tis some visitor,” I muttered, “tapping at my chamber door\"\"\"\n",
"s2=\"\"\"Only this and nothing more.”\"\"\"\n",
"test.rhymeSentences(s1, s2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Found floor for door\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Only this and nothing more.” On the floor.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 305
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Tay45PFlqI3P",
"colab_type": "code",
"colab": {},
"outputId": "d12b0c47-5a16-478d-a5fc-6397f951ba60"
},
"source": [
"s1 = \"\"\"Ah, distinctly I remember it was in the bleak December;\"\"\"\n",
"s2 = \"\"\"And each separate dying ember wrought its ghost upon the floor.\"\"\"\n",
"test.rhymeSentences(s1, s2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Found December for December;\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'And each separate dying ember wrought its ghost upon the floor in December.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 307
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Mo2Go7wNqI3R",
"colab_type": "code",
"colab": {},
"outputId": "3aa9726f-7f61-4798-d677-a926dc7659d4"
},
"source": [
"s1 = \"Eagerly I wished the morrow;—vainly I had sought to borrow\"\n",
"s2 = \"From my books surcease of sorrow—sorrow for the lost Lenore\"\n",
"test.rhymeSentences(s1, s2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Found boorish for borrow\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'From my books surcease of boorishness—boorishness for the lost Lenore.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 308
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "xABO8pDyqI3T",
"colab_type": "code",
"colab": {},
"outputId": "8f247072-d6c8-41f6-ac0d-6201ce83c6d0"
},
"source": [
"s1 = \"For the rare and radiant maiden whom the angels name Lenore\"\n",
"s2 = \"Nameless here for evermore.\"\n",
"test.rhymeSentences(s1, s2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Found lens for Lenore\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Nameless here for evermore.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 311
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "QdU4VTuJqI3V",
"colab_type": "code",
"colab": {},
"outputId": "b3c0db94-4aee-4eb0-d5a3-539fe075e575"
},
"source": [
"s1 = \"And the silken, sad, uncertain rustling of each purple curtain\"\n",
"s2 = \"Thrilled me—filled me with fantastic terrors never felt before\"\n",
"test.rhymeSentences(s1, s2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Found curtain for curtain\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Thrilled me—filled me with fantastic terrors never felt before curtain.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 312
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "LVVCXv77qI3Y",
"colab_type": "text"
},
"source": [
"# And this adds a randomly rhyming next sentence. I think it needs to end with punctuation to work"
]
},
{
"cell_type": "code",
"metadata": {
"id": "osnrAsqoqI3a",
"colab_type": "code",
"colab": {}
},
"source": [
"import random\n",
"class poemWrapper(poemWrapper):\n",
" \n",
" def addRhymingSentence(self, sentence):\n",
" \"\"\"\n",
" imprved version of finding rhyming word;\n",
" rhyme the second sentence to the first sentence\n",
" \"\"\"\n",
" \n",
" word = sentence.split()[-1].strip()\n",
" possibleRhymes = list(set([x.strip() for x in self.getManyRhymingWord(word).split(\",\") if (len(x) > 0 and x != word)]))\n",
" \n",
" target_word = random.choice(possibleRhymes)\n",
" \n",
" prompt = \"I want you to construct a nextSentence that follows the startSentence\\n\\n\"\n",
" prompt += \"startSentence: humpty dumpty sat on a wall\\n\"\n",
" prompt += \"nextEnding: fall\\n\"\n",
" prompt += \"nextSentence: humpty dumpty had a great fall\\n\\n\"\n",
" \n",
" prompt += \"startSentence: All the king's horses and all the king's men\\n\"\n",
" prompt += \"nextEnding: again\\n\"\n",
" prompt += \"nextSentence: Couldn't put humpty together again\\n\\n\"\n",
" \n",
" prompt += \"startSentence: james once ate a turnip\\n\"\n",
" prompt += \"nextEnding: grip\\n\"\n",
" prompt += \"nextSentence: james then lost his grip\\n\\n\"\n",
"\n",
" prompt += \"startSentence: I saw a cow walk into a lake\\n\"\n",
" prompt += \"nextEnding: steak\\n\"\n",
" prompt += \"nextSentence: I thought it would make great steak\\n\\n\" \n",
" \n",
" prompt += \"startSentence: {}.\\n\".format(sentence) #it needs punctuaton or it just repeats\n",
" prompt += \"nextEnding: {}\\n\".format(target_word)\n",
" prompt += \"nextSentence:\"\n",
"\n",
" return self.query(prompt.strip())[\"choices\"][0][\"text\"].strip()\n",
"\n",
" "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "rVrgRA4kqI3d",
"colab_type": "code",
"colab": {}
},
"source": [
"test = poemWrapper(key=json.load(open(\"../gpt3/key.json\", \"r\"))[\"key\"])\n",
"test.set_single_kwarg(\"max_tokens\", 100)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "n7W61gTmqI3e",
"colab_type": "code",
"colab": {},
"outputId": "a7756a45-c978-4c04-ac60-842564fb171d"
},
"source": [
"for i in range(10):\n",
" print(test.addRhymingSentence(\"I was talking with a friend about going to the store\"))"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"I said, \"I'm going to the store to buy some storks.\"\n",
"I said, \"I'm going to the store to buy some storks.\"\n",
"I was talking with a friend about going to the store.\n",
"I said, \"I'm going to the store to buy a stork.\"\n",
"I said, \"I'm going to the store to buy a stork.\"\n",
"I was talking with a friend about going to the store.\n",
"I was talking with a boor about going to the store.\n",
"I said, \"I'm going to the store to buy some storks.\"\n",
"I said, \"I think I'll stork it.\"\n",
"I said, \"I'm going to the store with storky.\"\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IRxz1kT5qI3h",
"colab_type": "text"
},
"source": [
"# well, it occasionally gets a rhyme"
]
},
{
"cell_type": "code",
"metadata": {
"scrolled": true,
"id": "lpK8cBfwqI3h",
"colab_type": "code",
"colab": {},
"outputId": "ab936029-ce0f-4b34-c514-19cf7899649c"
},
"source": [
"for i in range(10):\n",
" print(test.addRhymingSentence(\"I saw on the floor something being chased by the cat\"))"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"I thought it was a cat chasing something on the floor\n",
"I thought it would make great wet cat food\n",
"I saw on the floor something being chased by the cat. It sat on the mat.\n",
"I thought it was a cat chasing a pat\n",
"I saw on the floor something being chased by the cat. It sat on the mat.\n",
"I saw on the floor something being chased by the cat. It sat on the mat.\n",
"I saw on the floor something being chased by the cat. It sat on the mat.\n",
"I saw on the floor something being chased by the cat. It sat on the mat.\n",
"I thought it was a fat rat\n",
"I thought it would make great wet cat food\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "R45y--1lqI3k",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "o69GgdwFqI3m",
"colab_type": "text"
},
"source": [
"# Is it able to rhyme imaginary words?"
]
},
{
"cell_type": "code",
"metadata": {
"id": "wm3w2O9gqI3m",
"colab_type": "code",
"colab": {}
},
"source": [
"suffixes = [\"arn\", \"ano\", \"ing\", \"een\"]\n",
"prefixes = [\"max\", \"millan\", \"freep\", \"zooaz\"]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "myce-Hj_qI3o",
"colab_type": "code",
"colab": {}
},
"source": [
"myWords = [x+y for x in suffixes for y in prefixes]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Fd9YYmszqI3q",
"colab_type": "code",
"colab": {},
"outputId": "5a2f9909-bebf-4bcd-9520-880bd95fb979"
},
"source": [
"print(\", \".join([x for x in myWords if x.endswith(\"max\")]))"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"arnmax, anomax, ingmax, eenmax\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "RwoTTtBtqI3r",
"colab_type": "code",
"colab": {}
},
"source": [
"prompt = \"words rhyme if they have similar sounding endings.\\n\"\n",
"prompt += \"my words are: {}.\\n\".format(\", \".join(myWords))\n",
"prompt += \"q: Which of my words rhymes with arnmax?\\n\"\n",
"prompt += \"a: arnmax, anomax, ingmax, eenmax\\n\\n\"\n",
"prompt += \"q: Which of my words rhmye with {}?\\na:\""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "LfNdx4aUqI3t",
"colab_type": "code",
"colab": {},
"outputId": "27e7bdf1-d012-477e-8916-67a6d65dd0e2"
},
"source": [
"print(prompt)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"words rhyme if they have similar sounding endings.\n",
"my words are: arnmax, arnmillan, arnfreep, arnzooaz, anomax, anomillan, anofreep, anozooaz, ingmax, ingmillan, ingfreep, ingzooaz, eenmax, eenmillan, eenfreep, eenzooaz.\n",
"q: Which of my words rhymes with arnmax?\n",
"a: arnmax, anomax, ingmax, eenmax\n",
"\n",
"q: Which of my words rhmye with {}?\n",
"a:\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "BkgVJ5YqqI3v",
"colab_type": "code",
"colab": {}
},
"source": [
"guesses = {}"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "b9AUHimMqI3y",
"colab_type": "code",
"colab": {}
},
"source": [
"for word in myWords:\n",
" guesses[word] = test.query(prompt.format(word))"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "jMLJBg_aqI30",
"colab_type": "code",
"colab": {}
},
"source": [
"extracted_guesses = {}\n",
"for word in guesses:\n",
" extracted_guesses[word] = guesses[word][\"choices\"][0][\"text\"]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "H7HOcsQ8qI33",
"colab_type": "text"
},
"source": [
"# here's the rhyming results: looks like it worked"
]
},
{
"cell_type": "code",
"metadata": {
"scrolled": true,
"id": "ci2vTny7qI34",
"colab_type": "code",
"colab": {},
"outputId": "f8784f12-ce5b-4403-9992-a3cfc261b062"
},
"source": [
"extracted_guesses"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'arnmax': ' arnmax, anomax, ingmax, eenmax',\n",
" 'arnmillan': ' arnmillan, anomillan, ingmillan, eenmillan',\n",
" 'arnfreep': ' arnfreep, anofreep, ingfreep, eenfreep',\n",
" 'arnzooaz': ' arnzooaz, anozooaz, ingzooaz, eenzooaz',\n",
" 'anomax': ' anomax, arnmax, ingmax, eenmax',\n",
" 'anomillan': ' anomax, anomillan, ingmillan, eenmillan',\n",
" 'anofreep': ' anomax, anofreep, ingzooaz, eenfreep',\n",
" 'anozooaz': ' anomax, anozooaz, ingzooaz, eenzooaz',\n",
" 'ingmax': ' arnmax, anomax, ingmax, eenmax',\n",
" 'ingmillan': ' arnmax, anomax, ingmax, eenmax',\n",
" 'ingfreep': ' arnfreep, anofreep, eenfreep',\n",
" 'ingzooaz': ' ingzooaz, anozooaz, eenzooaz, arnzooaz',\n",
" 'eenmax': ' eenmax, anomax, ingmax, arnmax',\n",
" 'eenmillan': ' eenmillan, anomillan, ingmillan, eenmillan',\n",
" 'eenfreep': ' eenfreep, anofreep, ingfreep, anozooaz',\n",
" 'eenzooaz': ' eenzooaz, anozooaz, ingzooaz, eenzooaz'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 379
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YF5wqVrEqI36",
"colab_type": "text"
},
"source": [
"# here's what the request looked like"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pijdVRcnqI37",
"colab_type": "code",
"colab": {},
"outputId": "dfbc58f4-87b4-437f-cf3d-f699a1e0d650"
},
"source": [
"print(test.last_request)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"words rhyme if they have similar sounding endings.\n",
"my words are: arnmax, arnmillan, arnfreep, arnzooaz, anomax, anomillan, anofreep, anozooaz, ingmax, ingmillan, ingfreep, ingzooaz, eenmax, eenmillan, eenfreep, eenzooaz.\n",
"q: Which of my words rhymes with arnmax?\n",
"a: arnmax, anomax, ingmax, eenmax\n",
"\n",
"q: Which of my words rhmye with eenzooaz?\n",
"a:\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "MkDgFjcaqI38",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment