-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_features.py
More file actions
74 lines (54 loc) · 2.07 KB
/
extract_features.py
File metadata and controls
74 lines (54 loc) · 2.07 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
import os, sys
import enchant
from tqdm import tqdm
from pprint import pprint
parent_dir = os.path.abspath(os.path.dirname(__file__))
vendor_dir = os.path.join(parent_dir, 'vendor')
sys.path.append(vendor_dir)
from textblob import TextBlob
def _count_possible_misspellings(d, words):
return sum(not d.check(word.string) for word in words)
def extract_all_features(texts, report=False):
d = enchant.Dict("en_US")
features_list = []
print("Starting feature extraction...")
for text in tqdm(texts):
features = {}
tb = TextBlob(text)
words = tb.words
sentences = tb.sentences
char_length = len(text)
word_length = len(words)
sentences_length = len(sentences)
features['subjectivity'] = tb.sentiment.subjectivity
features['count_personal_pronouns'] = sum(tag[1] == 'PRP' for tag in tb.tags)
features['count_proper_noun'] = sum(tag[1] in ['NNP', 'NNPS'] for tag in tb.tags)
features['avg_word_length'] = char_length/word_length
features['length'] = word_length
features['count_misspellings'] = _count_possible_misspellings(d, words)
features['count_of_sentences'] = sentences_length
features['avg_sentence_length'] = char_length/sentences_length
features_list.append(features)
if report:
print("\nText:")
print(text)
print("Features:")
pprint(features)
return features_list;
def extract_most_performant_features(texts, report=False):
d = enchant.Dict("en_US")
features_list = []
print("Starting feature extraction...")
for text in tqdm(texts):
features = {}
tb = TextBlob(text)
words = tb.words
features['count_personal_pronouns'] = sum(tag[1] == 'PRP' for tag in tb.tags)
features['count_misspellings'] = _count_possible_misspellings(d, words)
features_list.append(features)
if report:
print("\nText:")
print(text)
print("Features:")
pprint(features)
return features_list;