ccg2lambda: A Compositional Semantics System

We demonstrate a simple and easy-to-use system to produce logical semantic representations of sentences. Our software operates by composing semantic formulas bottom-up given a CCG parse tree. It uses ﬂexible semantic templates to specify semantic patterns. Templates for English and Japanese accompany our software, and they are easy to understand, use and extend to cover other linguistic phenomena or languages. We also provide scripts to use our semantic representations in a textual entailment task, and a visualization tool to display semantically augmented CCG trees in HTML.


Introduction
We are motivated by NLP problems that benefit from any degree of computer language understanding or semantic parsing. Two prominent examples are Textual Entailment and Question-Answering, where the most successful approaches (Abzianidze, 2015; Berant et al., 2013) require symbolic representations of the semantics of sentences. We are inspired by the theoretical developments in the formal semantics literature, where higher-order logical (HOL) formulas are used to derive meaning representations (MR); despite what is typically believed in the NLP community, Mineshima et al. (2015) demonstrated that HOL can be used effectively at a reasonable speed.
In this paper, we describe ccg2lambda, our system to obtain MRs given derivations (trees) of a Combinatory Categorial Grammar (CCG) (Steedman, 2000). In order to obtain the MRs, our system is guided by the combinatory characteristics of CCG derivations and a list of manually designed semantic templates. The linguistic intu-itions behind the design of those semantic templates and the evaluation of the MRs that they produce is detailed in Mineshima et al. (2015), and is not repeated here. In that paper, we tackled a Textual Entailment task, where the meanings of premises and conclusions were represented symbolically, and their entailment relation was judged with a theorem prover of higher-order logics. With this system, we obtained a state-of-the-art performance on the FraCaS dataset (Cooper et al., 1994).
ccg2lambda and the accompanying semantic templates are open source 1 . Semantic templates are already available for English and Japanese, and they are easily extensible to other linguistic phenomena and other languages for which CCG parsers are available. Here we describe how to use ccg2lambda and how to specify semantic templates for other researchers to extend our work.

Related Work
The most similar system to ours is Boxer (Bos et al., 2004), which outputs first order formulas given CCG trees. Our system can additionally produce higher-order formulas, which are more expressive and potentially accurate (Mineshima et al., 2015).
There are three prominent textbook systems for computational semantics, that of Bird et al. (2009), Blackburn andBos (2005) and van Eijck and Unger (2010). These three systems, together with the Lambda Calculator 2 (Champollion et al., 2007) are excellent educational resources that are very accessible to beginner linguists in general, and semanticists in particular. The development of ccg2lambda is inspired by these systems, in that we aimed to produce a software that is easy to understand, use and extend with only basic knowledge of formal semantics and lambda calcu-lus. However, these systems are mainly developed for educational purposes and are not connected to fully fledged parsers, hence not immediately usable as a component of larger NLP systems.
We have developed ccg2lambda to process trees that are produced by wide-coverage CCG parsers (e.g. C&C and Jigg 3 ). Other semantic parsers such as those developed by Bos et al. (2004), Abzianidze (2015) and Lewis and Steedman (2013) also connect to wide-coverage CCG parsers, but they do not emphasize easy accessibility or extensibility. NL2KR (Vo et al., 2015) is an interactive system with powerful generalization capabilities, but it does not allow fine-grained lexicon specifications (only CCG categories) and does not output machine readable semantics. Instead, ccg2lambda produces XML machine-readable MRs, which make our system easy to integrate in larger logic or statistical NLP systems.
3 System Overview Although our main system contribution is a semantic parser, we use the problem of textual entailment as an end-to-end task. Figure 1 schematically shows the several components of our system.
The first stage is to parse sentences into CCG trees (see Figure 2 for an example). Our system currently supports the C&C parser  for English, and Jigg (Noji and Miyao, 2016) for Japanese.
The second stage is the semantic composition, where MRs are constructed compositionally over CCG trees using lambda calculus, thus allowing higher-order logics if necessary. To this end, our system is guided by the compositional rules of the CCG tree and the semantic templates provided by the user. In Section 4 we describe in detail how these semantic templates are specified and how they control the semantic outputs. The output of this stage is a Stanford CoreNLP-style XML file  where each sentence has three XML nodes: <tokens>, <ccg> and <semantics>. Thus, sentence semantics can simply be read off the root node of the CCG tree.
In the case of recognizing textual entailment, the third stage is the theorem construction, definition of predicate types, and execution with a logic prover. This stage is not essential to our system, but it is added to this paper to show the usefulness of our semantic representations in an NLP task.
3 https://github.com/mynlp/jigg 4 Semantic Composition ccg2lambda receives CCG trees and outputs (possibly higher-order) logic formulas. To that end, we use i) the combinatory characteristics of CCG trees to guide the semantic compositions, and ii) a list of semantic templates to assign a precise meaning to CCG constituents.
See Figure 2 for an example of CCG derivation for the sentence "Some woman ordered tea", augmented with its semantics. Nodes have CCG syntactic categories (e.g. N or S\N P ), which is what our system receives as input. On the same figure, we have added the logical semantic representations (e.g. λx.woman(x)) below the syntactic categories. Our system outputs these logical formulas. For clarity, leaves also display the token base forms. The symbols <, > and lex stand for left and right function application rules, and the type-shift rule in C&C, respectively. These rules and the syntactic categories guide the semantic composition, provided with semantic templates that describe the specific semantics.

