This repository was archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsara_network.py
More file actions
68 lines (54 loc) · 1.96 KB
/
sara_network.py
File metadata and controls
68 lines (54 loc) · 1.96 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
# -*- coding: utf-8 -*-
"""
Script
Generate Networks:
From retweets or mentions(@)
The tweets used to generate these networks are loaded from MongoDB.
SARA
Licença - MIT
LabMIC - UFSJ
2019 - 2021
"""
# system import
from pathlib import Path
# local
from utils import handler_input, is_directed
from sara.core.config import network_path
# intern import
from sara.core.network_generators import (get_mentions_network,
get_retweets_network)
from sara.core.sara_data import SaraData
from sara.core.utils import save_network
def main():
"""Generate networks without weight.
Generate Graph or Digraph (Directed) from mentions or retweets.
"""
graph_name, collection, database, directed, limit, source = handler_input()
directed = is_directed(directed)
# Create instance used to recovery data from database
data = SaraData(collection, database)
if source == 'r':
# Generate Retweets network
projection = {'user.screen_name': 1,
'retweeted_status.user.screen_name': 1}
# Recovery tweets
tweets = data.get_projected_data(projection, limit)
# Generate network using retweets
network = get_retweets_network(tweets, directed)
path_to_save = Path(network_path, "retweets/")
path_to_save = path_to_save.joinpath(graph_name)
save_network(network, path_to_save)
elif source == 'm':
# Generate mentions network
projection = {'user.screen_name': 1,
'entities.user_mentions.screen_name': 1}
tweets = data.get_projected_data(projection, limit)
network = get_mentions_network(tweets, directed)
# Save to file
path_to_save = Path(network_path, "mention/")
path_to_save = path_to_save.joinpath(graph_name)
save_network(network, path_to_save)
else:
raise ValueError("Invalid network type, valid type r or m")
if __name__ == '__main__':
main()