Video: https://youtu.be/dkgzpnbI5II
Codes: https://colab.research.google.com/github/taudataid/eLearning/blob/master/sna-02.ipynb
Social Network Analysis (SNA)
https://taudata.blogspot.com
SNA-02: Visualisasi Jaringan Sosial via Gephi
https://taudata.blogspot.com/2022/05/sna-02.html
(C) Taufik Sutanto
In [ ]:
import warnings; warnings.simplefilter('ignore')
import nltk
try:
import google.colab
IN_COLAB = True
!wget https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/taudataEDA02CTA.py
!wget https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/taudata_sna.py
!wget https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/network_unp2021.gexf
!mkdir data
!wget -P data/ https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/data/data_sna.csv
!wget -P data/ https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/data/slang.txt
!wget -P data/ https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/data/stopwords_id.txt
!wget -P data/ https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/data/stopwords_en.txt
!wget -P data/ https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/data/kata_dasar.txt
!wget -P data/ https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/data/wn-ind-def.tab
!wget -P data/ https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/data/wn-msa-all.tab
!wget -P data/ https://raw.githubusercontent.com/taudata-indonesia/eLearning/master/data/all_indo_man_tag_corpus_model.crf.tagger
!pip install --upgrade spacy python-crfsuite unidecode textblob sastrawi sklearn-pycrfsuite
!pip install --upgrade unidecode twython tweepy beautifulsoup4 json_lines lxml pyLDAvis json_lines
!python -m spacy download xx_ent_wiki_sm
!python -m spacy download en_core_web_sm
nltk.download('popular')
except:
IN_COLAB = False
print("Running the code locally, please make sure all the python module versions agree with colab environment and all data/assets downloaded")
In [ ]:
# import beberapa module yg dibutuhkan di WorkShop ini:
import graphviz, warnings; warnings.simplefilter('ignore')
import time, numpy as np, matplotlib.pyplot as plt, pandas as pd, networkx as nx
import random
import taudataEDA02CTA as tau2
import graphviz, taudata_sna as tau
import pyLDAvis, pyLDAvis.sklearn; pyLDAvis.enable_notebook()
from graphviz import Digraph
import json, urllib.request, requests
from urllib.request import Request, urlopen
from twython import TwythonStreamer
from bs4 import BeautifulSoup as bs
from tqdm import tqdm
seed = 123
random.seed(seed)
np.random.seed(seed)
"Done"
Graph From Social media
Mentions, Followers, Friends
In [ ]:
# import data
import json, urllib.request, requests, tweepy
from urllib.request import Request, urlopen
from tqdm import tqdm
# Contoh API Keys (Sesuaikan dengan API keys masing-masing)
Ck = 'NKrVDMAKVOv' # consumer_key
Cs = '8dDbaP1YIuQWJfRL4QEvp' # consumer_secret
At = '2214118411-lyB0xblpHaHWxU' # access_token
As = 'SzSYbjjJyifcF' # access_secret
'Done'
In [ ]:
# Pengambilan Data dan-atau Loading Data
qry = "data indonesia" # Ganti dengan sembarang query lain
tweetFile = 'Tweets_taudata.json'.format(qry) # RUBAH Path ini
try:
data = tau2.loadTweets(file=tweetFile)
print("Local Data loaded, processing {} tweets.".format(len(data)))
except Exception as Err_:
print("Local data not available, importing data directly from twitter ... ")
api = tau2.connect(key=(Ck, Cs, At, As))
data = tau2.crawlTwitter(api, qry, N=1000)
tau2.saveTweets(data, file=tweetFile) # Save untuk lain waktu
tweets = [t['full_text'] for t in data] # Hanya mengambil tweeet saja .
Usernames = [t['user']['screen_name'] for t in data] # Hanya mengambil username saja .
Usernames[:5]
Visualisasi Versi Gephi¶
- menggunakan Data dari tweepy: Dibahas di Workshop/Module ini.
- Crawl langsung dari gephi: https://www.youtube.com/watch?v=j_PUwmDcPBs
In [ ]:
json_file = 'Tweets_taudata.json'
gexf_file = 'Tweets_taudata.gexf'
tweets = tau.load_jsonl(json_file)
tau.create_gexf(tweets, gexf_file)
"Done"
Visualizing in Gephi¶
- "import" file gexf ke Gephi (yakinkan tipe network "Dynamics" - lihat gambar)
- "Open graph File ... "
Visualisasi¶
- Ganti Size Nodes: Nodes ==> Size ==> in-Degree
- Atur "range" sedemikian sehingga distribusi ukuran vertex cukup baik.
- Community Detection: Statistics ==> Modularity
- Ganti Warna Nodes: Nodes ==> Partition ==> Modularity
- Ganti Layout Graph: ForceAtlas2
- Stronger Gravity
- Gravity 0.05
- Prevent Overlap
- Add Node Label (Ada di tengah bawah, huruf "T")
- ganti warna background (misal hitam)
Navigasi¶
- Mouse Scroll: Zoom in-out ATAU panah kanan bawah untuk atur zoom
- Tekan (tahan) klik kanan lalu gerakkan mouse (atau touchpad) untuk memindahkan graph.
Analisa?¶
- Klik kanan di Node ==> Select in Data Laboratory ==> Pelajari akun dan-atau tweet orang tersebut.
Tidak ada komentar:
Posting Komentar
Relevant & Respectful Comments Only.