Semantic templates
Semantic templates are defined declaratively in a YAML 4 file, typically by a formal semanticist after an appropriate linguistic analysis. A template applies to a node of the CCG derivation tree if certain conditions are met. Each template has two required attributes: semantics and (syntactic) category. The attribute semantics is a lambda term in NLTK semantics format (Garrette and Klein, 2009). In case a template applies on a CCG leaf (that is, a word), the lambda term in the template is applied on the base form of the word, and β-reduction is performed. For example, the semantic template − semantics : \E.\x.E(x) category : N applying on a leaf whose base word is "woman" and its syntactic category is N , would produce the expression (λE.λx.E(x))(woman) which is β-reduced to λx.woman(x). Here, the base form "woman" substitutes all occurrences of the variable E in the semantics expression.
In case a template applies on a CCG inner node (a node with children), the lambda abstraction is applied on the semantics of the children, in order. Figure 1: System pipeline for recognizing textual entailment. Syntactic structures of sentences are obtained with a CCG parser. Then, we perform the semantic composition using semantic templates. The resulting meaning representations are used to perform various logical inferences with a theorem prover.
For example, in Figure 2, the template − semantics : \E.\F.∃y.(E(y) ∧ F (y)) category : NP rule : lex produces a type-raise from N to N P , and when applied to the CCG node whose child's semantics are λy.tea(y), it will produce, after β-reduction, the formula λF.∃y.(tea(y) ∧ F (y)). Here, the child's semantics λy.tea(y) substitute all occurrences of the variable E. The newly composed semantic representation λF.∃y.(tea(y) ∧ F (y)) now expects another predicate (a verb) as an argument F (i.e. "order"), which will be filled in the next step of the composition.
The category attribute of a semantic template may also specify conditions on the feature structures of CCG nodes (which are provided by the CCG parser), in which case templates apply if the syntactic category matches and the feature structure subsumes that of the CCG node. For example, if the semantic template specifies a syntactic category NP[dcl = true], it matches a CCG node with a category NP[dcl = true] or a category NP[dcl = true, adj = true].
Other conditions for matching templates to CCG nodes can be specified by adding more attributes to the semantic template. In the example above, the attribute rule : lex is used to specify the combination rule of that inner CCG node. In practice, any XML attribute of a CCG node can be used to specify matching conditions, which means that users of ccg2lambda can enrich CCG trees with arbitrary annotations such as Named Entities or Events and use them as matching conditions when defining semantic templates without modifying the software. It is also possible to specify attributes of the children of the target CCG node. These conditions are always prefixed by the string child, followed by the branch index 0 or 1. For example, a semantic template with the attribute child1 child0 pos : NN matches a node whose right child's (child1) left child's (child0) POS tag is an NN. Moreover, paths to child nodes can be left unspecified, by using the keyword child any X : Y; in this case, any child whose attribute X has value Y will be matched by the template. If more than one template matches a CCG node, the first appearing template is selected.

