Skip to content

Commit d58efeb

Browse files
author
Alex Redington
committed
Reverting changes, keeping chouser's work.
1 parent 118e6c2 commit d58efeb

File tree

1 file changed

+17
-49
lines changed

1 file changed

+17
-49
lines changed
Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,37 @@
1-
(ns twitterbuzz.stats)
1+
(ns twitterbuzz.graph)
22

33
;; code review from SDH:
44
;;
5-
;; 1. I think inc-or-nil could be replaced by fnil - fixed ar
5+
;; 1. I think inc-or-nil could be replaced by fnil
66
;; 2. update-retweet-count looks broken
7-
;; `when` instead of `if` in function to be reduced over - fixed ar
7+
;; `when` instead of `if` in function to be reduced over
88
;; 3. conversion from string keys to keywords should be
9-
;; done already before we get here - fixed ar
9+
;; done already before we get here
1010
;; 4. fns like top-tweets should not take an n arg
11-
;; consumers can always take what they want - fixed ar
12-
;; 5. namespace and file name do not match - fixed ar
11+
;; consumers can always take what they want
12+
;; 5. namespace and file name do not match
13+
14+
(defn inc-or-1
15+
"Returns 1 if x is nil, (inc x) otherwise"
16+
[x]
17+
(if (nil? x) 1 (inc x)))
1318

1419
(defn update-retweet-count
1520
"Updates the total count of retweets by id in m as indicated by the contents of
1621
the tweet t"
1722
[m t]
18-
(let [rt-id (get-in t [:retweeted_status :id_str])]
19-
(if (nil? rt-id) m (update-in m [rt-id] (fnil inc 0)))))
23+
(let [rt-id (get-in t ["retweeted_status" "id_str"])]
24+
(when-not (nil? rt-id) (update-in m [rt-id] inc-or-1))))
2025

2126
(defn top-tweets
22-
"Returns the ids of the most retweeted tweets out of tweet seq ts in descending
23-
order"
24-
[ts]
27+
"Returns the ids of top n most retweeted tweets out of tweet seq ts"
28+
[ts n]
2529
(let [retweet-counts (reduce update-retweet-count {} ts)
2630
tweet-ids (keys retweet-counts)]
27-
(sort-by #(get retweet-counts %) tweet-ids)))
28-
29-
(defn retweet-of
30-
"Returns the username of the user who originally authored the tweet
31-
that t is a retweet of, nil otherwise"
32-
[t]
33-
(get-in t [:retweeted_status :user :name]))
34-
35-
(defn mentioned-users
36-
"Returns the usernames of all the users mentioned in tweet t"
37-
[t]
38-
(map #(get % :screen_name) (get-in t [:entities :user_mentions])))
39-
40-
(defn new-edges
41-
"Filter the seq of sets n to find all sets in n that do not already
42-
exist in the seq of sets o"
43-
[n o]
44-
(filter #(not (contains? o %)) n))
45-
46-
(defn edges
47-
"Return a lazy seq of pairs of usernames representing the social edges of a tweet
48-
stream, where a social edge is defined as one user retweeting or
49-
mentioning another user in a given tweet. Edges are not directional; if
50-
@alice retweets @bob, and @bob mentions @alice, those two tweets only create
51-
one edge."
52-
([ts]
53-
(edges ts #{}))
54-
([ts edges]
55-
(let [t (first ts)
56-
other-users (concat (list (retweet-of t)) (mentioned-users t))
57-
author (get-in t [:user :name])
58-
tweet-edges (set (map #(set (list author %)) other-users))
59-
new-edges (new-edges tweet-edges edges)]
60-
(when t
61-
(if (empty? new-edges)
62-
(recur (rest ts) edges)
63-
(lazy-seq (concat new-edges (edges (rest ts) (concat new-edges edges)))))))))
31+
(take n (sort-by #(get retweet-counts %) tweet-ids))))
6432

6533
(defn graph
6634
"Returns a map representing the graph of the tweet seq ts"
6735
[ts]
68-
{:nodes {}
36+
{:nodes []
6937
:edges []})

0 commit comments

Comments
 (0)