-
Notifications
You must be signed in to change notification settings - Fork 35
/
spacy_features.py
365 lines (323 loc) · 14.5 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
'''
AAA lllllll lllllll iiii
A:::A l:::::l l:::::l i::::i
A:::::A l:::::l l:::::l iiii
A:::::::A l:::::l l:::::l
A:::::::::A l::::l l::::l iiiiiii eeeeeeeeeeee
A:::::A:::::A l::::l l::::l i:::::i ee::::::::::::ee
A:::::A A:::::A l::::l l::::l i::::i e::::::eeeee:::::ee
A:::::A A:::::A l::::l l::::l i::::i e::::::e e:::::e
A:::::A A:::::A l::::l l::::l i::::i e:::::::eeeee::::::e
A:::::AAAAAAAAA:::::A l::::l l::::l i::::i e:::::::::::::::::e
A:::::::::::::::::::::A l::::l l::::l i::::i e::::::eeeeeeeeeee
A:::::AAAAAAAAAAAAA:::::A l::::l l::::l i::::i e:::::::e
A:::::A A:::::A l::::::ll::::::li::::::ie::::::::e
A:::::A A:::::A l::::::ll::::::li::::::i e::::::::eeeeeeee
A:::::A A:::::A l::::::ll::::::li::::::i ee:::::::::::::e
AAAAAAA AAAAAAAlllllllllllllllliiiiiiii eeeeeeeeeeeeee
______ _ ___ ______ _____
| ___| | | / _ \ | ___ \_ _| _
| |_ ___ __ _| |_ _ _ _ __ ___ ___ / /_\ \| |_/ / | | (_)
| _/ _ \/ _` | __| | | | '__/ _ \/ __| | _ || __/ | |
| || __/ (_| | |_| |_| | | | __/\__ \ | | | || | _| |_ _
\_| \___|\__,_|\__|\__,_|_| \___||___/ \_| |_/\_| \___/ (_)
_____ _
|_ _| | |
| | _____ _| |_
| |/ _ \ \/ / __|
| | __/> <| |_
\_/\___/_/\_\\__|
Featurize folders of text files if default_text_features = ['spacy_features']
Extract linguistic features using the spacy library.
This is one of many things the spacy library can do.
Extracts 315 features with labels below:
['PROPN', 'ADP', 'DET', 'NUM', 'PUNCT', 'SPACE',
'VERB', 'NOUN', 'ADV', 'CCONJ', 'PRON', 'ADJ',
'SYM', 'PART', 'INTJ', 'X', 'pos_other', 'NNP',
'IN', 'DT', 'CD', 'NNPS', ',', '_SP', 'VBZ', 'NN',
'RB', 'CC', '', 'NNS', '.', 'PRP', 'MD', 'VB',
'HYPH', 'VBD', 'JJ', ':', '-LRB-', '$', '-RRB-',
'VBG', 'VBN', 'NFP', 'RBR', 'POS', 'VBP', 'RP',
'JJS', 'PRP$', 'EX', 'JJR', 'WP', 'WDT', 'TO',
'WRB', "''", '``', 'PDT', 'AFX', 'RBS', 'UH',
'WP$', 'FW', 'XX', 'SYM', 'LS', 'ADD', 'tag_other',
'compound', 'ROOT', 'prep', 'det', 'pobj',
'nummod', 'punct', '', 'nsubj', 'advmod',
'cc', 'conj', 'aux', 'dobj', 'nmod', 'acl',
'appos', 'npadvmod', 'amod', 'agent', 'case',
'intj', 'prt', 'pcomp', 'ccomp', 'attr',
'dep', 'acomp', 'poss', 'auxpass', 'expl',
'mark', 'nsubjpass', 'quantmod', 'advcl', 'relcl',
'oprd', 'neg', 'xcomp', 'csubj', 'predet', 'parataxis',
'dative', 'preconj', 'csubjpass', 'meta', 'dep_other',
'\ufeffXxx', 'Xxxxx', 'XXxxx', 'xx', 'X', 'Xxxx', 'Xxx',
',', '\n\n', 'xXxxx', 'xxx', 'xxxx', '\n', '.', ' ',
'-', 'xxx.xxxx.xxx', '\n\n\n', ':', '\n ',
'dddd', '[', '#', 'dd', ']', 'd', 'XXX-d',
'*', 'XXXX', 'XX', 'XXX', '\n\n\n\n',
'Xx', '\n\n\n ', '--', '\n\n ',
' ', ' ', ' ', "'x", 'x', 'X.', 'xxx--',
';', 'Xxx.', '(', ')', "'", '“', '”', 'Xx.',
'!', "'xx", 'xx!--Xxx', "x'xxxx", '?',
'_', "x'x", "x'xx", "Xxx'xxxx", 'Xxxxx--',
'xxxx--', '--xxxx', 'X--', 'xx--', 'xxxx”--xxx', 'xxx--“xxxx',
"Xxx'x", ';--', 'xxx--_xxx', "xxx'x", 'xxx!--xxxx',
'xxxx?--_Xxx', "Xxxxx'x", 'xxxx--“xxxx', "xxxx'xxx",
'--Xxxxx', ',--', '?--', 'xx--“xx', 'xx!--X', '.--',
'xxx--“xxx', ':--', 'Xxxxx--“xxxx', 'xxxx!--xxxx',
'xx”--xxx', 'xxxx--_xxx', 'xxxx--“xxx', '--xx',
'--X', 'xxxx!--Xxx', '--xxx', 'xxx_.', 'xxxx--_xx',
'xxxx--_xx_xxxx', 'xx!--xxxx', 'xxxx!--xx', "X'xx",
"xxxx'x", "X_'x", "xxx'xxx", '--Xxxx', "X'Xxxxx",
"Xx'xxxx", '--Xxx', 'xxxx”--xxxx', 'xxxx!--',
'xxxx--“x', 'Xxxx!--Xxxx', 'xxx!--Xxx',
'Xxxxx.', 'xxxx_.', 'xx--“Xxxx', '\n\n ',
'Xxxxx”--xxx', 'xxxx”--xx', 'xxxx--“xx',
"Xxxxx!--Xxx'x", "X'xxxx", 'Xxxxx?--',
'--Xx', 'xxxx!”--Xx', "xxxx--“X'x", "xxxx'", 'xxx.--“Xxxx',
'xxxx--“X', 'xxxx!--X', 'Xxx”--xx', 'xxx”--xxx', 'xxx-_xxx',
"x'Xxxxx", 'Xxxxx!--X', 'Xxxxx!--Xxx', 'dd-d.xxx', 'xxxx://xxx.xxxx.xxx/d/dd/',
'xXxxxx', 'xxxx://xxxx.xxx/xxxx', 'd.X.', '/', 'd.X.d', 'd.X', '%',
'Xd', 'xxxx://xxx.xxxx.xxx', 'ddd(x)(d', 'X.X.', 'ddd', '[email protected]',
'xxxx://xxxx.xxx', '$', 'd,ddd', 'shape_other', 'mean sentence polarity',
'std sentence polarity', 'max sentence polarity', 'min sentence polarity',
'median sentence polarity', 'mean sentence subjectivity',
'std sentence subjectivity', 'max sentence subjectivity',
'min sentence subjectivity', 'median sentence subjectivity',
'character count', 'word count', 'sentence number', 'words per sentence',
'unique chunk noun text', 'unique chunk root text',
'unique chunk root head text', 'chunkdep ROOT', 'chunkdep pobj',
'chunkdep nsubj', 'chunkdep dobj', 'chunkdep conj', 'chunkdep appos',
'chunkdep attr', 'chunkdep nsubjpass', 'chunkdep dative', 'chunkdep pcomp',
'number of named entities', 'PERSON', 'NORP', 'FAC', 'ORG', 'GPE', 'LOC',
'PRODUCT', 'EVENT', 'WORK_OF_ART', 'LAW', 'LANGUAGE', 'DATE', 'TIME',
'PERCENT', 'MONEY', 'QUANTITY', 'ORDINAL', 'CARDINAL']
'''
import spacy
from spacy.symbols import nsubj, VERB
from textblob import TextBlob
import numpy as np
def stats(matrix):
mean=np.mean(matrix)
std=np.std(matrix)
maxv=np.amax(matrix)
minv=np.amin(matrix)
median=np.median(matrix)
output=np.array([mean,std,maxv,minv,median])
return output
def spacy_featurize(transcript):
try:
nlp=spacy.load('en_core_web_sm')
except:
os.system('python3 -m spacy download en_core_web_sm')
nlp=spacy.load('en_core_web_sm')
doc=nlp(transcript)
# initialize lists
entity_types=['PERSON','NORP','FAC','ORG',
'GPE','LOC','PRODUCT','EVENT',
'WORK_OF_ART','LAW','LANGUAGE',
'DATE','TIME','PERCENT','MONEY',
'QUANTITY','ORDINAL','CARDINAL']
pos_types=['PROPN', 'ADP', 'DET', 'NUM',
'PUNCT', 'SPACE', 'VERB', 'NOUN',
'ADV', 'CCONJ', 'PRON', 'ADJ',
'SYM', 'PART', 'INTJ', 'X']
tag_types=['NNP', 'IN', 'DT', 'CD',
'NNPS', ',', '_SP', 'VBZ',
'NN', 'RB', 'CC', '', 'NNS',
'.', 'PRP', 'MD', 'VB',
'HYPH', 'VBD', 'JJ', ':',
'-LRB-', '$', '-RRB-', 'VBG',
'VBN', 'NFP', 'RBR', 'POS',
'VBP', 'RP', 'JJS', 'PRP$',
'EX', 'JJR', 'WP', 'WDT',
'TO', 'WRB', "''", '``',
'PDT', 'AFX', 'RBS', 'UH',
'WP$', 'FW', 'XX', 'SYM', 'LS',
'ADD']
dep_types=['compound', 'ROOT', 'prep', 'det',
'pobj', 'nummod', 'punct', '',
'nsubj', 'advmod', 'cc', 'conj',
'aux', 'dobj', 'nmod', 'acl',
'appos', 'npadvmod', 'amod', 'agent',
'case', 'intj', 'prt', 'pcomp',
'ccomp', 'attr', 'dep', 'acomp',
'poss', 'auxpass', 'expl', 'mark',
'nsubjpass', 'quantmod', 'advcl', 'relcl',
'oprd', 'neg', 'xcomp', 'csubj',
'predet', 'parataxis', 'dative', 'preconj',
'csubjpass', 'meta']
shape_types=['\ufeffXxx', 'Xxxxx', 'XXxxx', 'xx',
'X', 'Xxxx', 'Xxx', ',', '\n\n',
'xXxxx', 'xxx', 'xxxx', '\n',
'.', ' ', '-', 'xxx.xxxx.xxx', '\n\n\n',
':', '\n ', 'dddd', '[', '#', 'dd', ']',
'd', 'XXX-d', '*', 'XXXX',
'XX', 'XXX', '\n\n\n\n', 'Xx',
'\n\n\n ', '--', '\n\n ', ' ',
' ', ' ', "'x", 'x',
'X.', 'xxx--', ';', 'Xxx.',
'(', ')', "'", '“', '”',
'Xx.', '!', "'xx", 'xx!--Xxx',
"x'xxxx", '?', '_', "x'x", "x'xx",
"Xxx'xxxx", 'Xxxxx--', 'xxxx--',
'--xxxx', 'X--', 'xx--', 'xxxx”--xxx',
'xxx--“xxxx', "Xxx'x", ';--',
'xxx--_xxx', "xxx'x", 'xxx!--xxxx', 'xxxx?--_Xxx',
"Xxxxx'x", 'xxxx--“xxxx', "xxxx'xxx", '--Xxxxx',
',--', '?--', 'xx--“xx', 'xx!--X',
'.--', 'xxx--“xxx', ':--', 'Xxxxx--“xxxx',
'xxxx!--xxxx', 'xx”--xxx', 'xxxx--_xxx', 'xxxx--“xxx',
'--xx', '--X', 'xxxx!--Xxx', '--xxx',
'xxx_.', 'xxxx--_xx', 'xxxx--_xx_xxxx', 'xx!--xxxx',
'xxxx!--xx', "X'xx", "xxxx'x", "X_'x",
"xxx'xxx", '--Xxxx', "X'Xxxxx", "Xx'xxxx",
'--Xxx', 'xxxx”--xxxx', 'xxxx!--',
'xxxx--“x', 'Xxxx!--Xxxx', 'xxx!--Xxx', 'Xxxxx.',
'xxxx_.', 'xx--“Xxxx', '\n\n ', 'Xxxxx”--xxx',
'xxxx”--xx', 'xxxx--“xx', "Xxxxx!--Xxx'x", "X'xxxx",
'Xxxxx?--', '--Xx', 'xxxx!”--Xx', "xxxx--“X'x", "xxxx'",
'xxx.--“Xxxx', 'xxxx--“X', 'xxxx!--X', 'Xxx”--xx', 'xxx”--xxx',
'xxx-_xxx', "x'Xxxxx", 'Xxxxx!--X', 'Xxxxx!--Xxx',
'dd-d.xxx', 'xxxx://xxx.xxxx.xxx/d/dd/', 'xXxxxx', 'xxxx://xxxx.xxx/xxxx',
'd.X.', '/', 'd.X.d', 'd.X',
'%', 'Xd', 'xxxx://xxx.xxxx.xxx', 'ddd(x)(d',
'X.X.', 'ddd', '[email protected]', 'xxxx://xxxx.xxx',
'$', 'd,ddd']
chunkdep_types=['ROOT', 'pobj', 'nsubj', 'dobj', 'conj',
'appos', 'attr', 'nsubjpass', 'dative', 'pcomp']
# initialize lists
features=list()
labels=list()
poslist=list()
taglist=list()
deplist=list()
shapelist=list()
sentences=list()
sentence_length=0
sent_polarity=list()
sent_subjectivity=list()
# EXTRACT ALL TOKENS
for token in doc:
if token.pos_ in pos_types:
poslist.append(token.pos_)
else:
poslist.append('pos_other')
if token.tag_ in tag_types:
taglist.append(token.tag_)
else:
taglist.append('tag_other')
if token.dep_ in dep_types:
deplist.append(token.dep_)
else:
deplist.append('dep_other')
if token.shape_ in shape_types:
shapelist.append(token.shape_)
else:
shapelist.append('shape_other')
pos_types.append('pos_other')
tag_types.append('tag_other')
dep_types.append('dep_other')
shape_types.append('shape_other')
# count unique instances throughout entire tokenization
# keep labels as well
for i in range(len(pos_types)):
features.append(poslist.count(pos_types[i]))
labels.append(pos_types[i])
for i in range(len(tag_types)):
features.append(taglist.count(tag_types[i]))
labels.append(tag_types[i])
for i in range(len(dep_types)):
features.append(deplist.count(dep_types[i]))
labels.append(dep_types[i])
for i in range(len(shape_types)):
features.append(shapelist.count(shape_types[i]))
labels.append(shape_types[i])
# EXTRACT SENTENCES
for sent in doc.sents:
sentences.append(sent.text)
# NOW ITERATE OVER SENTENCES TO CALCULATE THINGS PER SENTENCE
for i in range(len(sentences)):
sent_polarity.append(TextBlob(sentences[i]).sentiment[0])
sent_subjectivity.append(TextBlob(sentences[i]).sentiment[1])
# STATISTICAL POLARITY AND SUBJECTIVITY FEATURES PER SENTENCE
sent_polarity=stats(np.array(sent_polarity))
for i in range(len(sent_polarity)):
features.append(sent_polarity[i])
if i == 0:
labels.append('mean sentence polarity')
elif i == 1:
labels.append('std sentence polarity')
elif i == 2:
labels.append('max sentence polarity')
elif i == 3:
labels.append('min sentence polarity')
elif i == 4:
labels.append('median sentence polarity')
sent_subjectivity=stats(np.array(sent_subjectivity))
for i in range(len(sent_subjectivity)):
features.append(sent_subjectivity[i])
if i ==0:
labels.append('mean sentence subjectivity')
elif i==1:
labels.append('std sentence subjectivity')
elif i==2:
labels.append('max sentence subjectivity')
elif i==3:
labels.append('min sentence subjectivity')
elif i==4:
labels.append('median sentence subjectivity')
# CHARACTERS
characters=len(transcript)
features.append(characters)
labels.append('character count')
# TOTAL NUMBER OF WORDS
words=len(transcript.split())
features.append(words)
labels.append('word count')
# TOTAL NUMBER OF SENTENCES
sentence_num=len(sentences)
features.append(sentence_num)
labels.append('sentence number')
# WORDS PER SENTENCE
wps=sentence_num/words
features.append(wps)
labels.append('words per sentence')
# NEED TO GET MORE FEATURES
#_________________________
# EXTRACT NOUN CHUNKS
chunktext=list()
chunkroot=list()
chunkdep=list()
chunkhead=list()
for chunk in doc.noun_chunks:
if chunk.text not in chunk.text:
chunktext.append(chunk.text)
#print('text:'+chunk.text)
if chunk.root.text not in chunkroot:
chunkroot.append(chunk.root.text)
# later extract chunkdep
chunkdep.append(chunk.root.dep_)
if chunk.root.head.text not in chunkhead:
chunkhead.append(chunk.root.head.text)
features.append(len(chunktext))
labels.append('unique chunk noun text')
features.append(len(chunkroot))
labels.append('unique chunk root text')
features.append(len(chunkhead))
labels.append('unique chunk root head text')
for i in range(len(chunkdep_types)):
features.append(chunkdep.count(chunkdep_types[i]))
labels.append('chunkdep '+chunkdep_types[i])
# EXTRACT NAMED ENTITY FREQUENCIES
ent_texts=list()
ent_labels=list()
for ent in doc.ents:
ent_texts.append(ent.text)
ent_labels.append(ent.label_)
features.append(len(ent_texts))
labels.append('number of named entities')
for i in range(len(entity_types)):
features.append(ent_labels.count(entity_types[i]))
labels.append(entity_types[i])
return features, labels