-
Notifications
You must be signed in to change notification settings - Fork 1
/
spacy_features.py
377 lines (339 loc) · 16.3 KB
/
spacy_features.py
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# This module could be a project available on GitHub and [spaCy Universe](https://spacy.io/universe).
# People could use it from `sent._.[attr]`.
from __future__ import annotations
from typing import Literal
import spacy.tokens
def create_model(model_name: str = "en_core_web_trf", prefer_gpu: bool = True) -> spacy.language.Language:
if prefer_gpu:
spacy.prefer_gpu()
return spacy.load(model_name)
def get_first_sentence(doc: spacy.tokens.Doc) -> spacy.tokens.Span:
return next(iter(doc.sents))
def get_sentence_count(doc: spacy.tokens.Doc) -> int:
return sum(1 for _ in doc.sents)
def get_noun_chunk_count(doc: spacy.tokens.Doc) -> int:
return sum(1 for _ in doc.noun_chunks)
def get_tense(sent: spacy.tokens.Span) -> Literal["Past", "Pres", "Fut"] | None:
"""Computes the grammatical tense of an English sentence. If it's not a sentence (or if it can't determine the
tense), it returns `None`.
Examples
---
>>> spacy_model = create_model()
>>> get_tense(get_first_sentence(spacy_model("The man runs in the forest.")))
'Pres'
>>> get_tense(get_first_sentence(spacy_model("The man is running again.")))
'Pres'
>>> get_tense(get_first_sentence(spacy_model("I'm walking on sunshine.")))
'Pres'
>>> get_tense(get_first_sentence(spacy_model("I was walking yesterday.")))
'Past'
>>> get_tense(get_first_sentence(spacy_model("I will be arriving next week.")))
'Fut'
>>> get_tense(get_first_sentence(spacy_model("I'll be arriving next week.")))
'Fut'
>>> get_tense(get_first_sentence(spacy_model("The dogs will walk.")))
'Fut'
>>> get_tense(get_first_sentence(spacy_model("She'll teach the class.")))
'Fut'
>>> get_tense(get_first_sentence(spacy_model("I'll always love you.")))
'Fut'
>>> get_tense(get_first_sentence(spacy_model("I left already.")))
'Past'
>>> get_tense(get_first_sentence(spacy_model("A cat was hungry again.")))
'Past'
>>> get_tense(get_first_sentence(spacy_model("They are going to jump the fence.")))
'Fut'
>>> get_tense(get_first_sentence(spacy_model("They are gonna jump the fence.")))
'Fut'
>>> get_tense(get_first_sentence(spacy_model("They are going to the cinema.")))
'Pres'
>>> get_tense(get_first_sentence(spacy_model("They have gone to the cinema.")))
'Pres'
>>> get_tense(get_first_sentence(spacy_model("They've gone to the cinema.")))
'Pres'
>>> get_tense(get_first_sentence(spacy_model("They have been going to the cinema.")))
'Pres'
>>> get_tense(get_first_sentence(spacy_model("They had gone to the cinema.")))
'Past'
>>> get_tense(get_first_sentence(spacy_model("They'd gone to the cinema.")))
'Past'
>>> get_tense(get_first_sentence(spacy_model("They had been going to the cinema.")))
'Past'
>>> get_tense(get_first_sentence(spacy_model("They will have gone to the cinema.")))
'Fut'
>>> get_tense(get_first_sentence(spacy_model("They will have been going to the cinema.")))
'Fut'
"""
root = sent.root
if ((root.lower_ in {"going", "gon", "gon'"} and any(t.tag_ == "VB" and t.dep_ == "xcomp" for t in root.rights))
or any(t.lower_ in {"'ll", "will"} and t.tag_ == "MD" for t in root.lefts)):
return "Fut"
elif ((root.tag_ == "VBN" or (root.tag_ == "VBG" and any(t.lower_ == "been" for t in root.lefts)))
and (have := next((t for t in root.lefts if t.lemma_ in {"have", "'d", "'ve"}), None))
and (have_token_morphological_tense := have.morph.get("Tense"))):
return have_token_morphological_tense[0] # noqa
elif ((be := next((t for t in root.lefts if t.lemma_ == "be" and t.dep_ == "aux"), None))
and (be.morph.get("VerbForm") or [""])[0] == "Fin"
and (be_morphological_tense := be.morph.get("Tense"))):
return be_morphological_tense[0] # noqa
elif root_morphological_tenses := root.morph.get("Tense"):
return root_morphological_tenses[0]
elif any(t.tag_ == "MD" and t.lower_ in {"can"} for t in root.lefts):
return "Pres"
else:
return None
def is_continuous(sent: spacy.tokens.Span) -> bool:
"""Computes the continuous grammatical aspect of an English sentence. If it's not a sentence (or if it can't
determine the tense), it returns `False`.
Examples
---
>>> spacy_model = create_model()
>>> is_continuous(get_first_sentence(spacy_model("The man runs in the forest.")))
False
>>> is_continuous(get_first_sentence(spacy_model("The man is running again.")))
True
>>> is_continuous(get_first_sentence(spacy_model("I'm walking on sunshine.")))
True
>>> is_continuous(get_first_sentence(spacy_model("I was walking yesterday.")))
True
>>> is_continuous(get_first_sentence(spacy_model("I will be arriving next week.")))
True
>>> is_continuous(get_first_sentence(spacy_model("I'll be arriving next week.")))
True
>>> is_continuous(get_first_sentence(spacy_model("The dogs will walk.")))
False
>>> is_continuous(get_first_sentence(spacy_model("She'll teach the class.")))
False
>>> is_continuous(get_first_sentence(spacy_model("I'll always love you.")))
False
>>> is_continuous(get_first_sentence(spacy_model("I left already.")))
False
>>> is_continuous(get_first_sentence(spacy_model("A cat was hungry again.")))
False
>>> is_continuous(get_first_sentence(spacy_model("They are going to jump the fence.")))
False
>>> is_continuous(get_first_sentence(spacy_model("They are gonna jump the fence.")))
False
>>> is_continuous(get_first_sentence(spacy_model("They are going to the cinema.")))
True
>>> is_continuous(get_first_sentence(spacy_model("They have gone to the cinema.")))
False
>>> is_continuous(get_first_sentence(spacy_model("They've gone to the cinema.")))
False
>>> is_continuous(get_first_sentence(spacy_model("They have been going to the cinema.")))
True
>>> is_continuous(get_first_sentence(spacy_model("They had gone to the cinema.")))
False
>>> is_continuous(get_first_sentence(spacy_model("They'd gone to the cinema.")))
False
>>> is_continuous(get_first_sentence(spacy_model("They had been going to the cinema.")))
True
>>> is_continuous(get_first_sentence(spacy_model("They will have gone to the cinema.")))
False
>>> is_continuous(get_first_sentence(spacy_model("They will have been going to the cinema.")))
True
>>> is_continuous(get_first_sentence(spacy_model("They would have been going to the cinema.")))
True
"""
root = sent.root
if (root.lower_ in {"going", "gon", "gon'"}
and (verb := next((t for t in root.rights if t.tag_ == "VB" and t.dep_ == "xcomp"), None))):
root = verb
return (root.morph.get("Aspect") or ["Hab"])[0] == "Prog"
def is_perfect(sent: spacy.tokens.Span) -> bool:
"""Computes the perfect grammatical aspect of an English sentence. If it's not a sentence (or if it can't
determine it), it returns `False`.
Examples
---
>>> spacy_model = create_model()
>>> is_perfect(get_first_sentence(spacy_model("The man runs in the forest.")))
False
>>> is_perfect(get_first_sentence(spacy_model("The man is running again.")))
False
>>> is_perfect(get_first_sentence(spacy_model("I'm walking on sunshine.")))
False
>>> is_perfect(get_first_sentence(spacy_model("I was walking yesterday.")))
False
>>> is_perfect(get_first_sentence(spacy_model("I will be arriving next week.")))
False
>>> is_perfect(get_first_sentence(spacy_model("I'll be arriving next week.")))
False
>>> is_perfect(get_first_sentence(spacy_model("The dogs will walk.")))
False
>>> is_perfect(get_first_sentence(spacy_model("She'll teach the class.")))
False
>>> is_perfect(get_first_sentence(spacy_model("I'll always love you.")))
False
>>> is_perfect(get_first_sentence(spacy_model("I left already.")))
False
>>> is_perfect(get_first_sentence(spacy_model("A cat was hungry again.")))
False
>>> is_perfect(get_first_sentence(spacy_model("They are going to jump the fence.")))
False
>>> is_perfect(get_first_sentence(spacy_model("They are gonna jump the fence.")))
False
>>> is_perfect(get_first_sentence(spacy_model("They are going to the cinema.")))
False
>>> is_perfect(get_first_sentence(spacy_model("They have gone to the cinema.")))
True
>>> is_perfect(get_first_sentence(spacy_model("They've gone to the cinema.")))
True
>>> is_perfect(get_first_sentence(spacy_model("They have been going to the cinema.")))
True
>>> is_perfect(get_first_sentence(spacy_model("They had gone to the cinema.")))
True
>>> # is_perfect(get_first_sentence(spacy_model("They'd gone to the cinema."))) # Bug: it's parsed as "would".
>>> is_perfect(get_first_sentence(spacy_model("They had been going to the cinema.")))
True
>>> is_perfect(get_first_sentence(spacy_model("They will have gone to the cinema.")))
True
>>> is_perfect(get_first_sentence(spacy_model("They will have been going to the cinema.")))
True
>>> is_perfect(get_first_sentence(spacy_model("They would have been going to the cinema.")))
True
"""
root = sent.root
return ((root.tag_ == "VBN" or (root.tag_ == "VBG" and any(t.lower_ == "been" for t in root.lefts)))
and any(t.lemma_ in {"have", "'d", "'ve"} for t in root.lefts))
def get_subject_person(sent: spacy.tokens.Span) -> Literal["1", "2", "3"] | None:
"""Computes the subject person of an English sentence. If it's not a sentence (or if it can't
determine it), it returns `None`.
Examples
---
>>> spacy_model = create_model()
>>> get_subject_person(get_first_sentence(spacy_model("The man runs in the forest.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("The man is running again.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("I'm walking on sunshine.")))
'1'
>>> get_subject_person(get_first_sentence(spacy_model("I was walking yesterday.")))
'1'
>>> get_subject_person(get_first_sentence(spacy_model("I will be arriving next week.")))
'1'
>>> get_subject_person(get_first_sentence(spacy_model("I'll be arriving next week.")))
'1'
>>> # get_subject_person(get_first_sentence(spacy_model("The dogs will walk."))) # It fails.
>>> get_subject_person(get_first_sentence(spacy_model("She'll teach the class.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("I'll always love you.")))
'1'
>>> get_subject_person(get_first_sentence(spacy_model("I left already.")))
'1'
>>> get_subject_person(get_first_sentence(spacy_model("A cat was hungry again.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They are going to jump the fence.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They are gonna jump the fence.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They are going to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They have gone to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They've gone to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They have been going to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They had gone to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They'd gone to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They had been going to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They will have gone to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They will have been going to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("They would have been going to the cinema.")))
'3'
>>> get_subject_person(get_first_sentence(spacy_model("You'll get there.")))
'2'
"""
root = sent.root
if root_morphological_person := root.morph.get("Person"):
return root_morphological_person[0]
elif ((subj := next((t for t in sent.root.children if t.dep_ == "nsubj"), None))
and (subj_morphological_person := subj.morph.get("Person"))):
return subj_morphological_person[0] # noqa
elif ((aux := next((t for t in sent.root.children if t.dep_ == "aux"), None))
and (aux_morphological_person := aux.morph.get("Person"))):
return aux_morphological_person[0] # noqa
else:
return None
def get_subject_number(sent: spacy.tokens.Span) -> Literal["Sing", "Plur"] | None:
"""Computes if the subject is plural in an English sentence. If it's not a sentence (or if it can't
determine it), it returns `None`.
Examples
---
>>> spacy_model = create_model()
>>> get_subject_number(get_first_sentence(spacy_model("The man runs in the forest.")))
'Sing'
>>> get_subject_number(get_first_sentence(spacy_model("The man is running again.")))
'Sing'
>>> get_subject_number(get_first_sentence(spacy_model("I'm walking on sunshine.")))
'Sing'
>>> get_subject_number(get_first_sentence(spacy_model("I was walking yesterday.")))
'Sing'
>>> get_subject_number(get_first_sentence(spacy_model("I will be arriving next week.")))
'Sing'
>>> get_subject_number(get_first_sentence(spacy_model("I'll be arriving next week.")))
'Sing'
>>> # get_subject_number(get_first_sentence(spacy_model("The dogs will walk."))) # It fails.
>>> get_subject_number(get_first_sentence(spacy_model("She'll teach the class.")))
'Sing'
>>> get_subject_number(get_first_sentence(spacy_model("I'll always love you.")))
'Sing'
>>> get_subject_number(get_first_sentence(spacy_model("I left already.")))
'Sing'
>>> get_subject_number(get_first_sentence(spacy_model("A cat was hungry again.")))
'Sing'
>>> get_subject_number(get_first_sentence(spacy_model("They are going to jump the fence.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They are gonna jump the fence.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They are going to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They have gone to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They've gone to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They have been going to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They had gone to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They'd gone to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They had been going to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They will have gone to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They will have been going to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("They would have been going to the cinema.")))
'Plur'
>>> get_subject_number(get_first_sentence(spacy_model("We'll get there.")))
'Plur'
"""
root = sent.root
if root_morphological_person := root.morph.get("Number"):
return root_morphological_person[0]
elif ((subj := next((t for t in sent.root.children if t.dep_ == "nsubj"), None))
and (subj_morphological_person := subj.morph.get("Number"))):
return subj_morphological_person[0] # noqa
elif ((aux := next((t for t in sent.root.children if t.dep_ == "aux"), None))
and (aux_morphological_person := aux.morph.get("Number"))):
return aux_morphological_person[0] # noqa
else:
return None
def has_any_adjective(doc: spacy.tokens.Doc) -> bool:
return any(t.pos_ == "ADJ" for t in doc)
def has_any_gerund(doc: spacy.tokens.Doc) -> bool:
return any(t.tag_ == "VBG" for t in doc)
def has_any_adverb(doc: spacy.tokens.Doc) -> bool:
return any(t.pos_ == "ADV" for t in doc)
def is_passive_voice(sent: spacy.tokens.Span) -> bool:
return any(t.dep_ == "auxpass" for t in sent.root.children)
def get_root_tag(sent: spacy.tokens.Span) -> str:
return sent.root.tag_
def get_root_pos(sent: spacy.tokens.Span) -> str:
return sent.root.pos_