If we are to create an interface that is more organic in its communication with the user, we must define a language for our crypto solver. This is where the Purple Language comes in. It is subject to the constraints that all numbers that appear in the language are single digit positive integers, only the operators and sequences mentioned will appear in the language, and the articulations will conform to the same form as the following examples:
what is three plus two ?
what is five divided by zero ?
what is the factorial of five ?
what is the square root of eight ?
what is six to the seventh power ?
display the first seven odd numbers .
display the first five natural numbers .
display the first two even numbers .
display the first eight Fibonacci numbers .
display the first five triangular numbers .
The ideas proposed by this language may help us in creating a natural language interface for the crypto solver, so lets define it in more detail.
In order to actually define the purple language, we must define a context free grammar (CFG) for it, which requires the 4 components to be defined:
Using the CFG that weve defined, we can now derive some sentences in the purple language:
sentence
sentence 2=> question ?
question ? 3=> what is operation ?
what is operation ? 4=> what is digit plus digit ?
what is digit plus digit ? 21=> what is two plus digit ?
what is two plus digit ? 21=> what is two plus two?
sentence
sentence 2=> question ?
question ? 3=> what is operation ?
what is operation ? 7=> what is digit divided by digit ?
what is digit divided by digit ? 25=> what is six divided by digit ?
what is six divided by digit ? 21=> what is six divided by two ?
sentence
sentence 2=> question ?
question ? 3=> what is operation ?
what is operation ? 10=> what is digit to the ordinal power ?
what is digit to the ordinal power ? 26=> what is seven to the ordinal power ?
what is seven to the ordinal power ? 33=> what is seven to the fifth power ?
sentence
sentence 1=> command .
command . 12=> display the first nononenumber sequence numbers .
display the first nononenumber sequence numbers . 44=> display the first eight sequence numbers .
display the first eight sequence numbers . 13=> display the first eight odd numbers .
sentence
sentence 1=> command .
command . 11=> display the first sequence number .
display the first sequence number . 16=> display the first fibonacci number .
sentence
sentence 1=> command .
command . 12=> display the first nononenumber sequence numbers .
display the first nononenumber sequence numbers . 45=> display the first nine sequence numbers .
display the first nine sequence numbers . 14=> display the first nine even numbers .
With a more robust definition, and an understanding for how to derive sentences, its time to think about how we might be able to incorporate this type of logic with our Crypto program. Luckily prolog provides us with a quick and efficient way to transform our CFG into something that can be used to recognize the language, which is called a definite clause grammar (DFG). We can transform a CFG to a DFG by changing the '->' to '-->', wrapping the terminals in [] brackets, and separating symbols of the rules of a production with commas, and end the sentence with a period. When this is done, each of the grammar symbols is translated by the prolog reader to a parser. You can take a look at a recognizer in prolog here, and a demo for it here. As for interpreting it, it follows a similar logic, with a bit more semantic support. You can take a look at the interpreter code in prolog here, and a demo for it here.
Now, let's try applying what we've learned here to crypto problems, so we can implement a natural language interface for the problems, just click here.