An NLG pipeline for a legal expert system: a work in progress
Abstract
We present the NLG component for L4, a prototype domain-specific language (DSL) for drafting laws and contracts. As a concrete use case, we describe a pipeline for a legal expert system created from L4 code. The NLG component is used in two steps. The first step is to create an interview, whose answers are processed into a query for an automated reasoner. The second step is to render the answers of the reasoner in natural language.
1 Introduction
We introduce L4, a prototype111L4 is a work in progress, and this article presents a snapshot of the project as of June 2021. Any concrete examples of L4 code may change in a few months. domain-specific language (DSL) for drafting laws and contracts. L4’s applied focus places it within the “Rules as Code” movement (e.g. OpenFisca , Catala Merigoux et al. (2021)) that itself draws on early computational law thinking Sergot et al. (1986); Love and Genesereth (2005). But rather than focusing on encoding laws into existing programming languages, we devise an external DSL designed for legal specification.
Drafting in a high-level, declarative DSL makes it possible to separate the legal layer and the application layer. We encode the rules once, and that encoding is a source for further applications, such as legal expert systems, consistency checking, visualisation and natural language generation. If the rules change, they need to be changed only once at the source, and the applications can be automatically updated.
In this paper, we describe an NLG pipeline from a set of rules drafted in L4 into natural language. The generated natural language must adapt to different communicative needs: a piece of code may become, for example, a declaration, a prohibition, a condition, an interview question or an answer to a user query. Because we require flexibility in output, it is important to have a deep understanding of any source material. Section 3 describes the first step, of acquiring a base for NLG from user’s descriptions of classes and predicates—this step is a general prerequisite to all kinds of NLG tasks, not limited to the one we describe here. Section 4 describes a concrete use case of creating a legal expert system from a L4 encoding. Finally, Section 5 outlines future work.
2 L4 example
As an introduction to L4, we will encode the rules of Rock-Paper-Scissors (RPS) for two players.
2.1 L4 basics
Types and values
First, we declare data types for the entities needed to represent the rules. A round of RPS is a Game with two Players, each throwing Sign. Then we introduce the three signs: rock, paper and scissors.
class # types
Player
Game
Sign
decl # values
Rock : Sign
Paper : Sign
Scissors : Sign
Predicates
Next, we define four predicates, used later in our rules. The current syntax of L4 is adopted from functional programming languages. For example, the type signature for Win : Player Game Bool means “the function Win takes a Player and a Game, and returns a Boolean”.
decl # predicates
Participate
: Player → Game → Bool
Throw : Player → Sign → Bool
Win : Player → Game → Bool
Beat : Sign → Sign → Bool
For the purposes of constructing an interview, it is useful to have information on how to group the different predicates. L4 offers an alternative syntax for the predicates to be fields in the data types, as follows.
# equivalent to the standalone
# versions Participate, Throw, Win
class
Player {
participate : Game → Bool
throw : Sign → Bool
win : Game → Bool
}
The class definition with the fields throw, win and participate is equivalent to the stand-alone predicates in capital letters. Note that capital letters have no significance in L4, we just use them in this example to distinguish between standalone predicates and those in a field.
As an alternative, participate and win can be defined as fields of the class Game. With this grouping, the type signature becomes Game Player Bool. We will use this grouping in the examples in Section 4.1, because that makes the best order for asking the questions.
class # different grouping
Game {
participate : Player → Bool
win : Player → Bool
}
Rules
We formulate the rules for winning a game: both players must throw a sign, and the winning player’s sign must beat the other player’s sign. The syntax for function application follows functional programming conventions: to say that “x wins rps”, we write Win x rps. The relations between the signs are also encoded as rules—implementation omitted for brevity.
rule <winner> # rules
for a : Player, g : Game,
r : Sign, s : Sign
if exists b : Player .
Participate a g &&
Participate b g &&
Throw a r && Throw b s &&
Beat r s
then Win a g
2.2 Natural language descriptions
For NLG purposes, the L4 file may include optional descriptions in controlled natural language (CNL). If no natural language description is given, the predicate name is used instead. For example, we can record that Participate takes its object with the preposition “in”.
lexicon
Participate @ "participate in"
The current default is to include only the predicate, and assume the argument order subject–object–indirect object. Subject agreement doesn’t matter: “participate in” and “participates in” are equivalent. For higher than ternary predicates, alternative argument order or several prepositions, the types may used as placeholder arguments. This is how to avoid NLG like “game wins player”, when the predicates are grouped as fields in the classes.
class
Game {win : Player → Bool}
# normalised into predicate
# win : Game → Player → Bool
lexicon
win @ "[Player] wins [Game]
3 CNL to NLG
Listenmaa et al. (2021) describes in detail the CNL and the process of parsing the descriptions, including ambiguity resolution, so we include only a short version here.
CNL
As seen in Section 2.2, the L4 classes and predicates can be enriched with a natural language description. These descriptions are written in a controlled natural language (CNL) which is implemented in Grammatical Framework (GF, Ranta 2004), a programming language for multilingual grammar applications.
Our CNL is based on the GF Resource Grammar Library (RGL) Ranta et al. (2009), which provides a library of syntactic structures and morphology for over 30 languages, and a large, multilingual morphological lexicon Angelov (2020) based on Princeton WordNet Fellbaum (1998), enriched with syntactic features like valency.
On top of the RGL and the GF-WordNet lexicon, we have added a set of constructions common to legal text. In addition, we have relaxed the rules for forming sentences, so that we can parse predicates without arguments, (participates in), or arguments in brackets ([Player] wins [Game]).
So far we have only used English, but the method is scalable to any of the languages that are in the RGL and have a large lexicon.
Goal of the CNL
The purpose of our CNL is to be able to parse the user input correctly, and thus use it correctly in the NLG. Unlike many other CNLs out there (see Kuhn (2014) for a survey), ours is not concerned with well-defined semantics—L4, being a programming language, is much better suited for that. We only care about well understood syntactic structure, so that we can have flexible NLG for different communicative needs.
4 NLG pipeline for an expert system
We construct an interview in order to find out who wins a game of RPS. The main puzzle, who wins RPS, is not one of the questions we pose to the user—instead, we ask about all the prerequisite information, and give the user’s answers to an automated reasoner, which will announce the winner.
NLG-wise, this involves two steps. In the first step, we generate interview questions from the L4 code (Section 4.1). In the second step, we generate natural language from the output of the reasoner (Section 4.2).
4.1 Interview questions
The RPS interview will ask about the players and which signs they threw.
-
1.
Is there a game?
-
•
Answer options: yes/no
-
•
Answered: yes
-
•
-
2.
Who participates in the game?
-
•
Answer options:
-
–
slot for free text
-
–
loop ”Are there more players?”
-
–
-
•
Answered:
-
–
Alice, yes for more players
-
–
Bob, no for more players
-
–
-
•
-
3.
Which sign does Alice throw?
-
•
Answer options: rock/paper/scissors
-
•
-
4.
Which sign does Bob throw?
-
•
Answer options: rock/paper/scissors
-
•
Docassemble
The questions are embedded in a Docassemble interview. Docassemble is an open-source legal expert system, where users answer a set of questions through a browser interface. The responses are then compiled together into a document or processed further in other applications—in our case, an automated reasoner.
In order to let the user customise the interview—for instance, the NLG, question ordering, additional links to source material— our system outputs a LExSIS (Legal Expert System Interface Schema) file, which is short and declarative. The final interview, a long and imperative piece of Python code, is then constructed from the LExSIS file.
Types of questions
The interview includes three types of questions: a yes–no question (1), an open-ended wh-question (2) and two enumeration questions (3,4). These are determined by the L4 code.
Game is a class, so the first question we ask is whether a game exists. When we know that a game exist, we ask wh-questions about the predicates related to the game: who participates and which sign do they throw. The L4 code lists three signs, but no pre-existing players: that’s why the answer options for (2) is a slot for free text, and for (3,4) an enumerated list.
The choice of who in (2) is because player is a human noun. The GF-WordNet lexicon Angelov (2020) contains this information for about 6500 words, so we just make it as a rule in the grammar. If some human nouns are missing the animacy information, they will just be verbalised with the general strategy, which players.
Order of questions
The order of the questions is determined by Docassemble and the L4 encoding.
Docassemble has its internal logic: the goal of the interview is this case to find out the winner of a game of RPS, and thus it starts out from the first prerequisite, is there a game?
L4 too has its own logic: if we used the encoding where participate is a field of the class Game, then it will group the questions accordingly. Participation is a feature of a game, so after ensuring that a game exists, the user is asked about the game’s participants—game is now old information, so in English it gets a definite article and is placed later in the question. With a different grouping of predicates in L4, the questions will also be grouped and phrased differently: for instance, Are there any players, followed by Which game does Alice participate in.
Constructing the questions
Table 2 shows the GF expressions needed to construct different types of sentences. All of the functions in normal typeface come from the language-independent API of the GF RGL222 http://www.grammaticalframework.org/lib/doc/synopsis/. The variables in italics are generic: any noun phrase could be in place of np, any transitive verb for v2. For a verb like participate in, the preposition is part of the verb’s lexical entry—that’s why the expression to construct a VP is identical for participates in RPS and throws rock.
None of the expression templates which includes a noun phrase specifies a determiner. The NP can be constructed in different phases: it can be new or old information, or it may be a proper noun. Unfortunately, the lexicon doesn’t contain properties like mass noun vs. count noun, nor idiomatic expressions, like throw rock instead of throw a rock. Currently, we just accept that sometimes the determiner choice is unnatural, and leave it to the user to postprocess the output.
4.2 Reasoner output in natural language
The second output target is a verbalisation of the output from an automated reasoner.
Using the user answers to the interview from Section 4.1, a solution satisfying all the constraints is generated.
The solution is then restructured in natural language, becoming the conclusion the user receives.
Alice wins RPS, because
-
•
Alice throws paper and Bob throws rock, and
-
•
paper beats rock.
s(CASP) query
The reasoner we use is s(CASP) Arias et al. (2018): a logic programming language for Constraint Answer Set Programming.
The L4 code is translated into s(CASP) rules and statements. The query “who wins a game of RPS” is phrased as ?- win(Game,Player), where Player and Game are s(CASP) variables. The order of the arguments comes from the L4 encoding where win is a field of the class Game.
The L4 encoding doesn’t list any players or games, so s(CASP) needs more information before it can meaningfully run the query. As we have seen in 4.1, this information comes from the interview.
s(CASP) answers
The answer consists of one or more sets of s(CASP) statements. These statements, just like their source in L4, are connected to the CNL descriptions, which are stored as GF abstract syntax trees. This makes it possible to aggregate them, such as grouping them by subject or predicate. In case of multiple sets for the same query, we group repeating statements in one block, grouped by and, and the rest under another, grouped by or. An example of this strategy is shown in Table 2, which shows an answer to a slightly different query: “list all ways that Alice can win a game”.
Currently, our order of aggregating predicates is intransitive before transitive, and nouns/adjectives before verbs. This results in sentences like are players and participants in RPS, are players and participate in RPS, play and participate in RPS, or less fluently, are participants in RPS and play.
5 Future work
As of June 2021, the whole L4 system, including the NLG component, is just a prototype. Future work for the CNL and user descriptions is listed in Listenmaa et al. (2021). The quality of NLG depends on the quality of user input—no amount of sophisticated information structure will cover for badly parsed description, so improving the CNL is our first priority.
We have demonstrated in this paper the system’s capability to answer questions of type What Is True (Alice wins RPS), When Is This True (when Alice throws paper and Bob throws rock) and Why (because paper beats rock). As we scale up to larger examples, we will need more fine-grained heuristics in aggregation, information structure and ordering, as well as adding variation to the structures. We also plan to add more languages.
In the longer term, we plan to support more types of queries, with hypotheticals and goal-seeking, such as What Would Make This True and What Else Is True, and new types of questions and answers require new NLG functions. As another long-term goal, we would like to incorporate more semantic knowledge in our NLG.
Is there a game | mkQS (mkCl noun) |
---|---|
Does Bob participate in RPS | mkQS (mkCl np (mkVP v2 np)) |
Who participates in RPS | mkQS (mkQCl who_IP (mkVP v2 np)) |
Which player throws rock | mkQS (mkQCl (mkIP which_IDet noun) (mkVP v2 np)) |
Which sign does Alice throw | mkQS (mkQCl (mkIP which_IDet noun) (mkClSlash np (mkVPSlash v2))) |
Alice wins RPS, if all of the following hold: | |
---|---|
RPS is a game, and | |
Alice and Bob are players and participate in RPS | |
and one of the following holds: | |
rock beats scissors, Alice throws rock and Bob throws scissors, | |
scissors beats paper, Alice throws scissors and Bob throws paper, or | |
paper beats rock, Alice throws paper and Bob throws rock. |
References
- Angelov (2020) Krasimir Angelov. 2020. A Parallel WordNet for English, Swedish and Bulgarian. In Proceedings of The 12th Language Resources and Evaluation Conference, pages 3008–3015.
- Arias et al. (2018) Joaquin Arias, Manuel Carro, Elmer Salazar, Kyle Marple, and Gopal Gupta. 2018. Constraint answer set programming without grounding. Theory and Practice of Logic Programming, 18(3-4):337–354.
- (3) Docassemble. https:/docassemble.org.
- Fellbaum (1998) Christiane Fellbaum. 1998. WordNet: An electronic lexical database. MIT press.
- Kuhn (2014) Tobias Kuhn. 2014. A survey and classification of controlled natural languages. Computational linguistics, 40(1):121–170.
- Listenmaa et al. (2021) Inari Listenmaa, Maryam Hanafiah, Regina Cheong, and Andreas Källberg. 2021. Towards CNL-based verbalization of computational contracts. In Proceedings of the 7th International Workshop on Controlled Natural Language (CNL 2020/21).
- Love and Genesereth (2005) Nathaniel Love and Michael Genesereth. 2005. Computational Law. In Proceedings of the 10th international conference on Artificial intelligence and law, ICAIL ’05, pages 205–209, New York, NY, USA. Association for Computing Machinery.
- Merigoux et al. (2021) Denis Merigoux, Nicolas Chataing, and Jonathan Protzenko. 2021. Catala: A programming language for the law.
- (9) OpenFisca. https://openfisca.org.
- Ranta (2004) Aarne Ranta. 2004. Grammatical Framework. Journal of Functional Programming, 14(2):145–189. Publisher: Cambridge University Press.
- Ranta et al. (2009) Aarne Ranta, Ali El Dada, and Janna Khegai. 2009. The GF Resource Grammar Library. Linguistic Issues in Language Technology, 2(2):1–63.
- Sergot et al. (1986) M. J. Sergot, F. Sadri, R. A. Kowalski, F. Kriwaczek, P. Hammond, and H. T. Cory. 1986. The British Nationality Act as a logic program. Communications of the ACM, 29(5):370–386.