-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment1.py
62 lines (46 loc) · 1.68 KB
/
assignment1.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
import argparse
from utils import read_hate_tweets
from evaluation import accuracy, f_1
from model.naivebayes import NaiveBayes
from model.logreg import LogReg
from helper import train_smooth, train_feature_eng, train_logreg
TWEETS_ANNO = './data/NAACL_SRW_2016.csv'
TWEETS_TEXT = './data/NAACL_SRW_2016_tweets.json'
MODEL_DICT = {'naive-bayes': NaiveBayes, 'logreg': LogReg}
def main():
parser = argparse.ArgumentParser(
description='Train naive bayes or logistic regression'
)
parser.add_argument(
'--model', dest='model',
choices=['naive-bayes', 'logreg'],
help='{naive-bayes, logreg}', type=str,
required=True
)
parser.add_argument(
'--test_smooth', dest='test_smooth',
help='Train and test Naive Bayes with varying smoothing parameter k',
action='store_true'
)
parser.add_argument(
'--feature_eng', dest='feature_eng',
help='Train and test Naive Bayes with different feature types',
action='store_true'
)
args = parser.parse_args()
(train_data, test_data) = read_hate_tweets(TWEETS_ANNO, TWEETS_TEXT)
model = MODEL_DICT[args.model]
if args.model == 'naive-bayes':
print("Training naive bayes classifier...")
nb = model.train(train_data)
print("Accuracy: ", accuracy(nb, test_data))
print("F_1: ", f_1(nb, test_data))
if args.test_smooth:
train_smooth(train_data, test_data)
if args.feature_eng:
train_feature_eng(train_data, test_data)
else:
print("Training logistic regression classifier...")
train_logreg(train_data, test_data)
if __name__ == "__main__":
main()