This document discusses using Python for social network analysis on Facebook data. It provides examples of:
- Connecting to the Facebook API and obtaining an access token
- Retrieving user and friend data via API calls
- Analyzing likes on posts to determine who likes a user's posts the most
- Performing text mining on post messages using NLTK and Jieba to determine popular topics
- Modeling the friendship network as a graph and using NetworkX and community detection to identify groups within the social network.
5. What do we want to know?
Who knows whom, and which people are common to their social
networks?
How frequently are particular people communicating with one
another?
Which social network connections generate the most value for a
particular niche?
How does geography affect your social connections in an online
world?
Who are the most influential/popular people in a social network?
What are people chatting about (and is it valuable)?
What are people interested in based upon the human language that
they use in a digital world?
19. Get Likes Count of Posts
access_token = '<access_token>'
url="https://graph.facebook.com/me/posts?access_token=%s"
response = requests.get(url%(access_token))
fb_data = json.loads(response.text)
count_dic = {}
for post in fb_data['data']:
if 'likes' in post:
for rec in post['likes']['data']:
if rec['name'] in count_dic:
count_dic[rec['name']] += 1
else:
count_dic[rec['name']] = 1
22. Take Cross-Strait Agreement
As Example
keyword_dic = {}
posts_url = 'https://graph.facebook.com/%s/posts?access_token=%s'
post_response = rs.get(posts_url%(userid, access_token))
post_json = json.loads(post_response.text)
for post in post_json['data']:
if 'message' in post:
m = re.search('服貿', post['message'].encode('utf-8'))
if m:
if userid not in keyword_dic:
keyword_dic[userid] = 1
else:
keyword_dic[userid] += 1
26. Using Jieba For Word
Tokenization
import jieba
data = post_json['data']
dic = {}
for rec in post_json['data']:
if 'message' in rec:
seg_list = jieba.cut(rec['message'])
for seg in seg_list:
if seg in dic:
dic[seg] = dic[seg] + 1
else:
dic[seg] = 1
30. Build Friendship Matrix
import networkx as nx
mutual_friends = {}
for friend in friends_obj['data']:
mutual_url = "https://graph.facebook.com/%s/mutualfriends?access_token=%s"
res = requests.get( mutual_url % (friend['id'], access_token) )
response_data = json.loads(res.text)['data']
mutual_friends[friend['name']] = [ data['name'] for data in response_data ]
nxg = nx.Graph()
[ nxg.add_edge('me', mf) for mf in mutual_friends ]
[ nxg.add_edge(f1, f2)
for f1 in mutual_friends
for f2 in mutual_friends[f1] ]