-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunigram_logit_classifier.py
153 lines (126 loc) · 5.83 KB
/
unigram_logit_classifier.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
from nltk import word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import precision_recall_fscore_support
from utilities.twokenize import tokenize
import numpy as np
import pandas as pd
import gzip
import glob
import json
import codecs
import sys, os
import cPickle as pickle
from utilities.wordSeparator import InitializeWords
reader = codecs.getreader("utf-8")
def read_corpus(inputDir):
'''
Reads tweets text from compressed files and returns a dict of user
and their tweets. Dict format:
key : user_id
value: list of tweets
'''
corpus = {}
for filename in glob.glob(inputDir+'/*.gz'):
user_id = filename.split('/')[-1].split('.')[0]
user_tweets_text = []
with gzip.open(filename, 'rb') as f:
for line in reader(f):
line = line.replace('\n','')
text_json = json.loads(line)['text']
user_tweets_text.append(text_json)
corpus[user_id] = user_tweets_text
return corpus
def nltk_tokenize(text):
tokens = word_tokenize(text)
return tokens
def tweet_tokenizer(text, wordlist, cleanup = 0): #cleanup = 3 is suggested
#tokens = tokenize(text)
#return tokens
return tokenize(text, wordlist, cleanup)
def get_train_test_split_text_features(k, k_fold_features, k_fold_labels):
X_train = sum(k_fold_features[:k]+k_fold_features[k+1:],[])
Y_train = sum(k_fold_labels[:k]+k_fold_labels[k+1:], [])
X_test= k_fold_features[k]
Y_test = k_fold_labels[k]
return X_train, Y_train, X_test, Y_test
def generate_k_fold_split(file_path):
reference_df = pd.read_csv(file_path)
reference_df = reference_df.set_index('anonymized_name')
return {k:reference_df[reference_df['fold']==k].index.tolist() for k in range(10)}
def get_text_features(k_folds_users_split, control_corpus, sch_corpus):
k_fold_features = []
k_fold_labels = []
for k in range(10):
users = k_folds_users_split[k]
features = []
labels = []
for user in users:
if user in control_corpus.keys():
features.append(' '.join(control_corpus[user]))
labels.append(0)
elif user in sch_corpus.keys():
features.append(' '.join(sch_corpus[user]))
labels.append(1)
k_fold_features.append(features)
k_fold_labels.append(labels)
return k_fold_features, k_fold_labels
def get_train_test_split_non_text_features(k, k_folds_users_split, features_df):
k_fold_users = [k_folds_users_split[i] for i in range(10)]
train_users = sum(k_fold_users[:k]+k_fold_users[k+1:],[])
test_users = k_fold_users[k]
train_df = features_df.ix[train_users]
test_df = features_df.ix[test_users]
return train_df, test_df
if __name__ == '__main__':
if len(sys.argv) < 5:
print("provide anonymized_control_tweets folder path, anonymized_schizophrenia_tweets folder path "\
"anonymized_user_manifest.csv path and normalized_sch_liwc_count.csv path")
print("Example: python unigram_logit_classifier.py anonymized_control_tweets "\
"anonymized_schizophrenia_tweets anonymized_user_manifest.csv normalized_sch_liwc_count.csv")
sys.exit()
control_folder_path = sys.argv[1]
sch_folder_path = sys.argv[2]
reference_file_path = sys.argv[3]
liwc_file_path = sys.argv[4]
control_corpus_dict = read_corpus(control_folder_path)
sch_corpus_dict = read_corpus(sch_folder_path)
k_folds_users_split = generate_k_fold_split(reference_file_path)
k_fold_text_features, k_fold_labels = get_text_features(k_folds_users_split, control_corpus_dict, sch_corpus_dict)
liwc_count_df = pd.read_csv(liwc_file_path)
liwc_count_df.set_index('Unnamed: 0', inplace=True)
scores = []
print ('Precision, Recall , F1-score')
if os.path.isfile('utilities/wordseparator.pickle'):
with open('utilities/wordseparator.pickle') as f:
wordlist = pickle.load(f)
else:
wordlisttxt = 'utilities/englishWords.txt' # A file containing common english words #assumes we are running from the top directory (schizophreniaThroughTweets)
wordlist = InitializeWords(wordlisttxt)
with open('utilities/wordseparator.pickle', 'w') as f:
pickle.dump(wordlist, f)
for k in range(10):
X_train, y_train, X_test, y_test = get_train_test_split_text_features(k, k_fold_text_features, k_fold_labels)
# discard all tokens which are present in more than 50% tweets or in less than 5 tweets.
#vectorize = TfidfVectorizer(tokenizer= tweet_tokenizer, ngram_range=(1, 1), binary=True, max_features=1000,
# min_df=5, max_df= 0.50)
vectorize = TfidfVectorizer(tokenizer=lambda text:tweet_tokenizer(text,wordlist,0), ngram_range=(1, 1), binary=True, max_features=1000,
min_df=5, max_df= 0.50)
train_data_features = vectorize.fit_transform(X_train)
test_data_features = vectorize.transform(X_test)
# #generate liwc features
# train_df, test_df = get_train_test_split_non_text_features(k, k_folds_users_split, liwc_count_df)
#
# #append unigram and liwc features.
# train_data_features = np.hstack([train_data_features.todense(), train_df])
# test_data_features = np.hstack([test_data_features.todense(), test_df])
#clf = LogisticRegression()
clf = SVC(kernel='linear')
clf.fit(train_data_features, y_train)
predicted_values = clf.predict(test_data_features)
scr = precision_recall_fscore_support(y_test, predicted_values, average='binary')
print (scr)
scores.append(scr)
print('Mean F-1 score')
print (np.mean([s[2] for s in scores]))