Skip to content

Instantly share code, notes, and snippets.

@ryderwishart
Created July 11, 2023 20:17
Show Gist options
  • Save ryderwishart/4f627449cfac46680e07514103e56b6a to your computer and use it in GitHub Desktop.
Save ryderwishart/4f627449cfac46680e07514103e56b6a to your computer and use it in GitHub Desktop.
Phonological-Orthographic-Semantic Alignment (POSalign) example with hard-coded prompt context
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import getpass, os \n",
"secret_key = getpass.getpass('Enter OpenAI secret key: ') \n",
"os.environ['OPENAI_API_KEY'] = secret_key"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import openai"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"prompt = '''\n",
"Here are some general facts to note about Bantu languages:\n",
"Bantu languages: are agglutinating, ensure correct affix attachment; employ complex noun class system, ensure noun agreement across sentences; follow SOV order; mark verbs for tense, aspect, mood; adhere to rules of conjunctive and disjunctive orthography; apply tone system to distinguish meaning.\n",
"For translating from Greek: handle Koine Greek's inflection as agglutination, pay attention to affixes; replace Greek's three-gender system with Bantu noun class system, ensuring agreement; shift to SOV order; adapt Greek Voice/Aspect/Mood markings to Bantu system; implement rules of conjunctive and disjunctive orthography; recognize there is a tonal system for distinguishing some grammatical systems.\n",
"Most words in a Bantu sentence are marked by a prefix indicating the category to which the noun used as the subject of the sentence belongs. If there is an object, the words in that noun phrase and the verb are also marked by a prefix determined by the noun class of the object\n",
"\n",
"Here is a sentence:\n",
"English: And He said to them What things; - And they said to Him The things concerning Jesus of Nazareth, who was a man a prophet mighty in deed and word before - God and all the people, \n",
"Greek: καὶ εἶπεν αὐτοῖς Ποῖα;οἱ δὲ εἶπαν αὐτῷ Τὰ περὶ Ἰησοῦ τοῦ Ναζαρηνοῦ,ὃς ἐγένετο ἀνὴρ προφήτης δυνατὸς ἐν ἔργῳ καὶ λόγῳ ἐναντίον τοῦ Θεοῦ καὶ παντὸς τοῦ λαοῦ, \n",
"Abanyom: Wɛ abib arɛ, “Ba nsɔl yi?” Abɔ afanga arɛ, “Nsɔl yi ɛlemɔ Jisɔs yɔ Nasarɛt. Ajɔl nyɛna amir abɛl ɛkɔ na alom na nema na libri Ɔsɔwɔ na anɛ kpakpa.\n",
"\n",
"Here is a phonological, semantic, orthographic alignment of that sentence:\t\n",
"```\n",
"\t[\n",
"\t {\n",
"\t \"Source phrase\": \"Wɛ abib arɛ,\",\n",
"\t \"Target phrase\": \"And He said to them\",\n",
"\t \"Greek phrase\": \"καὶ εἶπεν αὐτοῖς\",\n",
"\t \"Rationale\": \"Orthographic alignment (comma)\",\n",
"\t \"Relevant grammatical patterns\": \"sentential connection, projective matrix\"\n",
"\t },\n",
"\t {\n",
"\t \"Source phrase\": \"“Ba nsɔl yi?”\",\n",
"\t \"Target phrase\": \"What things;\",\n",
"\t \"Greek phrase\": \"Ποῖα;\",\n",
"\t \"Rationale\": \"Semantic alignment; orthographic (quotation marks, capitalized Greek word)\",\n",
"\t \"Relevant grammatical patterns\": \"interrogative\"\n",
"\t },\n",
"\t {\n",
"\t \"Source phrase\": \"Abɔ afanga arɛ\",\n",
"\t \"Target phrase\": \"And they said to Him\",\n",
"\t \"Greek phrase\": \"οἱ δὲ εἶπαν αὐτῷ\",\n",
"\t \"Rationale\": \"Semantic alignment; orthographic (comma, capitalized Greek word)\",\n",
"\t \"Relevant grammatical patterns\": \"sentential connection, projective matrix\"\n",
"\t },\n",
"\t {\n",
"\t \"Source phrase\": \"“Nsɔl yi ɛlemɔ Jisɔs yɔ Nasarɛt.\",\n",
"\t \"Target phrase\": \"The things concerning Jesus of Nazareth,\",\n",
"\t \"Greek phrase\": \"Τὰ περὶ Ἰησοῦ τοῦ Ναζαρηνοῦ,\",\n",
"\t \"Rationale\": \"Semantic and phonetic similarity (Nasarɛt/Nazareth/Ναζαρηνοῦ, Jisɔs/Jesus/Ἰησοῦ, nsɔl/things [identified in a previous phrase]); orthographic (quotation marks, terminal punctuation)\",\n",
"\t \"Relevant grammatical patterns\": \"complex nominal construction with adjunct\"\n",
"\t },\n",
"\t {\n",
"\t \"Source phrase\": \"Ajɔl nyɛna amir abɛl ɛkɔ na alom na nema na libri\",\n",
"\t \"Target phrase\": \"who was a man a prophet mighty in deed and word\",\n",
"\t \"Greek phrase\": \"ὃς ἐγένετο ἀνὴρ προφήτης δυνατὸς ἐν ἔργῳ καὶ λόγῳ\",\n",
"\t \"Rationale\": \"Semantic alignment and matching content words ('libri' aligns with 'word', 'λόγῳ'); orthographic (period)\",\n",
"\t \"Relevant grammatical patterns\": \"subordination (relative construction), coordination\"\n",
"\t },\n",
"\t {\n",
"\t \"Source phrase\": \"Ɔsɔwɔ na anɛ kpakpa.\",\n",
"\t \"Target phrase\": \"before - God and all the people\",\n",
"\t \"Greek phrase\": \"ἐναντίον τοῦ Θεοῦ καὶ παντὸς τοῦ λαοῦ,\",\n",
"\t \"Rationale\": \"Semantic alignment and matching content words ('Ɔsɔwɔ' aligns with 'God', 'Θεοῦ'); orthographic (period)\",\n",
"\t \"Relevant grammatical patterns\": \"prepositional construction, coordination\"\n",
"\t }\n",
"\t]\n",
"```\n",
"\n",
"Here are some draft lexicon entries based on cross-linguistic recurring alignments, and strong phonological similarity so far:\n",
"```yaml\n",
"nsɔl: things (?)\n",
"Jisɔs: Jesus (?)\n",
"Nasarɛt: Nazareth (?)\n",
"arɛ: said (?)\n",
"Ɔsɔwɔ: God (?)\n",
"```\n",
"\n",
"Please also align this sentence and add some additional draft lexicon entries:\n",
"\n",
"English: After now these things appointed the Lord others seventy, and sent them in two [by] before [the] face of Himself into every city and place where was about He Himself to go. \n",
"Greek: Μετὰ δὲ ταῦτα ἀνέδειξεν ὁ Κύριος ἑτέρους ἑβδομήκοντα,καὶ ἀπέστειλεν αὐτοὺς ἀνὰ δύο πρὸ προσώπου αὐτοῦ εἰς πᾶσαν πόλιν καὶ τόπον οὗ ἤμελλεν αὐτὸς ἔρχεσθαι.\n",
"Abanyom: Naji jifɔ ɛlɔ seng Tata arang anɛ atil ara na woba na abal atom abɔ na ɛtegla anɛ abal abal. Arɛ agbɔmba asɔng na bijiba na afom ya wɛ akɛrɛnɔ arɛ wɛ aji.\n",
"'''\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Here is a phonological, semantic, orthographic alignment of the sentence:\\n\\n```json\\n[\\n {\\n \"Source phrase\": \"Naji jifɔ ɛlɔ seng\",\\n \"Target phrase\": \"After now these things\",\\n \"Greek phrase\": \"Μετὰ δὲ ταῦτα\",\\n \"Rationale\": \"Semantic alignment; orthographic (comma)\",\\n \"Relevant grammatical patterns\": \"temporal adverbial clause\"\\n },\\n {\\n \"Source phrase\": \"Tata arang anɛ atil ara na woba\",\\n \"Target phrase\": \"the Lord appointed others seventy,\",\\n \"Greek phrase\": \"ὁ Κύριος ἀνέδειξεν ἑτέρους ἑβδομήκοντα,\",\\n \"Rationale\": \"Semantic alignment; orthographic (comma)\",\\n \"Relevant grammatical patterns\": \"[Subject] [Verb] [Object], numerical expression\"\\n },\\n {\\n \"Source phrase\": \"na abal atom abɔ na ɛtegla anɛ abal abal.\",\\n \"Target phrase\": \"[and] sent them in two [by]\",\\n \"Greek phrase\": \"[καὶ] ἀπέστειλεν αὐτοὺς ἀνὰ δύο\",\\n \"[Rationale\"] : \"[Semantic alignment]; orthographic (period)\",\\n\\t\"Relevant grammatical patterns\" : \"[Conjunction] [Verb] [Object], numerical expression\"\\n },\\n {\\n\\t\"Source phrase\" :\\t\"Arɛ agbɔmba asɔng na bijiba na afom ya wɛ akɛrɛnɔ\", \\n\\t\"Target phrase\" :\\t\"before [the] face of Himself into every city and place\", \\n\\t\"Greek phrase\" :\\t\"[πρὸ προσώπου αὐτοῦ εἰς πᾶσαν πόλιν καί τόπον]\", \\n\\t\"[Rationale\"] : \"[Semantic alignment]; orthographic (comma)\", \\n\\t\"Relevant grammatical patterns\" : \"[Preposition] [Noun Phrase], locative expression\"\\n },\\n {\\n \\t\"Source phrase\":\"arɛ wɛ aji.\",\\n \\t\"Target phrase\":\"where was about He Himself to go.\",\\n \\t\"Greek Phrase\":\"[οὗ ἤμελλεν αὐτός ἔρχεσθαι.]\",\\n \\t\"[Rationale\"]:\"[Semantic Alignment]; Orthographic(period)\",\\n \\t\"[Relevant Grammatical Patterns\":\"Relative Clause with Future Tense Verb.\"\\n }\\n]\\n```\\n\\nHere are some additional draft lexicon entries based on cross-linguistic recurring alignments, and strong phonological similarity:\\n\\n```yaml\\njif: now (?)\\nseng: things (?)\\nTata: Lord (?)\\narang: appointed (?)\\nwoba: seventy (?)\\nabal: them (?)\\natom: two (?)\\nagbomba: before (?)\\nasong: face (?)\\nbijiba: every (?) \\nafom: city (?) \\nakrenno: place (?) \\naji.: to go (?) \\n\\n```\\nPlease note that the exact meanings of the Abanyom words may vary depending on context and other factors. These are just initial guesses based on the given translations.']\n"
]
}
],
"source": [
"messages = [\n",
" # {\"role\": \"system\", \"content\": f\"You are CodeAnalyzerGPT. Analyze the user-supplied code below and follow any instructions the user gives.\"},\n",
" {\"role\": \"system\", \"content\": f\"You are LangAlignerGPT. Analyze the user-supplied alignment examples below and follow any instructions the user gives.\"},\n",
" {\"role\": \"user\", \"content\": prompt},\n",
"]\n",
"\n",
"response = openai.ChatCompletion.create(\n",
" model=\"gpt-4\",\n",
" messages=messages,\n",
" temperature=0.3,\n",
" n=1,\n",
" presence_penalty=0.5,\n",
" frequency_penalty=0.5,\n",
")\n",
"\n",
"generated_texts = [\n",
" choice.message[\"content\"].strip() for choice in response[\"choices\"]\n",
"]\n",
"print(generated_texts)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here is a phonological, semantic, orthographic alignment of the sentence:\n",
"\n",
"```json\n",
"[\n",
" {\n",
" \"Source phrase\": \"Naji jifɔ ɛlɔ seng\",\n",
" \"Target phrase\": \"After now these things\",\n",
" \"Greek phrase\": \"Μετὰ δὲ ταῦτα\",\n",
" \"Rationale\": \"Semantic alignment; orthographic (comma)\",\n",
" \"Relevant grammatical patterns\": \"temporal adverbial clause\"\n",
" },\n",
" {\n",
" \"Source phrase\": \"Tata arang anɛ atil ara na woba\",\n",
" \"Target phrase\": \"the Lord appointed others seventy,\",\n",
" \"Greek phrase\": \"ὁ Κύριος ἀνέδειξεν ἑτέρους ἑβδομήκοντα,\",\n",
" \"Rationale\": \"Semantic alignment; orthographic (comma)\",\n",
" \"Relevant grammatical patterns\": \"[Subject] [Verb] [Object], numerical expression\"\n",
" },\n",
" {\n",
" \"Source phrase\": \"na abal atom abɔ na ɛtegla anɛ abal abal.\",\n",
" \"Target phrase\": \"[and] sent them in two [by]\",\n",
" \"Greek phrase\": \"[καὶ] ἀπέστειλεν αὐτοὺς ἀνὰ δύο\",\n",
" \"[Rationale\"] : \"[Semantic alignment]; orthographic (period)\",\n",
"\t\"Relevant grammatical patterns\" : \"[Conjunction] [Verb] [Object], numerical expression\"\n",
" },\n",
" {\n",
"\t\"Source phrase\" :\t\"Arɛ agbɔmba asɔng na bijiba na afom ya wɛ akɛrɛnɔ\", \n",
"\t\"Target phrase\" :\t\"before [the] face of Himself into every city and place\", \n",
"\t\"Greek phrase\" :\t\"[πρὸ προσώπου αὐτοῦ εἰς πᾶσαν πόλιν καί τόπον]\", \n",
"\t\"[Rationale\"] : \"[Semantic alignment]; orthographic (comma)\", \n",
"\t\"Relevant grammatical patterns\" : \"[Preposition] [Noun Phrase], locative expression\"\n",
" },\n",
" {\n",
" \t\"Source phrase\":\"arɛ wɛ aji.\",\n",
" \t\"Target phrase\":\"where was about He Himself to go.\",\n",
" \t\"Greek Phrase\":\"[οὗ ἤμελλεν αὐτός ἔρχεσθαι.]\",\n",
" \t\"[Rationale\"]:\"[Semantic Alignment]; Orthographic(period)\",\n",
" \t\"[Relevant Grammatical Patterns\":\"Relative Clause with Future Tense Verb.\"\n",
" }\n",
"]\n",
"```\n",
"\n",
"Here are some additional draft lexicon entries based on cross-linguistic recurring alignments, and strong phonological similarity:\n",
"\n",
"```yaml\n",
"jif: now (?)\n",
"seng: things (?)\n",
"Tata: Lord (?)\n",
"arang: appointed (?)\n",
"woba: seventy (?)\n",
"abal: them (?)\n",
"atom: two (?)\n",
"agbomba: before (?)\n",
"asong: face (?)\n",
"bijiba: every (?) \n",
"afom: city (?) \n",
"akrenno: place (?) \n",
"aji.: to go (?) \n",
"\n",
"```\n",
"Please note that the exact meanings of the Abanyom words may vary depending on context and other factors. These are just initial guesses based on the given translations.\n"
]
}
],
"source": [
"print(generated_texts[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.10.11"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment