-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonalityclassifier.py
531 lines (302 loc) · 10.9 KB
/
personalityclassifier.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# This programme will classify people into mbti personality types based on their past 50 posts on social media using the basic naivebayesclassifier
# In[1]:
import pandas as pd
from streamlit import st
import numpy as np
import matplotlib.pyplot as plt
import nltk
import string
from nltk.classify import NaiveBayesClassifier
# ### Importing the dataset
# In[2]:
data_set = pd.read_csv("C:/Users/ADMIN/Desktop/project/mbti_1.csv")
data_set.tail()
# ### Checking the dataset for missing values
# In[3]:
data_set.isnull().any()
# ## Exploring the dataset
# The size of the dataset
# In[4]:
data_set.shape
# Explroing the posts in posts field
# In[5]:
data_set.iloc[0,1].split('|||')
# Finding the number of posts
# In[6]:
len(data_set.iloc[1,1].split('|||'))
# Finding the unique vales from type of personality column
# In[7]:
types = np.unique(np.array(data_set['type']))
types
# The total number of posts for each type
# In[8]:
total = data_set.groupby(['type']).count()*50
total
# In[10]:
all_posts= pd.DataFrame()
for j in types:
temp1 = data_set[data_set['type']==j]['posts']
temp2 = []
for i in temp1:
temp2+=i.split('|||')
temp3 = pd.Series(temp2)
all_posts[j] = temp3
# In[11]:
all_posts.tail()
# ### Creating a function to tokenize the words
# In[12]:
useless_words = nltk.corpus.stopwords.words("english") + list(string.punctuation)
def build_bag_of_words_features_filtered(words):
words = nltk.word_tokenize(words)
return {
word:1 for word in words \
if not word in useless_words}
# A random check of the function
# In[13]:
build_bag_of_words_features_filtered(all_posts['INTJ'].iloc[1])
# ## Creating an array of features
# In[14]:
features=[]
for j in types:
temp1 = all_posts[j]
temp1 = temp1.dropna() #not all the personality types have same number of files
features += [[(build_bag_of_words_features_filtered(i), j) for i in temp1]]
# Because each number of personality types have different number of posts they must be splitted accordingle. Taking 80% for training and 20% for testing
# In[15]:
split=[]
for i in range(16):
split += [len(features[i]) * 0.8]
split = np.array(split,dtype = int)
# In[16]:
split
# Data for training
# In[17]:
train=[]
for i in range(16):
train += features[i][:split[i]]
# Training the model
# In[18]:
sentiment_classifier = NaiveBayesClassifier.train(train)
# Testing the model on the dataset it was trained for accuracy
# In[19]:
nltk.classify.util.accuracy(sentiment_classifier, train)*100
# Creating the test data
# In[20]:
test=[]
for i in range(16):
test += features[i][split[i]:]
# Testing the model on the test dataset which it has never seen before
# In[21]:
nltk.classify.util.accuracy(sentiment_classifier, test)*100
# In[22]:
# Features for the bag of words model
features=[]
for j in types:
temp1 = all_posts[j]
temp1 = temp1.dropna() #not all the personality types have same number of files
if('I' in j):
features += [[(build_bag_of_words_features_filtered(i), 'introvert') for i in temp1]]
if('E' in j):
features += [[(build_bag_of_words_features_filtered(i), 'extrovert') for i in temp1]]
# Data for training
# In[23]:
train=[]
for i in range(16):
train += features[i][:split[i]]
# Training the model
# In[24]:
IntroExtro = NaiveBayesClassifier.train(train)
# Testing the model on the dataset it was trained for accuracy
# In[25]:
nltk.classify.util.accuracy(IntroExtro, train)*100
# Creating the test data
# In[26]:
test=[]
for i in range(16):
test += features[i][split[i]:]
# Testing the model on the test dataset which it has never seen before
# In[27]:
nltk.classify.util.accuracy(IntroExtro, test)*100
# Seeing that this model has good somewhat good results, I shall repeat the same with the rest of the traits
# ## Creating a classifyer for Intuition (N) and Sensing (S)
# In[28]:
# Features for the bag of words model
features=[]
for j in types:
temp1 = all_posts[j]
temp1 = temp1.dropna() #not all the personality types have same number of files
if('N' in j):
features += [[(build_bag_of_words_features_filtered(i), 'Intuition') for i in temp1]]
if('E' in j):
features += [[(build_bag_of_words_features_filtered(i), 'Sensing') for i in temp1]]
# Data for training
# In[29]:
train=[]
for i in range(16):
train += features[i][:split[i]]
# Training the model
# In[30]:
IntuitionSensing = NaiveBayesClassifier.train(train)
# Testing the model on the dataset it was trained for accuracy
# In[31]:
nltk.classify.util.accuracy(IntuitionSensing, train)*100
# Creating the test data
# In[32]:
test=[]
for i in range(16):
test += features[i][split[i]:]
# Testing the model on the test dataset which it has never seen before
# In[33]:
nltk.classify.util.accuracy(IntuitionSensing, test)*100
# ## Creating a classifyer for Thinking (T) and Feeling (F)
# In[34]:
# Features for the bag of words model
features=[]
for j in types:
temp1 = all_posts[j]
temp1 = temp1.dropna() #not all the personality types have same number of files
if('T' in j):
features += [[(build_bag_of_words_features_filtered(i), 'Thinking') for i in temp1]]
if('F' in j):
features += [[(build_bag_of_words_features_filtered(i), 'Feeling') for i in temp1]]
# Data for training
# In[35]:
train=[]
for i in range(16):
train += features[i][:split[i]]
# Training the model
# In[36]:
ThinkingFeeling = NaiveBayesClassifier.train(train)
# Testing the model on the dataset it was trained for accuracy
# In[37]:
nltk.classify.util.accuracy(ThinkingFeeling, train)*100
# Creating the test data
# In[38]:
test=[]
for i in range(16):
test += features[i][split[i]:]
# Testing the model on the test dataset which it has never seen before
# In[39]:
nltk.classify.util.accuracy(ThinkingFeeling, test)*100
# ## Creating a classifyer for Judging (J) and Percieving (P)
# In[40]:
# Features for the bag of words model
features=[]
for j in types:
temp1 = all_posts[j]
temp1 = temp1.dropna() #not all the personality types have same number of files
if('J' in j):
features += [[(build_bag_of_words_features_filtered(i), 'Judging') for i in temp1]]
if('P' in j):
features += [[(build_bag_of_words_features_filtered(i), 'Percieving') for i in temp1]]
# Data for training
# In[41]:
train=[]
for i in range(16):
train += features[i][:split[i]]
# Training the model
# In[42]:
JudgingPercieiving = NaiveBayesClassifier.train(train)
# Testing the model on the dataset it was trained for accuracy
# In[43]:
nltk.classify.util.accuracy(JudgingPercieiving, train)*100
# Creating the test data
# In[44]:
test=[]
for i in range(16):
test += features[i][split[i]:]
# Testing the model on the test dataset which it has never seen before
# In[45]:
nltk.classify.util.accuracy(JudgingPercieiving, test)*100
# In[192]:
def MBTI(input):
tokenize = build_bag_of_words_features_filtered(input)
ie = IntroExtro.classify(tokenize)
Is = IntuitionSensing.classify(tokenize)
tf = ThinkingFeeling.classify(tokenize)
jp = JudgingPercieiving.classify(tokenize)
mbt = ''
if(ie == 'introvert'):
mbt+='I'
if(ie == 'extrovert'):
mbt+='E'
if(Is == 'Intuition'):
mbt+='N'
if(Is == 'Sensing'):
mbt+='S'
if(tf == 'Thinking'):
mbt+='T'
if(tf == 'Feeling'):
mbt+='F'
if(jp == 'Judging'):
mbt+='J'
if(jp == 'Percieving'):
mbt+='P'
return(mbt)
# ### Building another functions that takes all of my posts as input and outputs the graph showing percentage of each trait seen in each posts and sums up displaying your personality as the graph title
#
# **Note:** The input should be an array of your posts
# In[243]:
def tellmemyMBTI(input, name, traasits=[]):
a = []
trait1 = pd.DataFrame([0,0,0,0],['I','N','T','J'],['count'])
trait2 = pd.DataFrame([0,0,0,0],['E','S','F','P'],['count'])
for i in input:
a += [MBTI(i)]
for i in a:
for j in ['I','N','T','J']:
if(j in i):
trait1.loc[j]+=1
for j in ['E','S','F','P']:
if(j in i):
trait2.loc[j]+=1
trait1 = trait1.T
trait1 = trait1*100/len(input)
trait2 = trait2.T
trait2 = trait2*100/len(input)
#Finding the personality
YourTrait = ''
for i,j in zip(trait1,trait2):
temp = max(trait1[i][0],trait2[j][0])
if(trait1[i][0]==temp):
YourTrait += i
if(trait2[j][0]==temp):
YourTrait += j
traasits +=[YourTrait]
desc=''.join(YourTrait)
if desc == 'ISTJ':
result= "ISTJs are reliable and responsible, known for their practical and organized approach to tasks."
elif desc == 'INTJ':
result= "INTJs are strategic and analytical thinkers, often seen as the 'architects' of ideas."
elif desc == 'INTP':
result= "INTPs are curious and inventive thinkers, known for their logical and abstract reasoning."
elif desc == 'ENTJ':
result= "ENTJs are natural leaders, decisive and assertive, often taking charge of situations."
elif desc == 'ENTP':
result= "ENTPs are innovative and adaptable, enjoying exploring new possibilities and ideas."
elif desc == 'INFJ':
result= "INFJs are empathetic and insightful, often referred to as the 'advocates' for their idealistic nature."
elif desc == 'INFP':
result= "INFPs are creative and compassionate, driven by their values and a desire for authenticity."
elif desc == 'ENFJ':
result= "ENFJs are charismatic and caring leaders, dedicated to bringing out the best in others."
elif desc == 'ENFP':
result= "ENFPs are enthusiastic and imaginative, always seeking new connections and experiences."
elif desc == 'ISFJ':
result= "ISFJs are supportive and nurturing, often putting others' needs before their own."
elif desc == 'ESTJ':
result= "ESTJs are efficient and decisive leaders, valuing order and structure in their environments."
elif desc == 'ESFJ':
result= "ESFJs are sociable and caring, often taking on the role of 'nurturers' in social groups."
elif desc == 'ISTP':
result= "ISTPs are pragmatic and adventurous, excelling in hands-on problem-solving and exploration."
elif desc == 'ISFP':
result= "ISFPs are artistic and sensitive, driven by a desire for self-expression and authenticity."
elif desc == 'ESTP':
result= "ESTPs are energetic and action-oriented, thriving in dynamic and fast-paced environments."
elif desc == 'ESFP':
result= "ESFPs are spontaneous and sociable, enjoying the present moment and engaging with others."
else:
result= "Invalid MBTI type code"
print('The Personality Trait is: '+result)
return(result)