-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainSet.py
53 lines (41 loc) · 1.43 KB
/
trainSet.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
#!/usr/bin/env python
import collections
import pandas as pd
import csv
import re
trSet = pd.read_csv('dataset/train-set/trainSet.csv')
DICHOTOMY = ('IE', 'NS', 'TF', 'PJ')
usersByTypes = collections.defaultdict(int)
def writeCSVFile(feature, list):
with open('dataset/train-set/train' + feature + '.csv', 'w', newline='', encoding="utf-8") as f:
writer = csv.writer(f)
for fiftyPosts in list:
row = [post for post in fiftyPosts if
('http' not in post) and (post != '') and (post is not None) and (re.search("[a-zA-Z]", post))]
if len(row) > 10:
writer.writerow(row)
for dichotomy in DICHOTOMY:
d1, d2 = dichotomy
for mbtiType in trSet['type']:
if d1 in mbtiType:
usersByTypes[d1] += 1
if d2 in mbtiType:
usersByTypes[d2] += 1
for dichotomy in DICHOTOMY:
d1, d2 = dichotomy
limit = min(usersByTypes[d1], usersByTypes[d2])
listD1 = []
listD2 = []
countD1 = 0
countD2 = 0
for user in trSet.iterrows():
if d1 in user[1]['type'] and countD1 < limit:
listD1.append(user[1]['posts'].split('|||'))
countD1 += 1
if d2 in user[1]['type'] and countD2 < limit:
listD2.append(user[1]['posts'].split('|||'))
countD2 += 1
if countD1 >= limit and countD2 >= limit:
break
writeCSVFile(d1, listD1)
writeCSVFile(d2, listD2)