-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (125 loc) · 4.92 KB
/
Copy pathmain.py
File metadata and controls
140 lines (125 loc) · 4.92 KB
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
# Kyle Muncie
# CSE472 Project 1
# Importing
import matplotlib.pyplot as plt
import networkx as nx
import snscrape.modules.twitter as sntwitter
import re
# resources: followed tutorial on https://medium.com/dataseries/how-to-scrape-millions-of-tweets-using-snscrape
# -195ee3594721 to scrape
def kyle():
# Creating global variables for the amount of tweets I want to pull, and also two arrays for the tweets
maxTweets = 200
proVaccineTweets = []
antiVaxTweets = []
# finding the pro-vaccine tweets within the last week
for i, tweet in enumerate(
sntwitter.TwitterSearchScraper('#GetVaccinated since:2022-09-14 until:2022-09-21').get_items()):
if i > maxTweets:
break
proVaccineTweets.append(tweet.content)
# Finding the anti-vaccines tweets withing the last week
for i, tweet in enumerate(
sntwitter.TwitterSearchScraper('#antivax since:2022-09-14 until:2022-09-21').get_items()):
if i > maxTweets:
break
antiVaxTweets.append(tweet.content)
# initiating a set to hold unique words
# initiating arrays to hold the edges
uniqueProWords = set()
uniqueAntiWords = set()
proEdges = []
antiEdges = []
# exclusion pattern to not accept any https which are gifs or any @ signs or most emojis
exclusionPattern = re.compile(r'(^https|[\U00010000-\U0010ffff]|^@|\U0000231B)', flags=re.UNICODE)
# node vals
# edge list
for tweet in proVaccineTweets:
# splitting the tweet to get rid of spaces
rawWords = tweet.split()
words = []
for word in rawWords:
# using the exclusionPattern to check to see if there are emojis, urls, or @s
m = re.search(exclusionPattern, word.strip())
if m:
continue
words.append(word)
for i in range(len(words)):
uniqueProWords.add(words[i])
if i < len(words) - 1 and len(words) > 1:
proEdges.append((words[i], words[i + 1]))
# Same as the previous but with is for antiVax
for tweet in antiVaxTweets:
rawWords = tweet.split()
words = []
for word in rawWords:
m = re.search(exclusionPattern, word.strip())
if m:
continue
words.append(word)
for i in range(len(words)):
uniqueAntiWords.add(words[i])
if i < len(words) - 1 and len(words) > 1:
antiEdges.append((words[i], words[i + 1]))
# This function is for page rank
def plot_pagerank_dist(graph, fileName):
pagerank_list = list(nx.pagerank(graph).values())
plt.hist(pagerank_list, alpha=0.85)
plt.xlabel('Degree')
plt.ylabel('Frequency')
plt.title('Histogram for Page Rank ' + fileName)
plt.savefig(fileName + ".png")
# this function is for clustering
def plot_clustering_dist(graph, fileName):
clustering_coeffs_list = list(nx.clustering(graph).values())
plt.hist(clustering_coeffs_list, bins=100, range=(0.0, 1.0), density=False)
plt.xlabel('Degree')
plt.ylabel('Frequency')
plt.title('Histogram for Clustering ' + fileName)
plt.savefig(fileName + ".png")
# This function is for degree distribution
def plot_degree_dist(importedGraph, fileName):
degrees = [importedGraph.degree(n) for n in importedGraph.nodes()]
plt.hist(degrees, bins=50, rwidth=20)
plt.xlabel('Degree')
plt.ylabel('Frequency')
plt.title('Degree Distribution of Nodes ' + fileName)
plt.savefig(fileName + ".png")
# Creating a graph and then saving it into a png
g = nx.Graph()
# adding the nodes from the unique words for pro-vaccine
g.add_nodes_from(list(uniqueProWords))
# adding the edges
g.add_edges_from(proEdges)
# drawing the graph
nx.draw(g, with_labels=True, node_size=100)
# styling the graph
plt.title("Network Graph for Pro-Vaccine")
# Saving the figure as a png
plt.savefig("networkGraphProVaccine.png")
# clearing the plt
plt.clf()
# making three calls to functions and passing the graph to make histograms
plot_degree_dist(g, "proVaxHistogram")
plt.clf()
plot_clustering_dist(g, "proClusteringHistogram")
plt.clf()
plot_pagerank_dist(g, "proPageRankHistogram")
# clearing the graph and then also clearing the plt
g.clear()
plt.clf()
# creating a new graph for the antivaccines and then also exporting to png
g = nx.Graph()
g.add_nodes_from(list(uniqueAntiWords))
g.add_edges_from(antiEdges)
nx.draw(g, with_labels=True, node_size=100)
plt.title("Network Graph for Anti-Vaccine")
plt.savefig("networkGraphAntiVaccine.png")
plt.clf()
plot_degree_dist(g, "antiVaxHistogram")
plt.clf()
plot_clustering_dist(g, "antiClusteringHistogram")
plt.clf()
plot_pagerank_dist(g, "antiPageRankHistogram")
if __name__ == "__main__":
kyle()