{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Translating English Sentences into Propositional Logic Statements\n",
    "\n",
    "In a Logic course, one exercise is to turn an English sentence like this:\n",
    "\n",
    "> *Sieglinde will survive, and either her son will gain the Ring and Wotan’s plan will be fulfilled or else Valhalla will be destroyed.*\n",
    "\n",
    "Into a formal Propositional Logic statement: \n",
    "\n",
    "    P ⋀ ((Q ⋀ R) ∨ S)\n",
    "    \n",
    "along with definitions of the propositions:\n",
    "\n",
    "    P: Sieglinde will survive\n",
    "    Q: Sieglinde’s son will gain the Ring\n",
    "    R: Wotan’s plan will be fulfilled\n",
    "    S: Valhalla will be destroyed\n",
    "\n",
    "For some sentences, it takes detailed knowledge to get a good translation. The following two sentences are ambiguous, with different prefered interpretations, and translating them correctly requires knowledge of eating habits:\n",
    "\n",
    "    I will eat salad or I will eat bread and I will eat butter.     P ∨ (Q ⋀ R)\n",
    "    I will eat salad or I will eat soup  and I will eat ice cream. (P ∨ Q) ⋀ R\n",
    "\n",
    "But for many sentences, the translation process is automatic, with no special knowledge required.  I will develop a program to handle these easy sentences. The program is based on the idea of a series of translation rules of the form:\n",
    "\n",
    "    Rule('{P} ⇒ {Q}', 'if {P} then {Q}', 'if {P}, {Q}')\n",
    "    \n",
    "which means that the logic translation will have the form `'P ⇒ Q'`, whenever the English sentence has either the form `'if P then Q'` or  `'if P, Q'`, where `P` and `Q` can match any non-empty subsequence of characters.  Whatever matches `P` and `Q` will be recursively processed by the rules. The rules are in order&mdash;top to bottom, left to right, and the first rule that matches in that order will be accepted, no matter what, so be sure you order your rules carefully. One guideline I have adhered to is to put all the rules that start with a keyword (like `'if'` or `'neither'`) before the rules that start with a variable (like `'{P}'`); that way you avoid accidently having a keyword swallowed up inside a `'{P}'`.\n",
    "\n",
    "Notice that given the sentence \"*Sieglinde will survive*\", the program should make up a new propositional symbol, `P`, and record the fact that `P` refers to \"*Sieglinde will survive*\". But the negative sentence \"*Sieglinde will not survive*\", should be translated as `～P`, where again `P` is  \"*Sieglinde will survive*\". So to fully specify the translation process, we need to define both `rules` and `negations`. (We do that using [regular expressions](https://docs.python.org/3.5/library/re.html), which can sometimes be confusing.)\n",
    "\n",
    "First the function to define a rule (and some auxiliary functions):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "def Rule(output, *patterns):\n",
    "    \"A rule that produces `output` if the entire input matches any one of the `patterns`.\" \n",
    "    return (output, [name_group(pat) + '$' for pat in patterns])\n",
    "\n",
    "def name_group(pat):\n",
    "    \"Replace '{Q}' with '(?P<Q>.+?)', which means 'match 1 or more characters, and call it Q'\"\n",
    "    return re.sub('{(.)}', r'(?P<\\1>.+?)', pat)\n",
    "            \n",
    "def word(w):\n",
    "    \"Return a regex that matches w as a complete word (not letters inside a word).\"\n",
    "    return r'\\b' + w + r'\\b' # '\\b' matches at word boundary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And now the actual rules. If you have a sentence that is not translated correctly by this program, you can  augment these rules to handle your sentence."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "rules = [\n",
    "    Rule('{P} ⇒ {Q}',         'if {P} then {Q}', 'if {P}, {Q}'),\n",
    "    Rule('{P} ⋁ {Q}',          'either {P} or else {Q}', 'either {P} or {Q}'),\n",
    "    Rule('{P} ⋀ {Q}',          'both {P} and {Q}'),\n",
    "    Rule('～{P} ⋀ ～{Q}',       'neither {P} nor {Q}'),\n",
    "    Rule('～{A}{P} ⋀ ～{A}{Q}', '{A} neither {P} nor {Q}'), # The Kaiser neither ...\n",
    "    Rule('～{Q} ⇒ {P}',        '{P} unless {Q}'),\n",
    "    Rule('{P} ⇒ {Q}',          '{Q} provided that {P}', '{Q} whenever {P}', '{P} implies {Q}',\n",
    "                               '{P} therefore {Q}', '{Q}, if {P}', '{Q} if {P}', '{P} only if {Q}'),\n",
    "    Rule('{P} ⋀ {Q}',          '{P} and {Q}', '{P} but {Q}'),\n",
    "    Rule('{P} ⋁ {Q}',          '{P} or else {Q}', '{P} or {Q}'),\n",
    "    ]\n",
    "\n",
    "negations = [\n",
    "    (word(\"not\"), \"\"),\n",
    "    (word(\"cannot\"), \"can\"),\n",
    "    (word(\"can't\"), \"can\"),\n",
    "    (word(\"won't\"), \"will\"),\n",
    "    (word(\"ain't\"), \"is\"),\n",
    "    (\"n't\", \"\"), # matches as part of a word: didn't, couldn't, etc.\n",
    "    ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the mechanism to process these rules. Note that `defs` is a dict of definitions of propositional symbols: `{P: 'english'}`. The three `match_*` functions return two values: the translation of a sentence, and a dict of defintions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def match_rules(sentence, rules, defs):\n",
    "    \"\"\"Match sentence against all the rules, accepting the first match; or else make it an atomic proposition.\n",
    "    Return two values: the Logic translation and a dict of {P: 'english'} definitions.\"\"\"\n",
    "    sentence = clean(sentence)\n",
    "    for rule in rules:\n",
    "        result = match_rule(sentence, rule, defs)\n",
    "        if result: \n",
    "            return result\n",
    "    return match_atomic_proposition(sentence, negations, defs)\n",
    "        \n",
    "def match_rule(sentence, rule, defs):\n",
    "    \"Match a single rule, returning the logic translation and the dict of definitions if the match succeeds.\"\n",
    "    output, patterns = rule\n",
    "    for pat in patterns:\n",
    "        match = re.match(pat, sentence, flags=re.I)\n",
    "        if match:\n",
    "            groups = match.groupdict()\n",
    "            for P in sorted(groups): # Recursively apply rules to each of the matching groups\n",
    "                groups[P] = match_rules(groups[P], rules, defs)[0]\n",
    "            return '(' + output.format(**groups) + ')', defs\n",
    "        \n",
    "def match_atomic_proposition(sentence, negations, defs):\n",
    "    \"No rule matched; sentence is an atom. Add new proposition to defs. Handle negation.\"\n",
    "    polarity = ''\n",
    "    for (neg, pos) in negations:\n",
    "        (sentence, n) = re.subn(neg, pos, sentence, flags=re.I)\n",
    "        polarity += n * '～'\n",
    "    sentence = clean(sentence)\n",
    "    P = proposition_name(sentence, defs)\n",
    "    defs[P] = sentence\n",
    "    return polarity + P, defs\n",
    "    \n",
    "def proposition_name(sentence, defs, names='PQRSTUVWXYZBCDEFGHJKLMN'):\n",
    "    \"Return the old name for this sentence, if used before, or a new, unused name.\"\n",
    "    inverted = {defs[P]: P for P in defs}\n",
    "    if sentence in inverted:\n",
    "        return inverted[sentence]                      # Find previously-used name\n",
    "    else:\n",
    "        return next(P for P in names if P not in defs) # Use a new unused name\n",
    "    \n",
    "def clean(text): \n",
    "    \"Remove redundant whitespace; handle curly apostrophe and trailing comma/period.\"\n",
    "    return ' '.join(text.split()).replace(\"’\", \"'\").rstrip('.').rstrip(',')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And finally some test sentences and a top-level function to produce output:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__________________________________________________________________________________________________________ \n",
      "Polkadots and Moonbeams \n",
      "Logic: (P ⋀ Q)\n",
      "P: Polkadots\n",
      "Q: Moonbeams\n",
      "__________________________________________________________________________________________________________ \n",
      "If you liked it then you shoulda put a ring on it \n",
      "Logic: (P ⇒ Q)\n",
      "P: you liked it\n",
      "Q: you shoulda put a ring on it\n",
      "__________________________________________________________________________________________________________ \n",
      "If you build it, he will come \n",
      "Logic: (P ⇒ Q)\n",
      "P: you build it\n",
      "Q: he will come\n",
      "__________________________________________________________________________________________________________ \n",
      "It don't mean a thing, if it ain't got that swing \n",
      "Logic: (～P ⇒ ～Q)\n",
      "P: it is got that swing\n",
      "Q: It do mean a thing\n",
      "__________________________________________________________________________________________________________ \n",
      "If loving you is wrong, I don't want to be right \n",
      "Logic: (P ⇒ ～Q)\n",
      "P: loving you is wrong\n",
      "Q: I do want to be right\n",
      "__________________________________________________________________________________________________________ \n",
      "Should I stay or should I go \n",
      "Logic: (P ⋁ Q)\n",
      "P: Should I stay\n",
      "Q: should I go\n",
      "__________________________________________________________________________________________________________ \n",
      "I shouldn't go and I shouldn't not go \n",
      "Logic: (～P ⋀ ～～P)\n",
      "P: I should go\n",
      "__________________________________________________________________________________________________________ \n",
      "If I fell in love with you, would you promise to be true and help me understand \n",
      "Logic: (P ⇒ (Q ⋀ R))\n",
      "P: I fell in love with you\n",
      "Q: would you promise to be true\n",
      "R: help me understand\n",
      "__________________________________________________________________________________________________________ \n",
      "I could while away the hours conferrin' with the flowers, consulting with the rain and my head I'd be a\n",
      "scratchin' while my thoughts are busy hatchin' if I only had a brain \n",
      "Logic: (P ⇒ (Q ⋀ R))\n",
      "P: I only had a brain\n",
      "Q: I could while away the hours conferrin' with the flowers, consulting with the rain\n",
      "R: my head I'd be a scratchin' while my thoughts are busy hatchin'\n",
      "__________________________________________________________________________________________________________ \n",
      "There's a federal tax, and a state tax, and a city tax, and a street tax, and a sewer tax \n",
      "Logic: (P ⋀ (Q ⋀ (R ⋀ (S ⋀ T))))\n",
      "P: There's a federal tax\n",
      "Q: a state tax\n",
      "R: a city tax\n",
      "S: a street tax\n",
      "T: a sewer tax\n",
      "__________________________________________________________________________________________________________ \n",
      "A ham sandwich is better than nothing and nothing is better than eternal happiness therefore a ham\n",
      "sandwich is better than eternal happiness \n",
      "Logic: ((P ⋀ Q) ⇒ R)\n",
      "P: A ham sandwich is better than nothing\n",
      "Q: nothing is better than eternal happiness\n",
      "R: a ham sandwich is better than eternal happiness\n",
      "__________________________________________________________________________________________________________ \n",
      "If I were a carpenter and you were a lady, would you marry me anyway? and would you have my baby \n",
      "Logic: ((P ⋀ Q) ⇒ (R ⋀ S))\n",
      "P: I were a carpenter\n",
      "Q: you were a lady\n",
      "R: would you marry me anyway?\n",
      "S: would you have my baby\n",
      "__________________________________________________________________________________________________________ \n",
      "Either Danny didn't come to the party or Virgil didn't come to the party \n",
      "Logic: (～P ⋁ ～Q)\n",
      "P: Danny did come to the party\n",
      "Q: Virgil did come to the party\n",
      "__________________________________________________________________________________________________________ \n",
      "Either Wotan will triumph and Valhalla will be saved or else he won't and Alberic will have the final word \n",
      "Logic: ((P ⋀ Q) ⋁ (～R ⋀ S))\n",
      "P: Wotan will triumph\n",
      "Q: Valhalla will be saved\n",
      "R: he will\n",
      "S: Alberic will have the final word\n",
      "__________________________________________________________________________________________________________ \n",
      "Sieglinde will survive, and either her son will gain the Ring and Wotan's plan will be fulfilled or else\n",
      "Valhalla will be destroyed \n",
      "Logic: (P ⋀ ((Q ⋀ R) ⋁ S))\n",
      "P: Sieglinde will survive\n",
      "Q: her son will gain the Ring\n",
      "R: Wotan's plan will be fulfilled\n",
      "S: Valhalla will be destroyed\n",
      "__________________________________________________________________________________________________________ \n",
      "Wotan will intervene and cause Siegmund's death unless either Fricka relents or Brunnhilde has her way \n",
      "Logic: (～(R ⋁ S) ⇒ (P ⋀ Q))\n",
      "P: Wotan will intervene\n",
      "Q: cause Siegmund's death\n",
      "R: Fricka relents\n",
      "S: Brunnhilde has her way\n",
      "__________________________________________________________________________________________________________ \n",
      "Figaro and Susanna will wed provided that either Antonio or Figaro pays and Bartolo is satisfied or else\n",
      "Marcellina's contract is voided and the Countess does not act rashly \n",
      "Logic: ((((P ⋁ Q) ⋀ R) ⋁ (S ⋀ ～T)) ⇒ (U ⋀ V))\n",
      "P: Antonio\n",
      "Q: Figaro pays\n",
      "R: Bartolo is satisfied\n",
      "S: Marcellina's contract is voided\n",
      "T: the Countess does act rashly\n",
      "U: Figaro\n",
      "V: Susanna will wed\n",
      "__________________________________________________________________________________________________________ \n",
      "If the Kaiser neither prevents Bismarck from resigning nor supports the Liberals, then the military will\n",
      "be in control and either Moltke's plan will be executed or else the people will revolt and the Reich will\n",
      "not survive \n",
      "Logic: ((～PQ ⋀ ～PR) ⇒ (S ⋀ (T ⋁ (U ⋀ ～V))))\n",
      "P: the Kaiser\n",
      "Q: prevents Bismarck from resigning\n",
      "R: supports the Liberals\n",
      "S: the military will be in control\n",
      "T: Moltke's plan will be executed\n",
      "U: the people will revolt\n",
      "V: the Reich will survive\n"
     ]
    }
   ],
   "source": [
    "sentences = '''\n",
    "Polkadots and Moonbeams.\n",
    "If you liked it then you shoulda put a ring on it.\n",
    "If you build it, he will come.\n",
    "It don't mean a thing, if it ain't got that swing.\n",
    "If loving you is wrong, I don't want to be right.\n",
    "Should I stay or should I go.\n",
    "I shouldn't go and I shouldn't not go.\n",
    "If I fell in love with you,\n",
    "  would you promise to be true\n",
    "  and help me understand.\n",
    "I could while away the hours\n",
    "  conferrin' with the flowers,\n",
    "  consulting with the rain\n",
    "  and my head I'd be a scratchin'\n",
    "  while my thoughts are busy hatchin'\n",
    "  if I only had a brain.\n",
    "There's a federal tax, and a state tax, and a city tax, and a street tax, and a sewer tax.\n",
    "A ham sandwich is better than nothing \n",
    "  and nothing is better than eternal happiness\n",
    "  therefore a ham sandwich is better than eternal happiness.\n",
    "If I were a carpenter\n",
    "  and you were a lady,\n",
    "  would you marry me anyway?\n",
    "  and would you have my baby.\n",
    "Either Danny didn't come to the party or Virgil didn't come to the party.\n",
    "Either Wotan will triumph and Valhalla will be saved or else he won't and Alberic will have the final word.\n",
    "Sieglinde will survive, and either her son will gain the Ring and Wotan’s plan will be fulfilled \n",
    "  or else Valhalla will be destroyed.\n",
    "Wotan will intervene and cause Siegmund's death unless either Fricka relents or Brunnhilde has her way.\n",
    "Figaro and Susanna will wed provided that either Antonio or Figaro pays and Bartolo is satisfied \n",
    "  or else Marcellina’s contract is voided and the Countess does not act rashly.\n",
    "If the Kaiser neither prevents Bismarck from resigning nor supports the Liberals, \n",
    "  then the military will be in control and either Moltke's plan will be executed \n",
    "  or else the people will revolt and the Reich will not survive'''.split('.')\n",
    "\n",
    "import textwrap\n",
    "\n",
    "def logic(sentences, width=106): \n",
    "    \"Match the rules against each sentence in text, and print each result.\"\n",
    "    for s in map(clean, sentences):\n",
    "        logic, defs = match_rules(s, rules, {})\n",
    "        print(width*'_', '\\n' + textwrap.fill(s, width), '\\nLogic:', logic)\n",
    "        for P in sorted(defs):\n",
    "            print('{}: {}'.format(P, defs[P]))\n",
    "\n",
    "logic(sentences)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That looks pretty good! But far from perfect.  Here are some errors:\n",
    "\n",
    "* `Should I stay` *etc.*:<br>questions are not poropositional statements.\n",
    "\n",
    "* `If I were a carpenter`:<br>doesn't handle modal logic.\n",
    "\n",
    "* `nothing is better`:<br>doesn't handle quantifiers.\n",
    "\n",
    "* `Either Wotan will triumph and Valhalla will be saved or else he won't`:<br>gets `'he will'` as one of the propositions, but better would be if that refered back to `'Wotan will triumph'`.\n",
    "\n",
    "* `Wotan will intervene and cause Siegmund's death`:<br>gets `\"cause Siegmund's death\"` as a proposition, but better would be `\"Wotan will cause Siegmund's death\"`.\n",
    "\n",
    "* `Figaro and Susanna will wed`:<br>gets `\"Figaro\"` and `\"Susanna will wed\"` as two separate propositions; this should really be one proposition. \n",
    "\n",
    "* `\"either Antonio or Figaro pays\"`:<br>gets `\"Antonio\"` as a proposition, but it should be `\"Antonio pays\"`.\n",
    "\n",
    "* `If the Kaiser neither prevents`:<br>uses the somewhat bogus propositions `PQ` and `PR`. This should be done in a cleaner way. The problem is the same as the previous problem with Antonio: I don't have a good way to attach the subject of a verb phrase to the multiple parts of the verb/object, when there are multiple parts.\n",
    "\n",
    "\n",
    "\n",
    "I'm sure more test sentences would reveal many more types of errors.\n",
    "\n",
    "There's also [a version](proplogic.py) of this program that is in Python 2 and uses only ASCII characters; if you have a Mac or Linux system you can download this as [`proplogic.py`](proplogic.py) and run it with the command `python proplogic.py`. Or you can run it [online](https://www.pythonanywhere.com/user/pnorvig/files/home/pnorvig/proplogic.py?edit)."
   ]
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
