Indian Institute of Information Technology, Allahabad (U.P.), India ... beginning and expert programmers in the C ... natural programming environment that sup-.
COFFEE: COmpiler Framework For Executables in English Aasish Pappu,Tejaswi Tenneti, Umashanker Tiwary Indian Institute of Information Technology, Allahabad (U.P.), India { akpappu b03, ttejaswi b03, ust}@iiita.ac.in
Abstract We are presenting our work on the system called COFFEE whose goal is to translate English sentences into Java programming language. Sentences such as “Add these two numbers” and “Find the average of numbers in the array A” could be used to generate functional code blocks that can later be compiled into native code. COFFEE uses the dependency graph of a given natural language sentence and uses a set of rules to transform this information into programming code. It also supports advanced compiler functions such as typecasting. Keywords: Natural Language Programming, Programming Framework
1
Introduction
Since the advent of programming languages incessant progress has been made in improvising languages in the developer’s perspective. Scrupulous code with unambiguous logic being the prime motive, newer paradigms have been proposed in the course of time. Object oriented and aspect oriented are few of the popular paradigms of the last century. Object oriented languages have definitely outshadowed their contemporaries and its predecessors. Inspite of wide usage of these languages, the notion of unambiguous implementation of the logic is partially fulfilled. The designs of new programming languages have generally not taken advantage of what has been discovered. For example, the new Java and JavaScript languages use the same mechanisms for looping, conditionals and assignments that have been shown to cause many errors for both beginning and expert programmers in the C language. there are many significant “holes” in the knowledge about how people reason about programs and programming. Thus, a demand for next generation programming language is
at the corner. Taking novice programmers into consideration expressing logic in any natural language is the easiest way. Even a nonprogrammer would wish to use such a “natural” programming language. This provides flexibility and also gains large user base because of limited-learning skills required to program in a natural language. In fact, all computer users would benefit from the ability to automate their repetitive tasks and customize their applications through scripting. COFFEE is an attempt to accomplish above mentioned objectives. COFFEE provides a natural programming environment that supports translation of simple text into programming constructs. For a given user input COFFEE extracts key phrases and key words, also observes the parse tree and dependency graph generated by Stanford parser. Based on the above entities(keywords, key phrases, parse and dependency graph) pre-defined templates(more about templates in later sections) are filled, thereafter java source code is generated. Organization of the Paper Initially, we discuss about the state of art, along with brief analysis of each work. Further, a little overview about the challenges and aspects of our system. We introduce the architecture of COFFEE and commentary thereon. Later, we present analysis of the results of our system and throw some light over our contribution to this research area. Finally, we conclude with some of drawbacks of our system and future scope.
2
Related Work
We have observed promising works in this realm. Programming in natural language: “NLC” as a prototype is a project undertaken by Ballard et al. This prototype enables a computer user to type English commands into a display terminal and watch them executed on example data shown on the screen. This pro-
totype deals with specific domain of languages such as ‘matrices’ etc. NaturalJava is a natural language interface for programming in Java by David Price et al. NaturalJava is a prototype for an intelligent natural-language based user interface for creating, modifying, and examining Java programs. There is no assistance to the programmer to make decisions in aspects like what data structure to use, what variables to use (eg. a floating point number or an integer). NPGS is an automatic programming project going on at Yale University. The goal of this project was to develop a system that would generate a GPSS simulation program after carrying on an English dialogue with a user about a simple queuing problem. The system is using basically some form of conceptual graph representation for their knowledge base and some form of procedural specification for their natural language processing. The natural language processing to be done by these systems requires techniques that are more advanced than those currently available. Another major feature is that an automatic programming system with the user and the computer engaged in a dialogue with the mutual goal of finding a match between the users requirements and what the system can provide. M’PAL is a programming language project based on English definitions. Rosebloom et al. developed a programming language that takes imperative sentences as input along with collection of definitions referred in Webster’s dictionary. Then compiler would supply the control flow needed to unify the definitions into a program. Kodama et al. worked on Natural Programming environment that enables a user to describe, modify or create multimedia content in Japanese. In this environment, Japanese description is provided to alter the layout of multimedia contents. Also an association between two multimedia contents can be established and parsed by machine translation translated into an executable program, and executed. At IBM Research, under Behavioral Science Group Miller et al. conducted a project to analyze natural language programs to discover the means by which process information is communicated. The ultimate goal was to find out most reliable and power communication channels that might be incorporated in natural lan-
guage interfaces. Lee et al. presented a case-based approach for natural language interface to a UNIX system with automatic programming ability. Natural language commands are analyzed by case-based parsing and then transformed into UNIX shell-scripts by case-based planning with derivational analogy.
3
Challenges
Before we discuss about the current system, we shall look over the prominent issues that must be dealt with a full-fledged natural programming environment: 1. Provision of proper feedback to the user regarding the progress of translation from natural language to programming language. 2. The system should be able to handle all the paraphrased forms of a given input sentence. 3. Conceiving an intermediate framework between natural language and programming syntax.
4
Aspects of COFFEE
The following aspects of a programming language which can be expressed in Natural English are covered in our system. 1. Imperative Sentences(e.g. “Select third positive element from the array”). 2. Mathematical Operations ( e.g. “Add a to b”). Here addition of ‘a’ and ‘b’ is performed. 3. Loops and Conditions (e.g. “List the names of everyone”). ‘List’ or ‘Everyone’ signifies iteration or looping.
5
Action Plan for COFFEE
To deal with each of the aspects objectively, we have categorized them into the following ‘Cases’. The basis for such categorization is the detailed study of how people think about programming syntax in common terms i.e. natural language. The cases are discussed as following :
5.1 Declaration Initializing a variable or data structure to a certain value as specified. Introducing new variables wherever necessary. 5.2 Selection Selecting a variable or data structure for a certain operation on it. 5.3 Mathematical Operations Selected variable or data structure arithmetic and boolean operations. 5.4 Production clauses They usually occur incase of conditional statements. If (condition is true) Then ( this is true) When (condition is reached) Do (this)
5.5 Datatypes (e.g. “Intialise g with 9.8”). The value 9.8 signify that g holds floating point values. 5.6 Control Structures This includes iterations or looping constructs.
6
Architecture of COFFEE
The architecture of COFFEE can be seen in the figure
• Dependency Graph Analyzer • Translation Module – Variable Tracker – Feedback Mechanism using a Dialogue System • Data Dictionary • English Parser 6.1
Dependency Graph Analyzer
6.1.1 Typed Dependency Graph Typed dependencies and phrase structures are different ways of representing the structure of sentences, whereas a phrase structure parse represents nesting of multi-word constituents, a dependency parse represents dependencies between individual words. A typed dependency parse additionally labels dependencies with grammatical relations, such as subject or indirect object. Some of the common grammatical relations in Noun Phrases are amod - adjective modifier rcmod - relative clause modifier det - determiner partmod - participial modifier infmod - infinitival modifier prep - prepositional modifier dep - dependency dobj - direct object
6.1.2 Dependency Graph Analysis In order to translate a statement which falls under the class of Imperative Operations we need to semantically analyze the statement and extract certain attributes from it. A general form of such a statement is .. .. ....
For e.g: In the statement: “Select all the elements which are greater than two from the array A.” • Imperative: Select • Indication of number of elements to be selected: all Figure 1: Architecture of COFFEE
• Condition: which are greater than two • from: array
• Name of the variable from which we need to select the required element(s): array named ‘A’ We contend that the translation of the statements under the Imperative operation “Select” can be considered a sub-problem of the problem of translating statements under operations like “Negate”, “Add”. Since, any operation that requires listing of elements should consist “Select” function for extracting the required elements. Hence, a “Select” operation is performed by default for every operation. The attributes required for the translation of a statement under the “Select” operation, and the extraction of these attributes is explained below. 6.1.3 Attribute 1 What do we need to select (i.e. element, number)? This attribute can known by generating the Parsed dependency graph of the statement, which gives a grammatical dependencies view of sentence structure. In the nodes of the Parsed dependency graph, if the node ‘Select’ is related to any word in the statement with the relation dobj() or dep(), then the value of the related node can be identified as the attribute required. e.g.: dobj(Select − 1, elements − 3) indicates that we need to select ‘elements’, which is our required attribute. This attribute is very important since this particular attribute is helpful in determining the rest of attributes. 6.1.4 Attribute 2 Is the number of elements to be selected greater than one? This attribute is useful for two reasons 1. Determines how much we need to scan a data-structure (like an array), in order to select the required element(s), e.g.: if a single element needs to be selected from an array A, then we can stop on first find. 2. The information about the number of elements is required for the interactive feedback module based on a dialogue to guide
the end-user in choosing the appropriate data-structure to store the selected element(s), e.g.: If more than one element needs to be selected then the system suggests the various possible datastructures in which the end-user may store the selected elements. This attribute can be determined by generating the Part Of Speech(POS) tagged parse tree of the statement. We initially assume that the number of elements to be selected is one. Starting from the leaf node in the parse tree whose value is equal to Attribute 1 we traverse along the leaves to the left of this node and stop at the node whose parent’s tag value is equal to “DT”(Determiner) or “PDT”(Predeterminer), or ends with “S”(Noun-plural or Adverb). Such a parent node suggests that its child node indicates a sense of plurality. Such child nodes usually contain values like “all”, “every”. This indicates that the number of elements to be selected is more than one.
6.1.5
Attribute 3
Where do we select the element(s) from? This attribute determines the variable name and the data structure (e.g.: array, list etc) of the variable from which the selection needs to be performed. This can be known from the Part Of Speech tagged parse tree of the statement. Starting from the leaf node in the parse tree whose value is equal to Attribute1 we traverse along the leaves to the right of this node and stop at the node whose parent’s tag value is equal to “IN”(Preposition). Then we start from this particular child node we traverse along the leaves to the right of this node and look for two consecutive leaves whose parent’s tag value is equal to “NN”(Noun-singular). The value of the first leaf indicates the data-structure of the variable and the value of the second leaf indicates the name of the variable. For further information of Part Of Speech tagged parse tree refer to Penn Treebank Project, University of Pennsylvania.
6.1.6 Attribute 4 What is the condition that needs to be applied to select an element? For example, in the statement, Select all the positive elements in the Array A, the condition is “positive”. We use the POS tagged parse tree of the statement for this purpose. Starting from the leaf node in the parse tree whose value is equal to Attribute1 we traverse along the leaves to the left of this node and stop at the node whose parent’s tag value equals “JJ”(Adjective) or “NN”(Noun-singular). Then the value of that particular leaf node is taken as the condition needed to be applied. For eg: For the statement- “Select the fifth element from the Array A”, the Part of Speech parse tree is shown below. ROOT (NP (NP (NNP Select)) (NP (NP (DT the) (JJ fifth) (NN element)) (PP (IN from) (NP (DT the) (NNP Array) (NNP A)))))) where NP - NounPhrase NNP - Proper Noun
If the condition cannot be found in the above step then check if the tag value of the parent of the leaf on the immediate right of the node whose value is equal to Attribute 1 starts with a “W”(WhDeterminer(WDT), Wh-Pronoun(WP)), Possessive wh-pronoun((WP) or WhAdverb(WRB)). If true, then starting from that child node traverse along the leaves to the right of this node, and pick all the leaves until a leaf’s parent equals “IN”(Preposition). Omitting this leaf, the rest of all the leaves picked above form the condition required. 6.2 Translation Module This part of the system deals with the generation of code in a high level programming language like Java. We have considered templates for general programming constructs (e.g.: a “for” loop, an “if” condition etc). These templates look like a piece of code with blanks in between to be filled by the variables, operators and programming statements. These blanks are filled based on the required attributes extracted from a statement, as described in the previous section. Once the necessary conditions have been extracted from a statement, we
use them to translate the English statements into programming syntax by using these attributes. The translation also comprises of two important elements: (1) Variable Tracker (2) Feedback Mechanism using a Dialogue System. 6.2.1 Variable Tracker The variable tracker keeps track of all the variables and their data-types. Any new variable which is declared explicitly by the user, or needs to be implicitly declared in the program (e.g.: The set of variables selected using the ‘Select’ operation need to be stored in a new variable of the type Array). 6.2.2
Feedback Mechanism using a Dialogue System The interactive feedback mechanism pops up dialogues to the assist the user wherever required. Its main tasks include asking the user for the variable name and the data-type whenever a new variable needs to be implicitly declared, and to warn the user if he tries to overwrite an existing variable or wrongly type-casts a variable. Translation algorithm developed by us is presented below. It describes how a sentence which requires the imperative operation “Select” to be performed on a given array element. For e.g.: A sentence like “Select the fifth element of the array A”. can be translated into float a = A[5]; if the user tells the interactive feedback system to store the selected element in the variable ‘A’ which is of the data-type ‘float’. The notations used in the algorithm of code translation are: • I = the operation which needs to be performed. • N = number of elements on which the imperative operation is to be performed. • C = the condition attribute (Attribute 4) extracted from the sentence. The condition C in the sentence can either indicate a cardinal position (e.g.: In “Select the fifth element in the array A” the condition attribute will be “fifth”), or can be a relational operator like greater than, or a condition like Positive, Negative etc. We maintain a database of the list of cardinals in English words and their corresponding decimal form.
For e.g. an entry into such a database will be one, f irst ≡ 1. A similar database is maintained for the list of relational operators in English words and their corresponding mathematical notation. For e.g. an entry into such a database will be greaterthan ≡ >. The template that needs to be used for translating a sentence is invoked based on I, N and C attributes of the sentence. The translation algorithm of COFFEE is given below:Algorithm 1 Algorithm of Code Translation
if(A[temp]>2)
After this template is invoked and filled with required attributes the translation algorithm proceeds similarly as described above. 6.3
Data Dictionary
The main purpose of data dictionary is to support code translation module of the system. It contains few important mathematical terms that need actual meaning of them, like phrases greater than, less than, positive, negative, average, sum, add, ordinality of numbers and other mathematical terms. Although, this dictionary is not exhaustive it is possible to incorporate any new term(specially mathematical) into it through user dialogue system. This dictionary can be made domain specific, so that frequently occuring terms can be incorporated into the dictionary.
1: if N == 1 and C ∈ CardinalN umber then 2: String To Decimal(C) 3: Prompt User(Select single element and enter name and datatype) 4: while the variable name already exists do 5: Warn User(Variable Exists and ready to overwrite it) 6: if User does not wish to overwrite then 7: Prompt User(Enter new variable name and datatype) 7 Results and Contribution 8: end while 9: else We have successfully implemented the class of 10: if Typecast done correctly and safely then operations like Declaration, Select and Impera11: Print to StdOut(vt.name=st.from name[cardinal]) 12: end if tives (mentioned above). The Imperative class 13: else of operations has been implemented to cover a 14: Warn User(Incorrect type-cast) variety of operations like Negate< element >, 15: Prompt User(Enter correct data-type) 16: end if Multiply< elements > etc. Also the Select 17: else class of operations can successfully generate 18: Print to StdOut(vt.datatype,vt.name=st.from name[cardinal]) Java code to select the required number of el19: end if
The cardinal (i.e. the condition attribute) in “Select the fifth element of the array A.”, is ‘fifth’ which can be translated to numeral ‘5’ using the list of cardinals database. The templates that could be used were given in lines 10 and 18 of the algorithm. Similarly, the templates for performing the select on more than one element from an Array consists of a ‘for’ loop for traversing through the array, and an ‘if’ condition to select the elements of the array based on the condition specified by the user. E.g. in the statement “Select all the elements which are greater than two from the array A”, the conditional attribute greater than two is translated into > 2 using the cardinal and relations lists in the database. Also, the number of elements to be selected (Attribute 2) is greater than one. Hence, the template consists of the ‘for’ loop which is used to traverse the array i.e. for(int temp=0;temp