The Purple Language

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.

Defining a Context Free Grammar

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:

  1. The Terminals: { . ? what is plus minus times divided by the factorial of square root to power display first number numbers odd even natural fibonacci prime triangular zero one two three four five six seven eight nine second third fourth fifth sixth seventh eighth nineth}
  2. The Nonterminals: {sentence command question operation sequence digit ordinal nononenumber}
  3. The Productions:
    1. sentence -> command .
    2. sentence -> question ?
    3. question -> what is operation
    4. operation -> digit plus digit
    5. operation -> digit minus digit
    6. operation -> digit times digit
    7. operation -> digit divided by digit
    8. operation -> the factorial of digit
    9. operation -> the square root of digit
    10. operation -> digit to the ordinal power
    11. command -> display the first sequence number
    12. command -> display the first nononenumber sequence numbers
    13. sequence -> odd
    14. sequence -> even
    15. sequence -> natural
    16. sequence -> fibonacci
    17. sequence -> prime
    18. sequence -> triangular
    19. digit -> zero
    20. digit -> one
    21. digit -> two
    22. digit -> three
    23. digit -> four
    24. digit -> five
    25. digit -> six
    26. digit -> seven
    27. digit -> eight
    28. digit -> nine
    29. ordinal -> first
    30. ordinal -> second
    31. ordinal -> third
    32. ordinal -> fourth
    33. ordinal -> fifth
    34. ordinal -> sixth
    35. ordinal -> seventh
    36. ordinal -> eighth
    37. ordinal -> nineth
    38. nononenumber -> two
    39. nononenumber -> three
    40. nononenumber -> four
    41. nononenumber -> five
    42. nononenumber -> six
    43. nononenumber -> seven
    44. nononenumber -> eight
    45. onononenumber -> nine
  4. The Start Symbol: sentence

Some Transformations

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 .

Recongizing and Interpreting the Language

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.