-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_currencies.py
62 lines (43 loc) · 1.71 KB
/
extract_currencies.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
import json
import re
# the function checks a tweet for cryptonames appearance and returns
# the list of found cryptos
def extract_currencies(tweet: str) -> list:
# get dict of cryptoes names from json files
# symbol: {symbol,name,slug}
with open("cryptonames.json") as json_file:
data = json.load(json_file)
# x - list of all words in tweet which starts with '#' or '$'
x = re.findall(r"\$[a-zA-Z]+|\#[a-zA-Z]+", tweet)
# make all letters upper
for i in range(len(x)):
x[i] = x[i].upper()
# list of found markers which matches with markers from markers from crypto dictionary
marker_list = list()
# looking for matches
if x:
for word in x:
for key in data:
# exludes '#' and '$' from word
# looks for match to crypto symbol or crypto name
if (word[1:] == key) or (word[1:] == data[key]['name'].upper()):
# add found match to list
marker_list.append(key)
return marker_list
if __name__ == "__main__":
# print(extract_currencies("Just swapped 1k $SOL for $AVAX using v2 cross-chain aggregator by @atlas_dex"))
with open("flat_tweets.json") as json_file:
file = json.load(json_file)
# dictionary of crypto mentions
marker_dict = dict()
counter = 0
for tweet in file:
marker_list = extract_currencies(tweet)
for marker in marker_list:
if marker in marker_dict:
marker_dict[marker] += 1
else:
marker_dict[marker] = 1
counter += 1
print(sorted(marker_dict.items(), key=lambda item: item[1], reverse=True))
print(counter)