File: cql.py

package info (click to toggle)
python-pynlpl 1.2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,900 kB
  • sloc: python: 25,677; sh: 73; makefile: 3
file content (307 lines) | stat: -rw-r--r-- 10,125 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#---------------------------------------------------------------
# PyNLPl - Corpus Query Language (CQL)
#   by Maarten van Gompel
#   Centre for Language Studies
#   Radboud University Nijmegen
#   http://proycon.github.com/folia
#   http://www.github.com/proycon/pynlpl
#   proycon AT anaproy DOT nl
#
# Parser and interpreter for a basic subset of the Corpus Query Language
#
#   Licensed under GPLv3
#
#----------------------------------------------------------------

from __future__ import print_function, unicode_literals, division, absolute_import

from pynlpl.fsa import State, NFA
import re
import sys

OPERATORS = ('=','!=')
MAXINTERVAL = 99

class SyntaxError(Exception):
    pass

class ValueExpression(object):
    def __init__(self, values):
        self.values = values #disjunction

    @staticmethod
    def parse(s,i):
        values = ""
        assert s[i] == '"'
        i += 1
        while not (s[i] == '"' and s[i-1] != "\\"):
            values += s[i]
            i += 1
        values = values.split("|")
        return ValueExpression(values), i+1

    def __len__(self):
        return len(self.values)

    def __iter__(self):
        for x in self.values:
            yield x

    def __getitem__(self,index):
        return self.values[index]

class AttributeExpression(object):
    def __init__(self, attribute, operator, valueexpression):
        self.attribute = attribute
        self.operator = operator
        self.valueexpr = valueexpression

    @staticmethod
    def parse(s,i):
        while s[i] == " ":
            i +=1
        if s[i] == '"':
            #no attribute and no operator, use defaults:
            attribute = "word"
            operator = "="
        else:
            attribute = ""
            while s[i] not in (' ','!','>','<','='):
                attribute += s[i]
                i += 1
            if not attribute:
                raise SyntaxError("Expected attribute name, none found")
            operator = ""
            while s[i] in (' ','!','>','<','='):
                if s[i] != ' ':
                    operator += s[i]
                i += 1
            if operator not in OPERATORS:
                raise SyntaxError("Expected operator, got '" + operator + "'")
        if s[i] != '"':
            raise SyntaxError("Expected start of value expression (doublequote) in position " + str(i) + ", got " + s[i])
        valueexpr, i = ValueExpression.parse(s,i)
        return AttributeExpression(attribute,operator, valueexpr), i

class TokenExpression(object):
    def __init__(self, attribexprs=[], interval=None):
        self.attribexprs = attribexprs
        self.interval = interval

    @staticmethod
    def parse(s,i):
        attribexprs = []
        while s[i] == " ":
            i +=1
        if s[i] == '"':
            attribexpr,i = AttributeExpression.parse(s,i)
            attribexprs.append(attribexpr)
        elif s[i] == "[":
            i += 1
            while True:
                while s[i] == " ":
                    i +=1
                if s[i] == "&":
                    attribexpr,i = AttributeExpression.parse(s,i+1)
                    attribexprs.append(attribexpr)
                elif s[i] == "]":
                    i += 1
                    break
                elif not attribexprs:
                    attribexpr,i = AttributeExpression.parse(s,i)
                    attribexprs.append(attribexpr)
                else:
                    raise SyntaxError("Unexpected char whilst parsing token expression,  position " + str(i) + ": " + s[i])
        else:
            raise SyntaxError("Expected token expression starting with either \" or [, got: " + s[i])

        if i == len(s):
            interval = None #end of query!
        elif s[i] == "{":
            #interval expression, find end:
            interval = None
            for j in range(i+1, len(s)):
                if s[j] == "}":
                    interval = s[i+1:j]

            if interval is None:
                raise SyntaxError("Interval expression started but no end-brace found")

            i += len(interval) + 2

            try:
                if ',' in interval:
                    interval = tuple(int(x) for x in interval.split(","))
                    if len(interval) != 2:
                        raise SyntaxError("Invalid interval: " + interval)
                elif '-' in interval: #alternative
                    interval = tuple(int(x) for x in interval.split("-"))
                    if len(interval) != 2:
                        raise SyntaxError("Invalid interval: " + interval)
                else:
                    interval = (int(interval),int(interval))
            except ValueError:
                raise SyntaxError("Invalid interval: " + interval)
        elif s[i] == "?":
            interval = (0,1)
            i += 1
        elif s[i] == "+":
            interval = (1,MAXINTERVAL)
            i += 1
        elif s[i] == "*":
            interval = (0,MAXINTERVAL)
            i += 1
        else:
            interval = None

        return TokenExpression(attribexprs,interval),i


    def __len__(self):
        return len(self.attribexprs)

    def __iter__(self):
        for x in self.attribexprs:
            yield x

    def __getitem__(self,index):
        return self.attribexprs[index]

    def nfa(self, nextstate):
        """Returns an initial state for an NFA"""
        if self.interval:
            mininterval, maxinterval = self.interval #pylint: disable=unpacking-non-sequence
            nextstate2 = nextstate
            for i in range(maxinterval):
                state = State(transitions=[(self,self.match, nextstate2)])
                if i+1> mininterval:
                    if nextstate is not nextstate2: state.transitions.append((self,self.match, nextstate))
                    if maxinterval == MAXINTERVAL:
                        state.epsilon.append(state)
                        break
                nextstate2 = state
            return state
        else:
            state = State(transitions=[(self,self.match, nextstate)])
            return state


    def match(self, value):
        match = False
        for _, attribexpr in enumerate(self):
            annottype = attribexpr.attribute
            if annottype == 'text': annottype = 'word'
            if attribexpr.operator == "!=":
                negate = True
            elif attribexpr.operator == "=":
                negate = False
            else:
                raise Exception("Unexpected operator " + attribexpr.operator)

            if len(attribexpr.valueexpr) > 1:
                expr = re.compile("^(" + "|".join(attribexpr.valueexpr) + ")$")
            else:
                expr = re.compile("^" + attribexpr.valueexpr[0] + '$')
            match = (expr.match(value[annottype]) is not None)
            if negate:
                match = not match
            if not match:
                return False
        return True



