-
Notifications
You must be signed in to change notification settings - Fork 0
/
funTry.py
94 lines (82 loc) · 2.69 KB
/
funTry.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
from tqdm import tqdm
import os
import twint
import pandas as pd
import numpy as np
from collections import defaultdict
from collections import Counter
import csv
from sklearn.naive_bayes import BernoulliNB
def getFollowing(username):
works = 0
fails = 0
c = twint.Config()
c.Username = username
c.Pandas = True
try:
twint.run.Following(c)
listOfFollowing = twint.storage.panda.Follow_df
#listOfFollowing = listOfFollowing.reindex(index=listOfFollowing.index[::-1])
#print(listOfFollowing)
#listOfFollowing.to_csv(r'/Users/teaganjohnson/Desktop/TwitterFinalProject/.csv', header=None, index=None, sep=' ', mode='a')
return listOfFollowing["following"][username]
except Exception:
return ""
def createVector(person, columnsVector):
followerVector = []
followTrump = False
followBiden = False
listOfFollowing = getFollowing(person)
totalCount = 0
if "realDonaldTrump" in set(listOfFollowing):
followTrump = True
if "JoeBiden" in set(listOfFollowing):
followBiden = True
for account in columnsVector:
if account in listOfFollowing:
totalCount+=1
followerVector.append(1)
else:
followerVector.append(0)
return followerVector, followTrump, followBiden, totalCount
topTeamsListMessy = np.load('topTeamsList.npy')
def cleanTeamsList(teamsList):
cleanList = []
for user in teamsList:
cleanUser = user[2:-3]
cleanList.append(cleanUser)
return cleanList
topTeamsList = cleanTeamsList(topTeamsListMessy)
matrix = np.load('matrix.npy')
targetArray = np.load('targetArray.npy')
model = BernoulliNB()
model.fit(matrix, targetArray)
input = input("Username: ")
personVector, followTrump, followBiden, totalCount = createVector(input, topTeamsList)
predictionProba = model.predict_proba(np.array(personVector).reshape(1, -1))
for num in predictionProba:
predictionProb = num
predictionProb = list(predictionProb)
if predictionProb[0] < .01:
predictionProb[0] = 0.0
if predictionProb[1] < .01:
predictionProb[1] = 0.0
print()
print()
print()
print("You searched for: ", input)
print()
print()
if totalCount == 0:
print("Oh no! This user doesn't follow any of our top users. Our model will not be accurate")
elif 0 < totalCount:
print("This acount follows ", totalCount, "of our top 102 accounts!")
print()
print()
print("This account has a ", predictionProb[1], "% chance of following Trump's Twitter account!")
print("This account has a ", predictionProb[0], "% chance of following Biden's Twitter account!")
print()
print("Do they follow Trump? ", followTrump)
print("Do they follow Biden? ", followBiden)
print()
print()