{'fraction_non_alphanumeric': 0.05047062013402022, 'fraction_numerical': 0.005887593446178874, 'mean_word_length': 3.94408272692455, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 2, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Non-simplifying Graph Rewriting Termination

So far, a very large amount of work in Natural Language Processing (NLP) rely on trees as the core mathematical structure to represent linguistic informations (e.g. in Chomsky's work). However, some linguistic phenomena do not cope properly with trees. In a former paper, we showed the benefit of encoding linguistic structures by graphs and of using graph rewriting rules to compute on those structures. Justified by some linguistic considerations, graph rewriting is characterized by two features: first, there is no node creation along computations and second, there are non-local edge modifications. Under these hypotheses, we show that uniform termination is undecidable and that non-uniform termination is decidable. We describe two termination techniques based on weights and we give complexity bound on the derivation length for these rewriting system.


Introduction
Linguists introduce different levels to describe a natural language sentence. Starting from a sentence given as a sequence of sounds or as a sequence of words; among the linguistic levels, two are deeply considered in literature: the syntactic level (a grammatical analysis of the sentence) and the semantic level (a representation of the meaning of the sentence). These two representations involve mathematical structures such as logical formulae, λ -terms, trees and graphs. One of the usual ways to describe syntax is to use the notion of dependency [16]. A dependency structure is an ordered sequence of words, together with some relations between these words. For instance, the sentence "I see that Mike begins to work" can be represented by the structure on the right.
There is a large debate in the literature about the mathematical nature of the structures needed for natural language syntax: do we have to consider trees or graphs? Trees are often considered for their simplicity; however, it is clearly insufficient. Let us illustrate the limitations of tree-representations with some linguistic examples. Consider the sentence "Bill expects Mary to come", the node "Mary" is shared, being the subject of "come" and the object of "expects" (below on the left). The situation can be even worse: cycles may appear such as in the sentence below where edges in the cycle are drawn with dashed line (below on the right).
For the semantic representation of natural language sentences, first order logic formulae are widely used. To deal with natural language ambiguity, a more compact representation of a set of logic formulae (called underspecified semantic representation) is used. DMRS [4] is one of these compact representation. The DMRS structure for the sentence "The Dog whose toy the cat bit barked" is given in the figure on the right.
To describe transformations between syntactic and semantics structures, there are solutions based on many computational models (finite state automata, λ -calculus). It is somewhat surprising that Graph Rewrite Systems (GRS) have been hardly considered so far ( [8,1,5,9]). To explain that, GRS implementations are usually considered to be too inefficient to justify their extra-generality. For instance, pattern matching does not take linear time where this is usually seen as an upper limit for fast treatment.
However, if one drops for a while the issue of efficiency, the use of GRS is promising. Indeed, linguistic considerations can be most of the time expressed by some relations between a few words. Thus, they are easily translated into rules. To illustrate this point, in [3,12], we proposed a syntax to semantics translator based on GRSs: given the syntax of a sentence, it outputs the different meaning associated to this syntax.
In the two earlier mentioned studies, we tried to delineate what are the key features of graph rewriting in the context of NLP. Roughly speaking, node creation are strictly restricted, edges may be shifted from one node to another and there is a need for negative patterns. Based on this analysis, we define here a suitable framework for NLP (see Section 3).
Compared to term rewriting, the semantics of graph rewriting is problematic: different choices can be made in the way the context is glued to the rule application [15]. As far as we see, our notion does not fit properly the DPO approach due to unguarded node deletion nor the SPO approach due to the shift command, as we shall see. Thus we will provide a complete description of our notion. We have chosen to present it in an operational way and we leave for future work a categorial semantics.
In our application, we use several hundreds of rules. To manage such a system, we use a notion of modular graph rewriting system: the full set of rules is divided in smaller subsets (called modules) that are used in turn.
In practice, we need some tools to verify termination and confluence properties of modules. In Section 4, we provide two termination methods based on a weight analysis. First, there is a direct motivation: in our NLP application, any computations should terminate. If it is not the case, it means that the rules where not correctly drawn. Then, termination ensures partly the correctness of the transformation. There is also an indirect reason to consider termination: one way of establishing confluence is through Newman's Lemma [11] which requires termination.
We consider two properties of the above mentioned termination methods. First, we show that they are decidable, that is the existence of weights can be computed statically from the rules, and thus we have a fully automatic tool to verify termination. Obviously, it is not complete. In a second step, we evaluate the strength of the two methods. To do that, we consider what restrictions they impose on the length of computations. We get quadratic time for the first method, polynomial time for the second. This article is an extended abstract of [2].

Linguistic motivations
Without any linguistic exhaustivity, we highlight in this section some crucial points of the kind of linguistic transformation we are interested in and hence the relative features of rewriting we have to consider.
Node preservation property. As linguistic examples above suggest, the goal of linguistic analysis is mainly to describe different kinds of relations between elements that are present in the input structure. As a consequence, the set of nodes in the output structure is directly predictable from the input and only a very restrictive notion on node creation is needed. In practice, these node creations can be anticipated in some enriched input structure on which the whole transformation can be described as a non-size increasing process.
Edge shifting. In the first example of the introduction (for the sentence "I see that Mike begins to work"), the verb "begins" is called a raising verb and we know that "Mike" is the deep subject of the verb "work"; "begins" being considered as a modifier of the verb. To recover this deep subject, one may imagine a local transformation of the graph which turns the first graph on the right into the second one. However, in our example above, a direct application of such a transformation leads to the structure below on the left which is not the right structure. Indeed, the transformation should shift what the linguists call the head of the phrase "Mike begins to work" from the word "begins" to the word "work" with all relative edges. In that case, the transformation should produce the structure below on the right: In a more general setting, our transformations may have to specify the fact that all incident edges of some node X must be transported to some other node Y . We call this operation shift.
To describe our graph rewriting rules, we introduce a system of commands (like in [6]) which expresses step by step the modifications applied on the input graph. The transformation described above is performed in our setting as follows: Negative conditions. In some situation, rules must be aware of the context of the pattern to avoid unwanted ambiguities. When computing semantics out of syntax, one has to deal with passive sentence; the two sentences below show that the agent is optional. In order to switch to the corresponding active form, two different linguistic transformations have to be defined for these two sentence; but, clearly, the first graph is a subgraph of the second one. We don't want the transformation for the short passive on the left to apply on the long passive on the right. we need to express a negative condition like "there is no out edge labeled by AGT out of the main verb" to prevent the unwanted transformation to occur. Long distance dependencies. Most of the linguistic transformation can be expressed with successive local transformation like the one above. Nevertheless, there are some cases where more global rewriting is required; consider the sentence "The women whom John seems to love", for which we consider the syntactic structure on the right. One of the steps in the semantic construction of this sentence requires to compute the antecedent of the relative pronoun "whom" (the noun "woman" in our example).
The subgraph we have to search in our graph (which is depicted as a non-local pattern) and the graph modification to perform are given on the right. The number of OBJ or COMP relations to consider (in the relation depicted as (OBJ|COMP)* in the figure) is unbounded (in linguistics, this phenomenon is called long distance dependencies); it is possible to construct grammatical sentences with an arbitrary large number of relations.
As we want to stay in the well-known framework of local rewriting, we will use several local transformations to implement such a non-local rule. The second and the third rules above preserve the set of nodes and the number of edges of each kind. Hence, this kind of rule will require special treatment with respect to termination issues.

Graph Rewriting for NLP
Before we enter into the technical sections, let us define some useful notations. First, we use the notation c to denote sequences. The empty sequence is written / 0. The length of a sequence is denoted by | c |. We use the same notation for sets: the empty set is denoted / 0 and the cardinality of a set S is written |S|. The context will make clear whether we are talking about sequences or sets.
Given a function f : X → Y and some sets The function c X : x ∈ X → c ∈ Y is the constant function on X. The identity function is written 1. Finally, given a function f : X → Y and (x, y) ∈ X ×Y , the function f [x → y] maps t = x to f (t) and x to y.
The set of natural numbers is N, integers are denoted by Z. Given two integers a, b, we define

Graphs
The graphs we consider are directed graphs with both labels on nodes and labels on edges. We restrict the edge set: given some edge label e, there is at most one edge labeled e between two given nodes α and β . This restriction reflects the fact that, in NLP application, our edges are used to encode linguistic information which are relations. We make no other explicit hypothesis on graphs: in particular, graphs may be disconnected, or have loops.
In this paper, we suppose given a finite set Σ E of edge labels and another finite set Σ N of node labels.
• N is a finite set of nodes; • is a labeling function: Let n, m ∈ N and e ∈ Σ E . When there is an edge from n to m labelled e (i.e. (n, e, m) ∈ E ), we write n e −→ m or n −→ m if the edge label is not relevant. If G denotes some graph (N , , E ), then N G , G , E G denote respectively N , and E .
A graph morphism µ is said to be injective if µ(n) = µ(m) implies n = m. We make the following abuse of notation: given some graph morphism µ : As shown in Section 2, negative conditions on patterns naturally arise in NLP. We classify negative conditions in two categories: the local ones, that is negative conditions on edges within the basic pattern and non-local ones, that is negative conditions concerning edges between a node of the basic pattern and a node of the context (either in-edges or out-edges).

Definition 3.4 (Pattern).
A pattern is a quadruple P = (B,Ē ,Ī ,Ō) of: • a basic pattern B = (N P , P , E P ); Given a basic pattern B, we shorten (B, / 0, / 0, / 0) to (B, / 0). In the following, given a pattern P, N P and E P denote respectively the set of nodes of its basic pattern and the set of edges of its basic pattern.
Definition 3.5 (Matching). Let P = (B,Ē ,Ī ,Ō) be a pattern, G = (N , , E ) be a graph, and µ : B → G be a basic matching. We say that µ is a matching from P into G (also written µ : P → G) whenever it satisfies the additional three conditions: Example 3.1. Negative conditions are used to remove 'unwanted' matching. To see their effect, consider for instance the basic pattern B 0 and its two basic matchings µ 1 and µ 2 in G 0 : Then, µ 1 and µ 2 are (the) two matchings P 0 → G 0 .
• Third, let the pattern Then, there is no matching of P 2 into G 0 .
In the following, patterns P 1 and P 2 are depicted as:

Graph decomposition
The proper description of actions of a rule on some graph G requires first the definition of two partitions: one on nodes and the other on edges. They are both induced by the matching of some pattern P into G. Definition 3.6 (Nodes decomposition: pattern image, crown and context). Let µ : P → G a matching from the pattern P into the graph G = (N , , E ). Nodes of G can be split in a partition of three sets N = P µ ⊕ K µ ⊕ C µ : • the pattern image is P µ = µ(N P ); • the crown contains nodes outside the pattern image which are directly connected to the pattern image: K µ = {n ∈ N \ P µ | ∃p ∈ P µ such that n −→ p or p −→ n}; • the context contains nodes not linked to the pattern image: C µ = N \ (P µ ∪ K µ ).
Definition 3.7 (Edges decomposition: pattern edges, crown edges, context edges and pattern-glued edges). Let µ : P → G a matching from the pattern P into the graph G = (N , , E ). Edges of G can be split in a partition of four sets E = µ(E P ) ⊕ K µ ⊕ C µ ⊕ H µ : • the pattern edges is µ(E P ); • the crown edges set contains edges which links a pattern image node to a crown node: • the context edges set contains edges which connect two nodes that are not in the pattern image: • the pattern-glued edges set contains edges which are not pattern edges but which connect two nodes that are in the pattern image: H µ = {n −→ m ∈ E | n ∈ P µ ∧ m ∈ P µ } \ µ(E P ).

Rules
In our graph rewriting framework, the transformation of the graph is described through some atomic commands (like in [6]). Commands definition refer to some pattern P and pattern nodes N P are used as identifiers. Let a, b ∈ N P , α ∈ Σ N and e ∈ Σ E , the five kinds of commands are label(a, α), del edge(a, e, b), add edge(a, e, b), del node(a) and shift(a, b). Their names speak for themselves, however, we will come back to their precise meaning in the subsection below. Before this, to ensure that commands always refer to valid node identifiers, we restrict command sequences to consistent sequences, that is sequences c 1 , . . . , c k such that for each command c i , 1 ≤ i ≤ k, which is a node deletion command del node(a) for some a ∈ N P , then the node name a does not occur in any command c j with i < j ≤ k.
Definition 3.8 (Rule). A rule R is a pair R = P, c of a pattern P and a sequence of commands c consistent with respect to P. A rule R = P, c is said to be node-preserving if c does not contain any del node command.

Graph Rewrite System
Let G = (N , , E ) a graph, R = P, c a rule and µ : P → G a matching. The application of the sequence c on G is a new graph which is written G · µ c (shortened G · c when µ is clear from the context) and is defined by induction on the length k of c. If k = 0, G · / 0 = G. If k > 0, let G = (N , , E ) be the graph obtained by application of the sequence c 1 , . . . , c k−1 ; then we consider each command in turn: The commands label, del edge and add edge are called local commands: they modify only the edges and the nodes described in the pattern. The commands del node and shift are non-local: they can modify edges outside the pattern. Note that a rule add edge (resp. del edge) may have no effect if the edge already exists (resp. does not exist). Note also that we can suppose that for a given sequence c and a given triple (a, e, b), there is at most one rule del edge(a, e, b) or add edge(a, e, b) in c (if not, only the last one is effective). Hence, we can define uniform rules: Definition 3.9 (Uniform rule). For c = c 1 , . . . , c k without node deletion, the rule P, c is uniform iff for all 1 ≤ i ≤ k, if c i = add edge(n, e, m) then (n, e, m) ∈Ē P and if c i = del edge(n, e, m) then (n, e, m) ∈ E P . Definition 3.10 (Rewrite step). Let G = (N , , E ) a graph, R = P, c a rule and µ : P → G a matching. Let G = G · c, then we say that G rewrites to G with respect to the rule R and the matching µ. We write it G → R,µ G or G → R G or even simply G → G . In our application, the translation of the syntax to semantics is split into several independent levels of transformation driven by linguistic consideration (such as translation of passive forms to active ones, computation of the deep subject of infinites). Rules are then grouped in subsets called modules and modules apply sequentially; each module being used as a graph rewrite system on the outputs of the previous module.  1 (Linear modification). Given a GRS G , there is a constant C > 0 such that, for any rewriting step G → R,µ G the two canonical corresponding edge decompositions E G = C µ ⊕ Q µ and E G = C µ ⊕ Q µ satisfy: |Q µ | ≤ C × (|G| + 1) and |Q µ | ≤ C × (|G| + 1) Proof. Let C = max{2 × |P| 2 × |Σ E |} | P, c ∈ G }. Both in G and G , edges that are not in the context are either between two pattern nodes or between a pattern node and a crown node. The total number of edges of the first kind (either pattern edges or glued-pattern edges) is bounded by |P| 2 × |Σ E |. For each pattern node, the number of edges which connect this node to some non-pattern node is bounded by 2×|G|×|Σ E | and so the total number of edges which link some pattern node to some non-pattern node is bounded by 2 × |G| × |Σ E | × |P|. Putting everything together, |Q µ | ≤ C × (|G| + 1) and |Q µ | ≤ C × (|G| + 1).

Weighted GRS
We recall that a GRS is said to be (strongly) terminating whenever there is no infinite sequence G 1 → G 2 → · · · . Given a terminating GRS G and a graph G, we define the derivation height of G, next denoted h G (G), to be the length of the longest derivation starting from G if such a derivation exists. If h G (G) is defined for all G such that |G| ≤ n, then we define the derivation height of G by: h G (n) = max{h G (G) | |G| ≤ n}. Actually, for non-size increasing GRS as presented above, we have immediately the decidability of non-uniform termination. That is, given some GRS G and some graph G, one may decide whether there is an infinite sequence G 1 → G 2 → G 3 → · · · . Indeed, one may observe that for such sequence, for all i ∈ N, |G i | ≤ |G|. Thus, the G i 's range in the finite set G ≤|G| of graphs of size less or equal to |G|. Consequently, either the system terminates or there is some j ≤ |G ≤|G| | and some k ≤ j such that G j = G k . To conclude, to decide non-uniform termination, it is sufficient to compute all the (finitely many) possibilities of rewriting G in less than |G ≤|G| | steps and to verify the existence of such a j and k above. Finally, since |G ≤|G| | ≤ 2 O(|G| 2 ) , the procedure as described above takes exponential time.
However, uniform termination-given a GRS, is it terminating?-of non-size increasing GRS remains an open problem. Uniform termination was proved undecidable when we drop the property of non-size increasingness (cf. Plump [14]). As a consequence, there is a need to define some termination method pertaining to non-size increasing GRS. Compared to standard work in termination [13,7], there are two difficulties: first, our graphs may be cyclic, thus forbidding methods developed for DAGs such as term-graphs. Second, using term rewriting terminology, our method should operate for some non-simplifying GRS, that is GRS for which the output may be "bigger" than the input. Indeed, the NLP programmer sometimes wants to compute some new relations, so that the input graph is a strict sub-graph of the resulting graph.

Termination by weight analysis
In the context of term-rewriting systems, the use of weights is very common to prove termination. There are many examples of such orderings, Knuth-Bendix Ordering [10] to cite one of them. We recall that all graphs we consider are defined relatively to two signatures Σ E of edge labels and Σ N of node labels. Definition 4.1 (Edge weight, node weight). An edge weight is a mapping w : Σ E → Z. Given some subset E of edges of G, the weight of E is w(E) = ∑ n e −→m∈E w(e). The edge weight of a graph G is w(G) = w(E G ). A node weight is a mapping η : Let us make some observations. Let |G| e denote the number of edges in G which have the label e, then w(G) = ∑ e∈Σ E w(e) × |G| e . Second, for a pattern matching µ : P → G, w(µ(P)) = w(P).
The weight of a graph may be negative. This is not standard, but it is useful here to cope with nonsimplifying rules, that is rules which add new edges. Since a graph G has at most |Σ E | × |G| 2 edges, the following lemma is immediate.
(c) for each graph G, we have |η(G)| ≤ K η × |G|.  1. either c contains a del node command 2. or R is a node-preserving rule and satisfy the three properties: (a) R is uniform, (b) w(P · 1 c ) < w(P), (c) for all e ∈ Σ E such that w(e) < 0, for all n ∈ Φ(N P ), let M n ⊂ E P be the set Φ −1 c (n); then M n contains at most one element m such that (m, e) ∈Ī and M n contains at most one element m ∈ M n such that (m , e) ∈Ō.
An edge weight is said to be compatible with a GRS G if it is compatible with all its rules. A weighted GRS is a pair (G , w) of a GRS and a compatible weight.
Hypothesis (2.b) will serve to manage edges in the pattern images while Hypothesis (2.c) will serve for the crown edges. One may note that when there is no shift commands in the rule, the Hypothesis (2.c) holds whatever w is. Indeed, in that case, Φ is the identity function and all the sets M n are singletons. The problem of the synthesis is the following. Given a GRS G , is there a weight w compatible with G ? Since the existence of weights can be described in Presburger's arithmetic, we have a positive answer: Observe that w(Q 1 · c 1 ) = 1 < 2 = w(Q 1 ) and w(Q 2 · c 2 ) = −2 < −1 = w(Q 2 ). However, there is an infinite sequence G 1 → R 1 G 2 → R 2 G 1 → R 1 · · · with G 1 and G 2 being: Set w(E) = 1. The rules are compatible with w. Each rule deleting exactly one edge, since the clique C n of size n has n 2 edges, the derivation height h G (C n ) = n 2 . The lower bound follows.
Full proofs of Theorem 4.1 and of Theorem 4.2 are given in [2].

Termination by lexicographic weight
In our experiments, in most cases, the weight analysis of the preceding section was sufficient. The main counter-example is however systems composed of rules as given in Section 2. The GRS is strongly terminating but there is no compatible weight. This section provides a conciliable extension of this termination proof method. With a little abstraction, the linguistic example of Section 2 about long distance dependencies is computed by some 'non-local rule' R nl : Such non-local rules can be implemented by rules:

Figure 1: Local implementation of the non-local rule
However, these rules are not compatible with any weight. Actually, as justified in [2], there is no implementation of such a rule by some weighted rules.
Given an order ≺ on some set U, its lexicographic extension to sequences in U is defined by The order ≺ lex is not wellfounded in general, but its restriction to sequences of equal length is such as soon as ≺ is well-founded. Definition 4.4 (Contextual weight). An edge contextual weight is a (finite) map ω : Σ N × Σ E × Σ N → Z. As for weights, it extends to any set E ⊆ E G of some graph G by: ω(E) = ∑ n e −→m∈E ω( (n), e, (m)). And the weight of a graph is ω(G) = ω(E G ).

Conclusion
The polynomial derivation height that we have proved in the last section can be reconsidered in the following way. The example of a GRS working in O(n k ) can be used as a clock. Then, since each transition of a (non-size increasing) Turing-Machine can be easily simulated by graph rewriting, we can state that any PTIME-predicate can be simulated by a lexicographically weighted GRS (up to a polynomial reduction). Since lexicographically weighted confluent GRS can be computed in polynomial time (each rewriting step can be simulated in linear time), it becomes clear that lexicographically weighted GRSs actually characterize PTIME. This provides a precise description of the computational content of the method.
We have implemented a software -called GREW (grew.loria.fr)-based on the Graph Rewriting definition presented in this article. In [12], the software was used to produce a semantically annotated version of the French Treebank; in this experiment, the system contains 34 modules and 571 rules and the corpus is constituted of 12 000 sentences of length up to 100 words. This experiment is a large scale application which shows that the proposed approach can be used in real-size applications.
As said earlier, despite the global non-confluence of the system, we can isolate subsets of rules that are confluent and use our system of modules to benefit from this confluence in implementation. In our last experiment, 26 of our 34 modules are confluent, but confluence proofs are tedious. We leave for further work the study of the local confluence of terminating GRS and the general study of confluence of Graph Rewriting Systems.