-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict.py
238 lines (197 loc) · 5.96 KB
/
predict.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import nltk
import string
from nltk.classify import NaiveBayesClassifier
data_set = pd.read_csv("mbti_1.csv")
all_posts = pd.DataFrame()
types = np.unique(np.array(data_set["type"]))
total = data_set.groupby(["type"]).count() * 50
# Organizing data
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
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}
##### Introverted and Extroverted
# 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]
]
split = []
for i in range(16):
split += [len(features[i]) * 0.8]
split = np.array(split, dtype=int)
train = []
for i in range(16):
train += features[i][: split[i]]
IntroExtro = NaiveBayesClassifier.train(train)
#### Intution and Sensing
# 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]
]
train = []
for i in range(16):
train += features[i][: split[i]]
IntuitionSensing = NaiveBayesClassifier.train(train)
#### Thinking Feeling
# 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]
]
train = []
for i in range(16):
train += features[i][: split[i]]
ThinkingFeeling = NaiveBayesClassifier.train(train)
#### Judging Perceiving
# 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]
]
train = []
for i in range(16):
train += features[i][: split[i]]
JudgingPercieiving = NaiveBayesClassifier.train(train)
temp = {
"train": [
81.12443979837917,
70.14524215640667,
80.03456948570128,
79.79341109742592,
],
"test": [
58.20469312585358,
54.46262259027357,
59.41315234035509,
54.40549600629061,
],
}
results = pd.DataFrame.from_dict(
temp,
orient="index",
columns=[
"Introvert - Extrovert",
"Intuition - Sensing",
"Thinking - Feeling",
"Judging - Percieiving",
],
)
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
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]
# Plotting
labels = np.array(results.columns)
intj = trait1.loc["count"]
ind = np.arange(4)
width = 0.4
fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, intj, width, color="royalblue")
esfp = trait2.loc["count"]
rects2 = ax.bar(ind + width, esfp, width, color="seagreen")
fig.set_size_inches(10, 7)
ax.set_xlabel("Finding the MBTI Trait", size=18)
ax.set_ylabel("Trait Percent (%)", size=18)
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(labels)
ax.set_yticks(np.arange(0, 105, step=10))
ax.set_title("Your Personality is " + YourTrait, size=20)
plt.grid(True)
fig.savefig(name + ".png", dpi=200)
plt.show()
return traasits
writings = open("charlotte.txt")
writing = writings.readlines()
writing = [line.rstrip() for line in writing]
print(tellmemyMBTI(writing, "Charlotte"))