[Language Processing and Python] 10.3 Level 1 Logic

Source: Internet
Author: User
Tags nltk

In this section, the natural language expressions are translated into one-level logic to indicate their meanings.

Not all natural language semantics can be expressed by the first-order logic.

Syntax

The first-order logic retains the boolean operators of all propositional logic, but it adds some important new mechanisms.

1. the proposition is analyzed as a predicate and a parameter.

Standard construction rules of level 1 logic recognize the following terms: independent variables, independent constants, predicates with different numbers of parameters

For example:

Angus walks can be converted to walk (angus), and Angus sees Bertie can be converted to see (angus, bertie ). We call walk as a mona1 predicate, and see as a binary predicate.

An atomic predicate such as see (angus, bertie) is true or false in case of s. It is not a logical problem, but dependent on a specific valuation. This value is the value selected for the constant see, angus, and bertie. Therefore, these expressions are non-logical constants.

Comparison: Logical constants (such as boolean operators) are always interpreted in each model of the first-order logic.

2. Check the syntax structure of the first-order logical expression. Generally, the type is specified for the expression.

Basic Type: e is the entity type, t is the formula type (type of expression with true values)

These two types can form complex types of function expressions.

For example, <e, T> is the type of the expression from the entity to the true value, that is, the one-element predicate.

You can call LogicParser to check the type:

