{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Meaning of Sentences, NLU and Logic \n", "\n", "*These notes will be heavily based on this [book](https://www.nltk.org/book/).*" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from IPython.core.interactiveshell import InteractiveShell\n", "InteractiveShell.ast_node_interactivity = \"all\" # see the value of multiple statements at once." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is not difficult to build a grammar formalisms for translation restricted to some specific tasks. In [book](https://www.nltk.org/book/), it is shown that a simple feature-based grammar formalism can translate English questions into SQL queries.\n", "\n", "In this notebook, it will be shown that using logic formalisms one can find more generic translation mechanisms. From [book](https://www.nltk.org/book/)\n", "\n", "> One advantage of logical formalisms is that they are more abstract and therefore more generic. If we wanted to, once we had our translation into logic, we could then translate it into various other special-purpose languages. In fact, most serious attempts to query databases via natural language have used this methodology." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Propositional Logic\n", "\n", "> Propositional [...] logic deals with propositions and argument flow. Compound propositions are formed by connecting propositions by logical connectives. \n", "\n", "Logical connectives include e.g. the symbols for not, and and or found [here](https://github.com/marcotav/natural-language-processing/blob/master/alphabet-human-thought/meaning-NLU-logic/images/logic.png).\n", "\n", "Consider the [example](https://www.nltk.org/book/):\n", "\n", "\t[Klaus chased Evi] and [Evi ran away]\n", " \n", "Representing the sentences by **propositional letters** $\\phi$ and $\\psi$ and using the connective **&** as logical operator this sentence acquires the structure $\\phi\\, \\&\\,\\psi$, its **logical form**. \n", "\n", "The convention here will be:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "negation \t-\n", "conjunction \t&\n", "disjunction \t|\n", "implication \t->\n", "equivalence \t<->\n" ] } ], "source": [ "import nltk\n", "nltk.boolean_ops()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Propositional letters are **well-formed formulas** which from now on we will call **formulas**. Therefore is $\\phi$ and $\\psi$ are formulas any combination using connectives are also formulas. There are several **truth-conditions**involving formulas such as ~$\\phi$ is true in s iff $\\phi$ is false in s, a so on. \n", "\n", "Applying the method `.parse` from `nltk.LogicParser()` into e.g. the truth condition -(P & Q) we obtain:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "nltk.sem.logic.NegatedExpression" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lp = nltk.sem.logic.LogicParser()\n", "lp.parse('-(P & Q)')\n", "type(lp.parse('-(P & Q)'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Inference\n", "\n", "The inference:\n", "\n", " Mark is taller than John.\n", " Therefore, John is not taller than Mark.\n", " \n", "Is correctly represented as:\n", "\n", " [MtJ,MtJ -> - JtM] / - JtM\n", " \n", "as we must include the a statement clarifying the implication of the expression 'is taller'.\n", "\n", "We can test argument validity using `nltk`:\n", "\n", " from nltk import Prover9\n", " lp = nltk.sem.logic.LogicParser()\n", " MtJ = lp.parse('MtJ')\n", " NotJtM = lp.parse('-JtM')\n", " R = lp.parse('MtJ -> -JtM')\n", " prover = nltk.sem.logic.Prover9()\n", " prover.prove(NotJtM, [MtJ, R])\n", " \n", "The output is `True`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### First-Order Logic\n", "\n", "> First-order logic uses quantified variables over non-logical objects and allows the use of sentences that contain variables, so that rather than propositions such as Socrates is a man one can have expressions in the form \"there exists X such that X is Socrates and X is a man\" and there exists is a quantifier while X is a variable.This distinguishes it from propositional logic, which does not use quantifiers or relations.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }