-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbow.py
143 lines (102 loc) · 4.88 KB
/
bow.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
#!/usr/bin/env python
# BOW + linear regression
import os
import pandas as pd
import numpy as np
import argparse
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from scipy.sparse import hstack
from utility import hatebase_features
from sk_classify import run_grid_search
from nltk.stem.porter import *
DATA_PATH = 'data/twitter_davidson/labeled_data_cleaned.csv'
parser = argparse.ArgumentParser(description='Run bag of words baselines.')
parser.add_argument("--clf", default='lr')
parser.add_argument("--scoring", default='roc_auc')
parser.add_argument('-hb', dest='hatebase', action='store_const',
const=True, default=False)
parser.add_argument('-stem', dest='stem', action='store_const',
const=True, default=False)
parser.add_argument('--word', dest='word', action='store_const',
const=True, default=True)
parser.add_argument('--hb_only', dest='hb_only', action='store_const',
const=True, default=False)
# parser.add_argument('--ngram', dest='ngram', action='store_const',
# const=True, default=False)
# parser.add_argument('--chargram', dest='char', action='store_const',
# const=True, default=False)
# parser.add_argument('--tfidf', dest='tfidf', action='store_const',
# const=True, default=False)
# parser.add_argument('-a', dest='all', action='store_const',
# const=True, default=False)
parser.add_argument('--multi', dest='multi', action='store_const',
const=True, default=False)
parser.add_argument('--data', dest='datafile', default=DATA_PATH)
args = parser.parse_args()
#
def basic_tokenizer(sentence):
tokens = sentence.strip().split() #basic tokenizer
return [w.rstrip(' ?:!,;.()-_') for w in tokens if w.rstrip(' ?:!,;.()-_')]
def stem_tokenizer(sentence):
tokens = basic_tokenizer(sentence)
stemmer = PorterStemmer()
stemmed_tokens = [stemmer.stem(t) for t in tokens]
return stemmed_tokens
#
data = pd.read_csv( args.datafile, header = 0, quoting = 0,
dtype = {'hate_speech': np.int32, 'offensive_language': np.int32, 'neither': np.int32, 'class': np.int32} )
data_raw = data['tweet']
tok = stem_tokenizer if args.stem else basic_tokenizer
if args.hatebase:
print "Generating hatebase features..."
hb_features = hatebase_features( data_raw.values.astype('U'), sparse=True, tokenizer=tok )
# Helper functions
def transform_data( vectorizer, data_raw ):
if args.hb_only:
return hb_features
features = vectorizer.fit_transform( data_raw.values.astype('U') )
if args.hatebase:
features = hstack((features, hb_features))
return features
#
if args.word or args.all:
print "Creating the bag of words..."
vectorizer = CountVectorizer( analyzer = "word", tokenizer = tok, preprocessor = None,
stop_words = None )
data_x = transform_data(vectorizer, data_raw )
if args.multi:
print data_x
run_grid_search( data_x, data['class'], args.clf, args.scoring )
else:
run_grid_search( data_x, data['hate_speech'], args.clf, args.scoring )
#
# if args.ngram or args.all:
# print "Creating the bag of n=1,2,3 grams..."
# vectorizer = CountVectorizer( analyzer = "word", ngram_range=(1,3), tokenizer = None, preprocessor = None,
# stop_words = None )
# train_data_features, test_data_features = transform_data(vectorizer, train_raw, test_raw )
# if args.multi:
# train_and_eval_auc( train_data_features, train['class'], test_data_features, test['class'].values )
# else:
# train_and_eval_auc( train_data_features, train['hate_speech'], test_data_features, test['hate_speech'].values )
# #
# if args.char or args.all:
# print "Creating the bag of character n grams..."
# vectorizer = CountVectorizer( analyzer = "char_wb", ngram_range=(3,5), tokenizer = None, preprocessor = None,
# stop_words = None )
# train_data_features, test_data_features = transform_data(vectorizer, train_raw, test_raw )
# if args.multi:
# train_and_eval_auc( train_data_features, train['class'], test_data_features, test['class'].values )
# else:
# train_and_eval_auc( train_data_features, train['hate_speech'], test_data_features, test['hate_speech'].values )
# #
# if args.tfidf or args.all:
# print "Vectorizing: TF-IDF"
# vectorizer = TfidfVectorizer( ngram_range = ( 1, 3 ), sublinear_tf = True )
# train_data_features, test_data_features = transform_data(vectorizer, train_raw, test_raw )
# if args.multi:
# train_and_eval_auc( train_data_features, train['class'], test_data_features, test['class'].values )
# else:
# train_and_eval_auc( train_data_features, train['hate_speech'], test_data_features, test['hate_speech'].values )