>>>tlp = nltk.LogicParser(type_check=>>>parsed = tlp.parse(>>><ConstantExpression angus>>>>>>><ConstantExpression walk>>>><e,?>

Note that the walk type is not recognized, because its type is unknown, it is possible that another type will appear in this context, such as: <e, e> or <e, <e, t> we need to develop a signal to solve this problem:

>>>sig = {: >>>parsed = tlp.parse(>>><e,t>

3. In the first-order logic, the predicate parameters can also be independent variables, such as x, y, and z.All e-type variables are in lower case. Independent variables are similar to personal pronouns, such as he, she, And it. In order to understand their meaning, we need to know the context they use.

For example:

(1415

He is the same as Cyril.

Likewise, there are also relations with different public relations. For example, 16a is unknown in the following sentence.

(16

 

Let's look at the example below:

(17) a. He  a dog &disappear(x)

AOpen formula (17b).

SpecifyExistence quantizer x("Some x exist"). We can bind these variables.

18a. ∃x.(dog(x)& a dog 

Below is the representation of 18a in NLTK:

(19) exists x.(dog(x) &disappear(x))

In addition to quantifiers, ∀ X ("for all x"), as shown in (20.

(20 it 

In NLTK, this indicates:

(21) all x.(dog(x) -> disappear(x))

In general, variable x appears freely in formula Phi if it does not appear in the range of allx or existsx.

Conversely, if x is restricted in formula Phi, it is within the range of allx. PHI and exists x. Phi. If all the variables in the formula are restricted, we say this formula is closed.

The parse () method of LogicParser in NLTK returns the object of the Expression class. Each example expr has a free () method, which returns a set of free variables in expr.

For example:

>>>lp =>>>lp.parse(>>>lp.parse(>>>lp.parse(>>>lp.parse(>>>lp.parse(>>>lp.parse()])

First-order theorem proof

The formula we want to prove is to prove the goal.

You can use Prover9 to demonstrate the proof:

>>>NotFnS= lp.parse(>>>SnF= lp.parse(>>>R= lp.parse(>>>prover=>>>
>>>FnS= lp.parse(>>>

Summary of level 1 logical language

We will adopt the Convention: <en, t> is the type of a predicate that consists of n parameters of the Type e to generate an expression of the type t. In this case, n is the number of predicates.

,])

>>>v= >>>val =>>>:: set([(: : set([(: set([(: : set([(, ), (, ), (, : set([(,), (,)])}

One form is P (T1 ,... tn), where P is n yuan, the true condition corresponds to (T1 ,... tn.

>>>(, )  val[>>>(,)  val[

Independent Variables and assignments

In our model, the use of context corresponds to assigning values to variables. This is a ing from an independent variable to an object in the domain.

Assign values using the constructor Assignment, which also takes the domain of the discussed model as the parameter.

>>>g= nltk.Assignment(dom, [(, ), (, >>>: , : }

You can also view the assignment values:

>>>/y][o/x]

The following shows how to evaluate an atomic formula of a level logic.

Create a model and call the evaluate () method to calculate the true value.

>>>m=>>>m.evaluate(

When the interpreted function encounters the variable y, it does not check the value in val, but queries the value of this variable in g of the variable assignment.

If the final value is true, we will say that the value g meets the formula see (olive, y ).

The purge () function helps us clearly BIND:

>>>>>>

The general process of determining true or false formulas in a model isModel check.

Quantization

exists x.(girl(x) &walk(x))
(25) girl(x) &>>>m.evaluate(

NLTK provides a useful tool: the satisfiers () method. It returns a set of all individuals that meet the open formula. The parameters of this method are an analysis formula, a variable, and a value assignment. The following are examples:

>>>fmla1 = lp.parse(>>>m.satisfiers(fmla1,, >>>fmla2 = lp.parse(>>>m.satisfiers(fmla2,, , >>>fmla3 = lp.parse(>>>m.satisfiers(fmla3,, ])

Why is fmla2 such a value? Note that the true-Value Condition of-> means that fmla2 is equivalent to-girl (x) | walk (x ).

Quantifiers range Ambiguity

When we give a sentence a formal expression of two quantifiers:

(26) Everybody admires someone.
a. all x.(person(x)-> exists y.(person(y)&&all x.(person(x)-> admire(x,y)))

We can use both of them, but the two meanings are different: 27b is logically stronger than 27a.

We use the quantitative scope of terms to distinguish two Logics:

The range of dimensions is wider than that of dimensions, and the range order in (27b) is reversed. So now we have two ways to represent (26), which are reasonable. In other words, we call (26) the range of quantifiers is ambiguous, and the formula (27) gives us a way to clarify the two reading methods. However, we are not only interested in two different representations associated with (26); we also want to show details about how the two expressions in the model lead to different true value conditions.

To check ambiguity more carefully, let's make the following amendments to the valuation:

>>>v2= >>>val2 = nltk.parse_valuation(v2)

>>> M2 >>>> g2 >>> fmla4 = lp. parse (>>> m2.satisfiers (fmla4, >>> fmla5 = lp. parse (>>> m2.satisfiers (fmla5, >>> fmla6 = lp. parse (>>> m2.satisfiers (fmla6,])

Model Creation

The creation of a model is a set of given sentences and attempts to create a new model. If it succeeds, we will know that the set is consistent because we have a model as evidence.

Mace4 (Mace4 searches for finite models and counterexamples .)

>>>a3 = lp.parse(>>>c1 = lp.parse(>>>c2 = lp.parse(>>>mb=nltk.Mace(5>>>>>>>>>

Mace4 will also look for an inverse example for us:

For example, the linked list:

[There  a woman that every man loves, Adamis a man,Eveis a woman]

Our conclusion is:

Adam loves Eve.

Can Mace4 find a false model that makes assumptions true? In the following code, we use MaceCommand () to check the created model.

>>>a4 = lp.parse(>>>a5 = lp.parse(>>>a6 = lp.parse(>>>g= lp.parse(>>>mc= nltk.MaceCommand(g, assumptions=>>>

Mace4 found a countersample model where Adam loves a woman rather than Eve.

>>>:: : : set([(, : set([(: set([(,), (,)])}

Furthermore, we do not specify a set where man and woman represent non-intersecting sets. Therefore, model generators overlap each other.

Therefore, let's add a new hypothesis so that man and woman do not intersect. The model generator still produces an inverse model, but this time it is more intuitive:

>>>a7 = lp.parse(>>>g= lp.parse(>>>mc= nltk.MaceCommand(g, assumptions=>>>>>>:: : : set([(, : set([(: set([(,), (,)])}

In our hypothesis, Eve is not the only female in the universe, so the anti-sample model is acceptable. To exclude this possibility, we will have to add further assumptions, such as exists y. allx. (woman (x)-> (x = y) to ensure that there is only one female in the model.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.