-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslangFrequencyByState.py
57 lines (49 loc) · 1.9 KB
/
slangFrequencyByState.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
import pandas as pd
import snscrape.modules.reddit as snReddit
def findfrequency(state, word):
try:
print(f'Ran at state {state}')
# daterange = pd.date_range(start='2022/03/25', end='2023-03-25')
scraper = snReddit.RedditSubredditScraper(state)
# Holds tweets
posts = []
i = 0
numberofmatches = 0
for post in scraper.get_items():
# Stop because it gets mad it will fingerprinting
if i > 100:
break
# and post.selftext is not None and word in post.selftext
if type(post) == snReddit.Submission and post.selftext is not None and word in post.selftext:
data = [
post.date,
post.author,
post.selftext
]
posts.append(data)
numberofmatches += 1
# print("Submission Found!")
# and post.body is not None and word in post.body
elif type(post) != snReddit.Submission and post.body is not None and word in post.body:
data = [
post.date,
post.author,
post.body
]
posts.append(data)
numberofmatches += 1
# print("Submission Found!")
i += 1
# Created data frame using pandas
dataframe = pd.DataFrame(posts, columns=['date', 'username', 'post'])
# Converts code to CSV
dataframe.to_csv(f'{state}.csv', index=False)
print(f'{state}: Total number of Matches with the word {numberofmatches}')
# return numberofmatches
# getstatewordfrequency(wordToQueryFor)
return numberofmatches / 100
except:
print(f'Error! {state} did not finish running!')
return None
def getstatewordfrequency(word):
findfrequency(word, "Texas")