-
Notifications
You must be signed in to change notification settings - Fork 1
/
FeatureClass.py
1458 lines (1269 loc) · 66.8 KB
/
FeatureClass.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
__version__ = "1.0"
__date__ = "24.07.2016"
__author__ = "Ivan Bilan"
import nltk
import os
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from string import punctuation
from time import sleep
import codecs
import re
import numpy
class FeatureClass(object):
def __init__(self, lang):
# load resources
self.language = lang
if lang == "en":
self.connective_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources/en/connective_words.txt'))
self.slang_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\en\\slang_words.txt'))
# get emoticon words
# list taken from https://gist.github.com/ryanlewis/a37739d710ccdb4b406d
self.emotion_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\en\\emotion_words.txt'))
# get swear words
self.abbreviation_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\en\\abbreviation.txt'))
self.contractions_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\en\\contractions.txt'))
self.cachedStopWords = stopwords.words("english")
elif lang == "nl":
# get connective words
self.connective_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\nl\\connective_words.txt'))
self.slang_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\nl\\slang_words.txt'))
# get emoticon words
# list taken from https://gist.github.com/ryanlewis/a37739d710ccdb4b406d
self.emotion_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\nl\\emotion_words.txt'))
# get swear words
# self.swear_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\nl\\swear_words.txt')
self.abbreviation_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\nl\\abbreviation.txt'))
self.contractions_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\nl\\contractions.txt'))
self.cachedStopWords = stopwords.words("dutch")
elif lang == "es":
# get connective words
self.connective_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\es\\connective_words.txt'))
self.slang_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\es\\slang_words.txt'))
# get emoticon words
self.emotion_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\es\\emotion_words.txt'))
# get swear words
# self.swear_words_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\es\\swear_words.txt')
self.abbreviation_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\es\\abbreviation.txt'))
self.contractions_list = self.extract_bow(os.path.join(os.path.dirname(__file__), 'Resources\\es\\contractions.txt'))
self.cachedStopWords = stopwords.words("spanish")
# emoticon regex
self.m_emoticons_comp = re.compile("[\;\:\=]\-*[\)\(\]\>\/]")
# pre-scaling text levels
calc = list()
self.calc_words = self.recursive_addition(calc, 0, 13, 10450)
calc2 = list()
self.calc_chars = self.recursive_addition(calc2, 0, 80, 64800)
def recursive_addition(self, list_inner, val, step, limit):
val+=step
list_inner.append(val)
if val < limit:
return self.recursive_addition(list_inner, val, step, limit)
else:
return list_inner
def regex_str(self, items):
"""
Create a regex out of a list
"""
new_list = list()
for x in items:
# print x
new_list.append(re.escape(x))
fulls_joined = '|'.join(new_list)
my_regex = r'(^|\b)('+fulls_joined+r')(\s|$)' # repr(fulls_joined)
# print my_regex
return my_regex
def extract_bow(self, filename):
"""
Extract tokens from txt file
"""
input_file = codecs.open(filename, encoding = "utf_8", mode='r')
lines = input_file.readlines()
bow_words = []
for x in lines:
x = x.replace('\r\n', '')
bow_words.append(x.strip().lower())
input_file.close()
return list(set(bow_words))
def catch_url(self, untagged, average_value = 0):
"""
Get the usage of linked content
"""
if average_value == 3:
sum_vector = 0
for single_text in untagged:
tokens = single_text.split()
result = re.findall("\[URL\]", untagged)
if (len(result) > 0) and (len(tokens) > 1):
average = float(len(result)) / len(tokens)
sum_vector += average
else:
average = 0
sum_vector += average
if len(untagged) > 0:
average_return = sum_vector / len(untagged)
else:
average_return = sum_vector
return average_return
else:
tokens = untagged.split()
result = re.findall(r"url", untagged, re.IGNORECASE)
if average_value == 0:
return len(result)
elif average_value == 1:
if (len(result) > 0) and (len(tokens) > 0):
average = float(len(result)) / len(tokens)
else:
average = 0
return average
elif average_value == 5:
return self.counter_pre_scaling(len(result), len(word_tokenize(untagged)))
def contractions(self, untagged, average_value = 0):
# count the amount of contractions in the text
# list of words taken from http://www.textfixer.com/resources/english-contractions-list.php
return self.word_counter_feature(self.contractions_list, untagged, average_value)
'''
# not included, may be used in future work
# this function counts the amount of swear words in the blog posts
def swear_words(self, untagged, average_value = 5):
# list taken from https://gist.github.com/ryanlewis/a37739d710ccdb4b406d
return self.word_counter_feature(self.swear_words_list, untagged, average_value)
'''
def emotion_words(self, untagged, average_value = 5):
# this function counts the amount of emotion words ( e.g. disgusted, hurt, aggressive )
# http://www.psychpage.com/learning/library/assess/feelings.html
return self.word_counter_feature(self.emotion_words_list, untagged, average_value)
def slang_words(self, untagged, average_value = 5):
# this function counts the amount of slang words
# http://www.psychpage.com/learning/library/assess/feelings.html
return self.word_counter_feature(self.slang_words_list, untagged, average_value)
def connective_words(self, untagged, average_value = 5):
#### checked
# feature suggested here : http://www.uni-weimar.de/medien/webis/research/events/pan-13/pan13-talks/pan13-author-profiling/meina13-poster.pdf
# these feature looks for all connective words (words that provide stylistic connection between sentences, paragraphs etc.)
# http://www.grammarbank.com/connectives-list.html
return self.word_counter_feature(self.connective_words_list, untagged, average_value)
def get_abbreviations(self, untagged, average_value = 5):
# http://www.grammarbank.com/connectives-list.html
# list taken from http://www.textfixer.com/resources/english-contractions-list.php
return self.word_counter_feature(self.abbreviation_list, untagged, average_value)
# get all emoticons
# not used, use for future work
def get_emoticons(self, untagged, average_value = 0):
if average_value == 3:
sum_vector = 0
for single_text in untagged:
counter = 0
# tokens = word_tokenize(untagged)
tokens = single_text.split()
for token in tokens:
m_emoticons = self.m_emoticons_comp.match(token)
if m_emoticons:
counter += 1
#print token
if (counter > 0) and (len(tokens) > 1):
average = float(counter) / len(tokens)
sum_vector += average
else:
average = 0
sum_vector += average
if len(untagged) > 0:
average_return = sum_vector / len(untagged)
else:
average_return = sum_vector
return average_return
else:
counter = 0
tokens = untagged.split()
for token in tokens:
m_emoticons = self.m_emoticons_comp.match(token)
if m_emoticons:
counter += 1
if average_value == 0:
return counter
elif average_value == 1:
if (counter > 0) and (len(tokens) > 1):
average = float(counter) / len(tokens)
else:
average = 0
return average
# get all emoticons
def positive_emoticons(self, untagged, average_value=0):
# not used
# this function counts the amount of positive emoticons
if average_value == 3:
sum_vector = 0
for single_text in untagged:
# print single_text, len(single_text)
counter = 0
# tokens = word_tokenize(untagged)
tokens = single_text.split()
list_of_unicode_smilies = [u'😀', u'😁', u'😂', u'😃',u'😄',u'😅',u'😆', u'😉',u'😊',u'😋', u'😎',u'🙋',u'😸', u'😛',u'?']
for token in tokens:
# m = re.match("(\:|\;)(\=|c|\-\o)*(\]|\)|\D|\*)*", token)
m = re.match("((?::|;|<)(?:-|,)?(?:\)|D|3))", token)
if m: # or m2
#print token
counter += 1
if token in list_of_unicode_smilies:
#print token
counter += 1
if (counter > 0) and (len(tokens) > 1):
average = float(counter) / len(tokens)
sum_vector += average
else:
average = 0
sum_vector += average
if len(untagged) > 0:
average_return = sum_vector / len(untagged)
else:
average_return = sum_vector
# print average_return
return average_return
else:
counter = 0
tokens = untagged.split()
list_of_unicode_smilies = [u'😀', u'😁', u'😂', u'😃',u'😄',u'😅',u'😆', u'😉',u'😊',u'😋', u'😎',u'🙋',u'😸', u'😛',u'?']
for token in tokens:
# m = re.match("(\:|\;)(\=|c|\-\o)*(\]|\)|\D|\*)*", token)
m = re.match("((?::|;|<)(?:-|,)?(?:\)|D|3))", token)
if m: # or m2
#print token
counter += 1
if token in list_of_unicode_smilies:
#print token
counter += 1
if average_value == 0:
return counter
elif average_value == 1:
if (counter > 0) and (len(tokens) > 3):
average = float(counter) / len(tokens)
else:
average = 0
return average
# not used
# this function counts the amount of negative emoticons
def negative_emoticons(self, untagged, average_value = 0):
if average_value == 3:
sum_vector = 0
for single_text in untagged:
counter = 0
tokens = single_text.split()
#print tokens[0]
list_of_unicode_smilies = [u'😒', u'😕', u'😟', u'😠', u'😞', u'😢', u'😦', u'😧', u'😬', u'😿', u'🙎']
for token in tokens:
# m = re.match("(\:|\;)(\=|c|\-\o)*(\]|\)|\D)*", token)
#m = re.match("((?::|;|=|D)(?:-)?(?:\(|\\|x|X|8|c|\[|\:))", token) # |\:
m = re.match("((?::|;|=|D)(?:-)?(?:\(|\\|x|X|8|c|C|\[|\:))", token) # |\:
#m2 = re.match("\<\3*", token)
if m : # or m2
#print token
counter += 1
if token in list_of_unicode_smilies:
#print token
counter += 1
if (counter > 0) and (len(tokens) > 1):
average = float(counter) / len(tokens)
sum_vector += average
else:
average = 0
sum_vector += average
if len(untagged) > 0:
average_return = sum_vector / len(untagged)
else:
average_return = sum_vector
return average_return
else:
counter = 0
tokens = untagged.split()
#print tokens[0]
list_of_unicode_smilies = [u'😒', u'😕', u'😟', u'😠', u'😞', u'😢', u'😦', u'😧', u'😬', u'😿', u'🙎']
for token in tokens:
# m = re.match("(\:|\;)(\=|c|\-\o)*(\]|\)|\D)*", token)
#m = re.match("((?::|;|=|D)(?:-)?(?:\(|\\|x|X|8|c|\[|\:))", token) # |\:
m = re.match("((?::|;|=|D)(?:-)?(?:\(|\\|x|X|8|c|C|\[|\:))", token) # |\:
#m2 = re.match("\<\3*", token)
if m : # or m2
#print token
counter += 1
if token in list_of_unicode_smilies:
#print token
counter += 1
if average_value == 0:
return counter
elif average_value == 1:
if (counter > 0) and (len(tokens) > 3):
average = float(counter) / len(tokens)
else:
average = 0
return average
# not used
# this function counts the amount of neutral emoticons
def neutral_emoticons(self, untagged, average_value = 0):
if average_value == 3:
sum_vector = 0
for single_text in untagged:
counter = 0
list_of_unicode_smilies = [u'😐', u'😑', u'😶']
tokens = single_text.split()
#print tokens[0]
for token in tokens:
# m = re.match("(\:|\;)(\=|c|\-\o)*(\]|\)|\D|\*)*", token)
m = re.match("((?::|=|<)(?:-)?(?:\||o|O))", token)
# m2 = re.match("\<\3*", token)
if m:
counter += 1
if token in list_of_unicode_smilies:
counter += 1
if (counter > 0) and (len(tokens) > 1):
average = float(counter) / len(tokens)
sum_vector += average
else:
average = 0
sum_vector += average
if len(untagged) > 0:
average_return = sum_vector / len(untagged)
else:
average_return = sum_vector
return average_return
else:
counter = 0
list_of_unicode_smilies = [u'😐', u'😑', u'😶']
tokens = untagged.split()
#print tokens[0]
for token in tokens:
# m = re.match("(\:|\;)(\=|c|\-\o)*(\]|\)|\D|\*)*", token)
m = re.match("((?::|=|<)(?:-)?(?:\||o|O))", token)
# m2 = re.match("\<\3*", token)
if m:
counter += 1
if token in list_of_unicode_smilies:
counter += 1
if average_value == 0:
return counter
elif average_value == 1:
if (counter > 0) and (len(tokens) > 3):
average = float(counter) / len(tokens)
else:
average = 0
return average
# not used
def quotation(untagged):
counter = 0
# regex to find all quoted content
prog = re.compile(r'''".*?"''')
# count all occurrences of quoted content
counter = len(prog.findall(untagged))
# test
#if counter != 0 :
# print prog.findall(conversation)
# print counter
return counter
# count all punctuation marks, without special characters
# we use a list of unicode characters because this
# solution works better than with 'string.punctuation'
def general_punctuation_new(self, untagged, average_value=0):
if average_value == 3:
pass
else:
counter = 0
#print conversation
count = lambda l1,l2: sum([1 for x in l1 if x in l2])
counter = count(untagged,set(punctuation))
'''
if counter > 2:
print untagged
print counter
'''
if average_value == 0:
# print counter
return counter
elif average_value == 1:
if len(untagged) > 0:
result = float(counter) / len(untagged.split())
# print result
return result
else:
return 0
elif average_value == 5:
return self.counter_pre_scaling_char(counter, len(untagged))
def general_punctuation(self, untagged, average_value=0):
if average_value == 3:
pass
else:
list_of_punct = [u"\u0021", # exclamation mark
u"\u002E", # fullstop
u"\u002D", # hyphen
u"\u003B", # semicolon
u"\u0337", u"\u0338", u"\u002F",u"\u005C", # slash, solidus
u"\u003F" , # question mark
u"\u005B" ,u"\u005D" ,u"\u007B",u"\u007D",u"\u0028",u"\u0029", #brackets
u"\u0084" ,u"\u0022", u"\u00BB", u"\u00AB" # quotation marks
u"\u2024" ,u"\u2025" ,u"\u2026", # ellipsis
u"\u2012" ,u"\u2013" ,u"\u2014" ,u"\u2015" , # dash
u"\u2018" ,u"\u2019" ,u"\u2020" ,u"\u201A" ,u"\u201B" , # quotation line 1
u"\u201C" ,u"\u201D" ,u"\u201E" ,u"\u201F" ,# quotation
u"\u003A" , # colon
u"\u002C", u"\u02BD",u"\u02BB", u"\u0312",u"\u0313",u"\u0314",u"\u0315",# comma
u"\u02BC" ,u"\u0027",# apostrophe
u"\u02EE" , #double apostrophe
# special characters
u"\u2010" , u"\u2011" , u"\u2012", u"\u2013", u"\u2014",u"\u2015",
u"\u2016", u"\u2017",u"\u2018",u"\u2020",u"\u201A", u"\u201B",
u"\u201C", u"\u201D", u"\u201E", u"\u201F", u"\u2020", u"\u2021",
u"\u2022", u"\u2023", u"\u2024", u"\u2025", u"\u2026", u"\u2027",
u"\u2028", u"\u2029", u"\u2030", u"\u2031", u"\u2032", u"\u2033",
u"\u2034", u"\u2035", u"\u2036", u"\u2037", u"\u2038", u"\u2039",
u"\u2040", u"\u2041", u"\u2042", u"\u2043", u"\u2044", u"\u2045",
u"\u2046", u"\u2047", u"\u2048", u"\u2049", u"\u2050", u"\u2051",
u"\u2052", u"\u2053", u"\u2054", u"\u2055", u"\u2056", u"\u2057",
u"\u2058", u"\u2059", u"\u2060", u"\u2061", u"\u2062", u"\u2063",
u"\u2064", u"\u2065", u"\u2066", u"\u2067", u"\u2068", u"\u2069",
u"\u206A", u"\u206B", u"\u206C", u"\u206D", u"\u206E", u"\u206F",
u"\u205A", u"\u205B", u"\u205C", u"\u205D", u"\u205E", u"\u205F",
u"\u204A", u"\u204B", u"\u204C", u"\u204D", u"\u204E", u"\u204F",
u"\u203A", u"\u203B", u"\u203C", u"\u203D", u"\u203E", u"\u203F",
u"\u202A", u"\u202B", u"\u202C", u"\u202D", u"\u202E", u"\u202F"
]
counter = 0
for x in untagged:
for char in x:
# print char
if char in list_of_punct:
counter += 1
if average_value == 0:
# print counter
return counter
elif average_value == 1:
if len(untagged) > 0:
result = float(counter) / len(untagged)
# print result
return result
else:
return 0
def special_characters(self, untagged):
# get all special characters, that are not included in the general_punctuation feature
list_of_punct = [u"\u2010" , u"\u2011" , u"\u2012", u"\u2013", u"\u2014",u"\u2015",
u"\u2016", u"\u2017",u"\u2018",u"\u2020",u"\u201A", u"\u201B",
u"\u201C", u"\u201D", u"\u201E", u"\u201F", u"\u2020", u"\u2021",
u"\u2022", u"\u2023", u"\u2024", u"\u2025", u"\u2026", u"\u2027",
u"\u2028", u"\u2029", u"\u2030", u"\u2031", u"\u2032", u"\u2033",
u"\u2034", u"\u2035", u"\u2036", u"\u2037", u"\u2038", u"\u2039",
u"\u2040", u"\u2041", u"\u2042", u"\u2043", u"\u2044", u"\u2045",
u"\u2046", u"\u2047", u"\u2048", u"\u2049", u"\u2050", u"\u2051",
u"\u2052", u"\u2053", u"\u2054", u"\u2055", u"\u2056", u"\u2057",
u"\u2058", u"\u2059", u"\u2060", u"\u2061", u"\u2062", u"\u2063",
u"\u2064", u"\u2065", u"\u2066", u"\u2067", u"\u2068", u"\u2069",
u"\u206A", u"\u206B", u"\u206C", u"\u206D", u"\u206E", u"\u206F",
u"\u205A", u"\u205B", u"\u205C", u"\u205D", u"\u205E", u"\u205F",
u"\u204A", u"\u204B", u"\u204C", u"\u204D", u"\u204E", u"\u204F",
u"\u203A", u"\u203B", u"\u203C", u"\u203D", u"\u203E", u"\u203F",
u"\u202A", u"\u202B", u"\u202C", u"\u202D", u"\u202E", u"\u202F"
]
counter = 0
for x in untagged:
if x in list_of_punct:
counter += 1
#print x
#print counter
return counter
##################### Gender Preferential Features START #############################
def stylistic_ending_custom(self, untagged, average_value = 0, custom_ending=None):
# count all words that end with -able, -ful, -al, -ible, -ic, -ive, -less, -ous
## these features are introduced in http://www.aclweb.org/anthology/D10-1021
if average_value == 3:
sum_value = 0
for text in untagged:
# tokens = word_tokenize(untagged)
tokens = text.split()
counter = 0
for x in tokens:
if x.endswith(custom_ending):
counter += 1
sum_value += counter
if len(untagged) > 0:
average_inner = sum_value / len(untagged)
else:
average_inner = sum_value
return average_inner
else:
tokens = word_tokenize(untagged)
'''
counter = 0
for x in tokens:
if x.endswith(custom_ending):
counter += 1
'''
my_regex = r'(^|\b)(\w+'+custom_ending+r')(\s|$)'
custom_regex = re.compile(my_regex, re.IGNORECASE)
counter = len(custom_regex.findall(untagged))
if average_value == 0:
# print counter
return counter
elif average_value == 1:
if (counter > 0) and (len(tokens) > 0):
average = float(counter) / len(tokens)
else:
average = 0
# print average
return average
elif average_value == 5:
return self.counter_pre_scaling(counter, len(tokens))
##################### Gender Preferential Features END #############################
# calculate token/type ratio
def type_token_ratio(self, untagged, average_value = 0):
if average_value == 3:
sum_value = 0
for text in untagged:
# tokenize the conversation
# tokens = word_tokenize(text)
tokens = text.split()
relevant_tokens = []
for x in tokens:
if len(x) > 2:
relevant_tokens.append(x)
types = set(relevant_tokens)
if (len(types)) != 0:
ratio = float(float(len(types)) / float(len(relevant_tokens)) * 100)
else:
ratio = 0
sum_value += ratio
if len(untagged) > 0:
average_inner = sum_value / len(untagged)
return average_inner
else:
return sum_value
else:
# tokenize the conversation
tokens = word_tokenize(untagged)
relevant_tokens = []
for x in tokens:
if len(x) > 2:
relevant_tokens.append(x)
types = set(relevant_tokens)
if (len(types)) != 0:
ratio = float(float(len(types)) / float(len(relevant_tokens)) * 100)
else:
ratio = 0
return ratio
def amount_of_tokens(self, untagged):
splited_conversation = untagged.split()
words = []
for x in splited_conversation:
x = re.sub('[\.!\?\,\:\'\"]', '', x)
if (len(x) >= 1):
words.append(x)
#print len(splited_conversation)
return len(words)
def amount_of_types(self, untagged):
splited_conversation = untagged.split()
words = []
for x in splited_conversation:
x = re.sub('[\.!\?\,\:\'\"]', '', x)
if (len(x) >= 1):
words.append(x)
#print len(splited_conversation)
set_words = set(words)
return len(set_words)
# not used
# this feature was suggested in:
# 'Forensic Psycholinguistics. Using Language Analysis for Identifying and Assessing Offenders'
# http://diogenesllc.com/statementlinguistics.pdf
def amount_sorryWords(self, untagged, average_value=0):
if average_value == 3:
pass
else:
tokens = untagged.split()
result = re.findall(r"sorry", untagged)
if average_value == 0:
# print len(result)
return len(result)
elif average_value == 1:
if (len(result) > 0) and (len(tokens) > 0):
average = float(len(result)) / len(tokens)
else:
average = 0
# print average
return average
def average_wordlength(self, untagged, average_value=0):
# calculate average word length
if average_value == 3:
# add funct for PAN
pass
else:
if len(untagged.split()) == 0:
average_word_length = 0
else:
average_word_length = numpy.mean([len(word) for word in untagged.split()])
if isinstance(average_word_length, float):
pass
else:
average_word_length = 0
return average_word_length
def words_capitalized(self, untagged, average_value = 5):
# count capitalized words
if average_value == 3:
sum_value = 0
for text in untagged:
# tokenize the conversation
# tokens = word_tokenize(untagged)
tokens = text.split()
# amount_words = len(tokens)
counter = 0
for word in tokens:
if word[0].isupper():
counter += 1
sum_value += counter
if len(untagged) > 0:
average_inner = sum_value / len(untagged)
else:
average_inner = sum_value
return average_inner
else:
# tokenize the conversation
tokens = untagged.split()
counter = 0
for index, word in enumerate(tokens):
# print word
if word[0].isupper() and word != 'URL' and index != 0:
counter += 1
if average_value == 0:
# print counter
return counter
elif average_value == 1:
if (counter > 0) and (len(tokens) > 0):
average = float(counter) / len(tokens)
else:
average = 0
# print average
return average
elif average_value == 5:
return self.counter_pre_scaling(counter, len(tokens))
def AllCaps(self, untagged, average_value=5):
# counts words with all capital letters
# tokenize the conversation
tokens = word_tokenize(untagged)
counter = 0
for word in tokens:
if word.isupper() and not word == 'URL' and not word == 'USER': # or [URL]
counter += 1
#print word
if average_value == 0:
# print counter
return counter
elif average_value == 1:
if (counter > 0) and (len(tokens) > 0):
average = float(counter) / len(tokens)
else:
average = 0
# print average
return average
elif average_value == 5:
return self.counter_pre_scaling(counter, len(tokens))
############## Features that require Pos_tags START #########################
def count_stop_words(self, untagged, average_value = 0):
# import nltk
# nltk.download()
# print len(stopwords.words('english'))
return self.word_counter_feature(self.cachedStopWords, untagged, average_value)
def each_part_of_speech(self, tagged, average_value=0, custom_pos=None):
### all POS tags are based on TreeTagger tags
### in TreeTagger, each language has a different TAG set
if self.language == "en":
if average_value == 3:
pass
else:
splitted_text = tagged.split()
if custom_pos == "nouns":
# pos_counter = int(splitted_text.count("NN") + splitted_text.count("NNP") + splitted_text.count("NNPS") + splitted_text.count("NNS"))
list_to_find = ["NN", "NNP", "NNPS", "NNS"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "adjectives":
# pos_counter = int(splitted_text.count("JJ") + splitted_text.count("JJR") + splitted_text.count("JJS"))
list_to_find = ["JJ", "JJR", "JJS"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "determiner":
# pos_counter = int(splitted_text.count("DT") + splitted_text.count("WDT") + splitted_text.count("PDT"))
list_to_find = ["DT", "WDT", "PDT"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "conjunctions":
# pos_counter = int(splitted_text.count("CC") + splitted_text.count("IN"))
list_to_find = ["CC", "IN"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "pronouns":
# pos_counter = int(splitted_text.count("PRP") + splitted_text.count("PRP$") + splitted_text.count("WP") + splitted_text.count("WP$"))
list_to_find = ["PRP", "PRP$", "WP", "WP$"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "verbs":
# pos_counter = int(splitted_text.count("VB") + splitted_text.count("VBD") + splitted_text.count("VBG") + splitted_text.count("VBN") + splitted_text.count("VBP") + splitted_text.count("VBZ"))
list_to_find = ["VB", "VBD", "VBG", "VBN", "VBP", "VBZ"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "adverbs":
# pos_counter = int(splitted_text.count("RB") + splitted_text.count("RBR") + splitted_text.count("RBS"))
list_to_find = ["RB", "RBR", "RBS"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "modals":
# pos_counter = int(splitted_text.count("MD"))
list_to_find = ["MD"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "interjections":
# pos_counter = int(splitted_text.count("UH"))
list_to_find = ["UH"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "to_pos":
# pos_counter = int(splitted_text.count("TO"))
list_to_find = ["TO"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "cardinal_num":
# pos_counter = int(splitted_text.count("CD"))
list_to_find = ["CD"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
if average_value == 0:
return pos_counter
elif average_value == 1:
if len(splitted_text) > 0:
result = float(pos_counter) / len(splitted_text)
else:
result = 0
return result
elif average_value == 5:
return self.counter_pre_scaling(pos_counter, len(splitted_text))
elif self.language == "nl":
if average_value == 3:
pass
else:
splitted_text = tagged.split()
if custom_pos == "nouns":
# pos_counter = int(splitted_text.count("NN") + splitted_text.count("NNP") + splitted_text.count("NNPS") + splitted_text.count("NNS"))
list_to_find = ["noun*kop", "nounabbr", "nounpl", "nounprop", "nounsg"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "adjectives":
# pos_counter = int(splitted_text.count("JJ") + splitted_text.count("JJR") + splitted_text.count("JJS"))
list_to_find = ["adj", "adj*kop", "adjabbr"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "determiner":
# pos_counter = int(splitted_text.count("DT") + splitted_text.count("WDT") + splitted_text.count("PDT"))
list_to_find = ["det__demo", "prondemo", "det__art"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "conjunctions":
# pos_counter = int(splitted_text.count("CC") + splitted_text.count("IN"))
list_to_find = ["conjcoord", "conjsubo"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "pronouns":
# pos_counter = int(splitted_text.count("PRP") + splitted_text.count("PRP$") + splitted_text.count("WP") + splitted_text.count("WP$"))
list_to_find = ["pronindef", "pronpers", "pronposs", "pronquest", "pronrefl", "pronrel"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "verbs":
# pos_counter = int(splitted_text.count("VB") + splitted_text.count("VBD") + splitted_text.count("VBG") + splitted_text.count("VBN") + splitted_text.count("VBP") + splitted_text.count("VBZ"))
list_to_find = ["verbinf", "verbpapa", "verbpastpl", "verbpastsg", "verbpresp", "verbprespl", "verbpressg"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "adverbs":
# pos_counter = int(splitted_text.count("RB") + splitted_text.count("RBR") + splitted_text.count("RBS"))
list_to_find = ["adv", "advabbr", "pronadv"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "modals":
# pos_counter = int(splitted_text.count("MD"))
# here changed to particle -te in Dutch
list_to_find = ["partte"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "interjections":
# pos_counter = int(splitted_text.count("UH"))
list_to_find = ["int"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "to_pos":
# pos_counter = int(splitted_text.count("TO"))
list_to_find = ["prep", "prepabbr"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "cardinal_num":
# pos_counter = int(splitted_text.count("CD"))
list_to_find = ["num__card", "num__ord"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
if average_value == 0:
return pos_counter
elif average_value == 1:
if len(splitted_text) > 0:
result = float(pos_counter) / len(splitted_text)
else:
result = 0
return result
elif average_value == 5:
return self.counter_pre_scaling(pos_counter, len(splitted_text))
elif self.language == "es":
if average_value == 3:
pass
else:
splitted_text = tagged.split()
if custom_pos == "nouns":
# pos_counter = int(splitted_text.count("NN") + splitted_text.count("NNP") + splitted_text.count("NNPS") + splitted_text.count("NNS"))
list_to_find = ["NN", "NP", "NC", "NMEA", "PAL", "PDEL", "PE"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "adjectives":
# pos_counter = int(splitted_text.count("JJ") + splitted_text.count("JJR") + splitted_text.count("JJS"))
list_to_find = ["ADJ"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "determiner":
# pos_counter = int(splitted_text.count("DT") + splitted_text.count("WDT") + splitted_text.count("PDT"))
list_to_find = ["DM"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "conjunctions":
# pos_counter = int(splitted_text.count("CC") + splitted_text.count("IN"))
list_to_find = ["CC", "CCAD", "CQUE", "CSUBF", "CSUBI", "CSUBX"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "pronouns":
# pos_counter = int(splitted_text.count("PRP") + splitted_text.count("PRP$") + splitted_text.count("WP") + splitted_text.count("WP$"))
list_to_find = ["DM", "INT", "PPC", "PPO", "PPX", "REL"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "verbs":
# pos_counter = int(splitted_text.count("VB") + splitted_text.count("VBD") + splitted_text.count("VBG") + splitted_text.count("VBN") + splitted_text.count("VBP") + splitted_text.count("VBZ"))
list_to_find = ["VCLIger", "VCLIinf", "VCLIfin", "VEadj", "VEfin", "VEger", "VEinf", "VHadj", "VEadj", "VHfin", "VHger" , "VHinf", "VLadj", "VLfin", "VLger", "VLinf", "VSadj", "VSfin", "VSger", "VSinf"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "adverbs":
# pos_counter = int(splitted_text.count("RB") + splitted_text.count("RBR") + splitted_text.count("RBS"))
list_to_find = ["ADV"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "modals":
# pos_counter = int(splitted_text.count("MD"))
list_to_find = ["VMadj", "VMfin", "VMger", "VMinf"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "interjections":
# pos_counter = int(splitted_text.count("UH"))
list_to_find = ["ITJN"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "to_pos":
# pos_counter = int(splitted_text.count("TO"))
# here prepositions
list_to_find = ["PREP", "PREP/DEL"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
elif custom_pos == "cardinal_num":
# pos_counter = int(splitted_text.count("CD"))
list_to_find = ["CARD"]
custom_regex = re.compile(self.regex_str(list_to_find), re.IGNORECASE)
pos_counter = len(custom_regex.findall(tagged))
if average_value == 0:
return pos_counter
elif average_value == 1:
if len(splitted_text) > 0:
result = float(pos_counter) / len(splitted_text)
else: