-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeature_extraction.py
More file actions
161 lines (132 loc) · 7.69 KB
/
Copy pathfeature_extraction.py
File metadata and controls
161 lines (132 loc) · 7.69 KB
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
import re
import sys
from nltk import TweetTokenizer
from scipy.stats import kurtosis, skew
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from text_processing import TextProcessing
from utils import Utils
from lexical_features import lexical_es, lexical_en
class FeatureExtraction(BaseEstimator, TransformerMixin):
def __init__(self, lang='es'):
try:
self.lexical = lexical_es if lang == 'es' else lexical_en
except Exception as e:
Utils.standard_error(sys.exc_info())
print('Error FeatureExtraction: {0}'.format(e))
def fit(self, x, y=None):
return self
def transform(self, list_messages):
try:
result = self.get_features(list_messages)
return result
except Exception as e:
Utils.standard_error(sys.exc_info())
print('Error transform: {0}'.format(e))
def get_features(self, messages: str):
try:
features = list(abs(self.get_features_lexical(messages)))
result = np.array(features, dtype=np.float32)
return result
except Exception as e:
Utils.standard_error(sys.exc_info())
print('Error get_features: {0}'.format(e))
return None
def get_features_lexical(self, message):
result = None
try:
lexical = self.lexical
text_tokenizer = TweetTokenizer()
tags = ('mention', 'url', 'hashtag', 'emoji', 'rt')
vector = dict()
tokens_text = text_tokenizer.tokenize(message)
if len(tokens_text) > 0:
vector['weighted_position'], vector['weighted_normalized'] = self.weighted_position(tokens_text)
vector['label_mention'] = float(sum(1 for word in tokens_text if word == 'mention'))
vector['label_url'] = float(sum(1 for word in tokens_text if word == 'url'))
vector['label_hashtag'] = float(sum(1 for word in tokens_text if word == 'hashtag'))
vector['label_emoji'] = float(sum(1 for word in tokens_text if word == 'emoji'))
vector['label_retweets'] = float(sum(1 for word in tokens_text if word == 'rt'))
vector['lexical_diversity'] = self.lexical_diversity(message)
label_word = vector['label_mention'] + vector['label_url'] + vector['label_hashtag']
label_word = label_word + vector['label_emoji'] + vector['label_retweets']
vector['label_word'] = float(len(tokens_text) - label_word)
vector['first_person_singular'] = float(
sum(1 for word in tokens_text if word in lexical['first_person_singular']))
vector['second_person_singular'] = float(
sum(1 for word in tokens_text if word in lexical['second_person_singular']))
vector['third_person_singular'] = float(
sum(1 for word in tokens_text if word in lexical['third_person_singular']))
vector['first_person_plurar'] = float(
sum(1 for word in tokens_text if word in lexical['first_person_plurar']))
vector['second_person_plurar'] = float(
sum(1 for word in tokens_text if word in lexical['second_person_plurar']))
vector['third_person_plurar'] = float(
sum(1 for word in tokens_text if word in lexical['third_person_plurar']))
vector['avg_word'] = np.nanmean([len(word) for word in tokens_text if word not in tags])
vector['avg_word'] = vector['avg_word'] if not np.isnan(vector['avg_word']) else 0.0
vector['avg_word'] = round(vector['avg_word'], 4)
vector['kur_word'] = kurtosis([len(word) for word in tokens_text if word not in tags])
vector['kur_word'] = vector['kur_word'] if not np.isnan(vector['kur_word']) else 0.0
vector['kur_word'] = round(vector['kur_word'], 4)
vector['skew_word'] = skew(np.array([len(word) for word in tokens_text if word not in tags]))
vector['skew_word'] = vector['skew_word'] if not np.isnan(vector['skew_word']) else 0.0
vector['skew_word'] = round(vector['skew_word'], 4)
# adverbios
vector['adverb_neg'] = sum(1 for word in tokens_text if word in lexical['adverb_neg'])
vector['adverb_neg'] = float(vector['adverb_neg'])
vector['adverb_time'] = sum(1 for word in tokens_text if word in lexical['adverb_time'])
vector['adverb_time'] = float(vector['adverb_time'])
vector['adverb_place'] = sum(1 for word in tokens_text if word in lexical['adverb_place'])
vector['adverb_place'] = float(vector['adverb_place'])
vector['adverb_mode'] = sum(1 for word in tokens_text if word in lexical['adverb_mode'])
vector['adverb_mode'] = float(vector['adverb_mode'])
vector['adverb_cant'] = sum(1 for word in tokens_text if word in lexical['adverb_cant'])
vector['adverb_cant'] = float(vector['adverb_cant'])
vector['adverb_all'] = float(vector['adverb_neg'] + vector['adverb_time'] + vector['adverb_place'])
vector['adverb_all'] = float(vector['adverb_all'] + vector['adverb_mode'] + vector['adverb_cant'])
vector['adjetives_neg'] = sum(1 for word in tokens_text if word in lexical['adjetives_neg'])
vector['adjetives_neg'] = float(vector['adjetives_neg'])
vector['adjetives_pos'] = sum(1 for word in tokens_text if word in lexical['adjetives_pos'])
vector['adjetives_pos'] = float(vector['adjetives_pos'])
vector['who_general'] = sum(1 for word in tokens_text if word in lexical['who_general'])
vector['who_general'] = float(vector['who_general'])
vector['who_male'] = sum(1 for word in tokens_text if word in lexical['who_male'])
vector['who_male'] = float(vector['who_male'])
vector['who_female'] = sum(1 for word in tokens_text if word in lexical['who_female'])
vector['who_female'] = float(vector['who_female'])
result = np.array(list(vector.values()))
except Exception as e:
Utils.standard_error(sys.exc_info())
print('Error get_lexical_features: {0}'.format(e))
return result
@staticmethod
def lexical_diversity(text):
result = None
try:
text_out = re.sub(r"[\U00010000-\U0010ffff]", '', text)
text_out = re.sub(
r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+'
r'|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))',
'', text_out)
text_out = text_out.lower()
result = round((len(set(text_out)) / len(text_out)), 4)
except Exception as e:
Utils.standard_error(sys.exc_info())
print('Error lexical_diversity: {0}'.format(e))
return result
@staticmethod
def weighted_position(tokens_text):
result = None
try:
size = len(tokens_text)
weighted_words = 0.0
weighted_normalized = 0.0
for w in tokens_text:
weighted_words += 1 / (1 + tokens_text.index(w))
weighted_normalized += (1 + tokens_text.index(w)) / size
result = (weighted_words, weighted_normalized)
except Exception as e:
Utils.standard_error(sys.exc_info())
print('Error weighted_position: {0}'.format(e))
return result