class Query(object):
    def __init__(self, s):
        self.tokenexprs = []
        i = 0
        l = len(s)
        while i < l:
            if s[i] == " ":
                i += 1
            else:
                tokenexpr,i = TokenExpression.parse(s,i)
                self.tokenexprs.append(tokenexpr)

    def __len__(self):
        return len(self.tokenexprs)

    def __iter__(self):
        for x in self.tokenexprs:
            yield x

    def __getitem__(self,index):
        return self.tokenexprs[index]

    def nfa(self):
        """convert the expression into an NFA"""
        finalstate = State(final=True)
        nextstate = finalstate
        for tokenexpr in reversed(self):
            state = tokenexpr.nfa(nextstate)
            nextstate = state
        return NFA(state)


    def __call__(self, tokens, debug=False):
        """Execute the CQL expression, pass a list of tokens/annotations using keyword arguments: word, pos, lemma, etc"""

        if not tokens:
            raise Exception("Pass a list of tokens/annotation using keyword arguments! (word,pos,lemma, or others)")

        #convert the expression into an NFA
        nfa = self.nfa()
        if debug:
            print(repr(nfa), file=sys.stderr)

        return list(nfa.find(tokens,debug))







def cql2fql(cq):
    fq = "SELECT FOR SPAN "
    if not isinstance(cq, Query):
        cq = Query(cq)

    for i, token in enumerate(cq):
        if i > 0: fq += " & "
        fq += "w"
        if token.interval:
            fq += " {" + str(token.interval[0]) + "," + str(token.interval[1])+ "} "
        else:
            fq += " "
        if token.attribexprs:
            fq += "WHERE "
            for j, attribexpr in enumerate(token):
                if j > 0:
                    fq += " AND "
                fq += "("
                if attribexpr.operator == "!=":
                    operator = "NOTMATCHES"
                elif attribexpr.operator == "=":
                    operator = "MATCHES"
                else:
                    raise Exception("Invalid operator: " + attribexpr.operator)
                if attribexpr.attribute in ("word","text"):
                    if len(attribexpr.valueexpr) > 1:
                        fq += "text " + operator + " \"^(" + "|".join(attribexpr.valueexpr) + ")$\" "
                    else:
                        fq += "text " + operator + " \"^" + attribexpr.valueexpr[0] + "$\" "
                else:
                    annottype = attribexpr.attribute
                    if annottype == "tag":
                        annottype = "pos"
                    elif annottype == "lempos":
                        raise Exception("lempos not supported in CQL to FQL conversion, use pos and lemma separately")
                    fq += annottype + " HAS class "
                    if len(attribexpr.valueexpr) > 1:
                        fq += operator + " \"^(" + "|".join(attribexpr.valueexpr) + ")$\" "
                    else:
                        fq += operator + " \"^" + attribexpr.valueexpr[0] + "$\" "
                fq += ")"

    return fq