System Usage and Output
The command for the semantic composition is: # python semparse.py ccgtrees.xml templates.yaml semantics.xml where ccgtrees.xml is a Jigg's XML style CCG tree, templates.yaml contains the semantic templates, and semantics.xml is the XML output of the system. We also provide a script to convert C&C XML trees into Jigg's XML style. The output of semparse.py follows the conventions of Stanford coreNLP (see Figure 3). However, we follow Jigg's style to represent <s p a n i d =" s 1 " c h i l d =" s 2 " c a t e g o r y ="N" r u l e =" l e x " /> 10 <s p a n . . . /> 11 </ c c g> 12 <s e m a n t i c s> 13 <s p a n i d =" s 1 " c h i l d =" s 2 " sem="\y . t e a ( y ) " t y p e =" t e a : E n t i t y −> P r o p " /> 14 <s p a n . . . /> 15 </ s e m a n t i c s> Our semantic composition produces the <semantics> tag, which has as many children nodes (<span>) as the CCG tree, the same span identifiers and structure. However, semantic spans also have a "sem" attribute encoding the semantics (using NLTK's semantics format) that have been composed for that constituent. An example of a resulting semantic logic formula in NLTK semantics format is: \F.exists y. ( tea(y) & F (y)) Note that predicates are prefixed with an underscore to avoid collisions with reserved predicates in NLTK semantics format or in a potential prover.
Semantic spans also provide the type of single predicates (attribute "type"). For instance, the type of the predicate tea is a function that receives an entity as an argument, and produces a proposition: tea : Entity → Prop Types are automatically inferred using NLTK semantics functionality. However, it is possible to force arbitrary types in a semantic template by adding the attribute "coq type". For example, we can specify the type for a transitive verb as: − semantics : . . . category : (S\NP)/NP coq type : Entity → Entity → Prop We can activate these types with the flag --arbi-types in the call to semparse.py.

Textual Entailment
The logical formulas that ccg2lambda outputs can be used in a variety of applications. In this demonstration, we use them to recognize textual entailment, an NLP problem that often requires precise language understanding. We assume that the user inputs a file with one sentence per line. All sentences are assumed to be premises, except the last sentence, which is assumed to be the conclusion. An entailment problem example is: premise 1 : All women ordered coffee or tea. premise 2 : Some woman did not order coffee. conclusion: Some woman ordered tea.
Contrarily to other textual entailment systems based on logics (Angeli and Manning, 2014;Mac-Cartney and Manning, 2007), we do not assume single-premise problems, which makes our system more general. The MRs of the problem above are: We build a theorem by concatenating meaning representations of the premises {p 1 , . . . , p n } and the conclusion c with the implication operator, which is a convenience in theorem proving: Theorem : p 1 → . . . → p n → c.
Finally, we pipe the theorem and type definitions to Coq (Castéran and Bertot, 2004), an interactive higher-order prover that we run fully automated with the use of some tactics (including arithmetics and equational reasoning), as described in Mineshima et al. (2015). We return the label yes (entailment) if the conclusion can be logically proved from the premises, no if the negated conclusion can be proved, and unknown otherwise.
The recognition of textual entailment can be performed with the following command: # python prove.py semantics.xml where the entailment judgment (yes, no, unknown) is printed to standard output. Moreover, the flag --graph out allows to specify an HTML file to print a graphical visualization of the CCG tree structure of sentences, their semantic composition (every constituent annotated with a component of the formula), and the prover script. An excerpt of the visualization is shown in Section 6. Figure 4: Visualization of the semantic output of ccg2lambda for the sentence "All women ordered coffee or tea." where logical semantic representations appear below their respective CCG nodes. Figure 5: Visualization of the semantic output of ccg2lambda for the sentence "Some woman did not order coffee." where logical semantic representations appear below their respective CCG nodes. Figure 6: Visualization of the semantic output of ccg2lambda for the sentence "Some woman ordered tea." where logical semantic representations appear below their respective CCG nodes.

Visualization
For visualization purposes, we provide a separate script that can be called as: # python visualize.py semantics.xml > semantics.html which produces a file semantics.html with an HTML graphical representation of the CCG tree, augmented at every node with the semantics composed up to that node (see Figures 4, 5 and 6 for an excerpt). These semantic representations are obtained with the semantic templates that accompany our software and that were developed and evaluated in Mineshima et al. (2015). The trivial propositions "TrueP" have no effect and appear in the formulas in place of potential modifiers (such as adjectives or adverbs) of more complex sentences. The visualization can be configured to display the root on top, change colors and sizes of the syntactic categories, feature structures, logical formulas and base forms at the leaves.

Future Work and Conclusion
As an extension to ccg2lambda, it would be valuable to produce (possibly scored) N-best lists of logical formulas, instead of the current single 1-best. Moreover, our current semantic templates do not cover all syntactic categories that C&C or Jigg produce, and we need a good default combination mechanism. Other minor enhancements are to produce logical formulas for each CCG derivation in an N-best list, and to allow features other than the base form to become predicates.
In this paper we have demonstrated our system to convert CCG trees to logic MRs. It operates by composing semantics bottom-up, guided by the combinatory characteristics of the CCG derivation and semantic templates provided by the user. In this release, semantic templates for English and Japanese are also included. As Mineshima et al. (2015) has shown, the MRs obtained by ccg2lambda are useful to recognize textual entailment. We believe that these easy-to-produce MRs can be useful to NLP tasks that require precise language understanding or that benefit from using MRs as features in their statistical systems.