forked from neo4j-graph-examples/twitter-trolls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter-trolls.neo4j-browser-guide
683 lines (488 loc) · 17.6 KB
/
twitter-trolls.neo4j-browser-guide
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
= Russian Twitter Trolls
:experimental:
:icon: font
== Russian Twitter Trolls
++++
<div class="col-lg-4">
++++
As part of the House Intelligence Committee investigation into *how Russia may have influenced the 2016 US Election*, Twitter released the screen names of almost 3000 Twitter accounts believed to be connected to Russia's Internet Research Agency, a company known for operating *social media troll accounts*. Twitter immediately suspended these accounts, deleting their data from Twitter.com and the Twitter API.
A team at NBC News including link:https://twitter.com/bpopken[Ben Popken^] and link:https://twitter.com/mrejfox[EJ Fox^] was able to reconstruct a dataset consisting of a subset of the deleted data for their investigation and using Neo4j were able to link:https://www.nbcnews.com/tech/social-media/russian-trolls-went-attack-during-key-election-moments-n827176[show how these troll accounts went on attack during key election moments.^] *NBC News open-sourced the reconstructed dataset* and released it as this Neo4j database.
++++
</div>
++++
++++
<div class="col-lg-3">
++++
[subs=attributes]
++++
<img src="{img}/datamodel2.png" class="img-responsive">
++++
This interactive Neo4j Browser guide will help you to explore the dataset of Russian Troll tweets by guidng you through:
* an overview of the datamodel
* how to explore the data using Cypher, the query language for graphs
* some of the investigative queries used to make sense of the the dataset
++++
</div>
++++
++++
<div class="col-lg-5">
++++
[subs=attributes]
++++
<img src="{img}/article.png" class="img-responsive">
++++
Read the NBC News story about the data link:https://www.nbcnews.com/tech/social-media/russian-trolls-went-attack-during-key-election-moments-n827176[here^]
_NOTE: Much of this content is offensive and explicit. Query with caution._
++++
</div>
++++
== The Datamodel
++++
<div class="col-lg-4">
++++
Neo4j uses the labeled property graph datamodel to represent data.
=== Nodes
Nodes can have one or more *node labels* to describe the "type" of the node.
* *Tweet* - The Tweet
* *User* - A Twitter user
* *Troll* - An optional node label to identify users that are on the House list of Russian troll accounts
* *Hashtag* - Any hashtags used in the tweet
* *Source* - The Twitter application used to post the tweet
* *URL* - A link embedded in a tweet
++++
</div>
++++
++++
<div class="col-lg-3">
++++
=== Relationships
Relationships have a single *relationship type*.
* *POSTED* - Connects the `Tweet` with the User who posted it
* *MENTIONS* - Connects any Users that are `@-mentioned` to the `Tweet`
* *HAS_TAG* - Connect a `Tweet` to any `Hashtag` nodes included in the tweet
* *POSTED_VIA* - Connects a `Tweet` to its `Source` application node
* *RETWEETED* - Connects a `Tweet` that retweets another `Tweet`
* *IN_REPLY_TO* - Connects a `Tweet` that is a reply to another `Tweet`
++++
</div>
++++
++++
<div class="col-lg-5">
++++
[subs=attributes]
++++
<img src="{img}/datamodel.png" class="img-responsive">
++++
Nodes and relationships can store arbitrary key-value pair *properties*. This image shows the properties and data type used in this database.
.Execute this query to inspect the datamodel in the database:
[source,cypher]
----
CALL db.schema()
----
++++
</div>
++++
== Introduction To Cypher
++++
<div class="col-lg-5">
++++
In order to work with our labeled property graph, we need a query language for graphs.
=== Graph Patterns
Cypher is the query language for graphs and is centered around *graph patterns*. Graph patterns are expressed in Cypher using ASCII-art like syntax.
*Nodes* are defined within parentheses `()`. Optionally, we can specify node label(s): `(:User)`
*Relationships* are defined within square brackets `[]`. Optionally we can specify type and direction:
`(:Tweet)**+<-[:POSTED]-+**(:User)`
Graph elements can be bound to *aliases* that can be referred to later in the query:
`(**t**:Tweet)+<-+[**r**:POSTED]-(**u**:User)`
=== Predicates
Filters can be applied to these graph patterns to limit the matching paths. Boolean logic operators, regular expressions and string comparison operators can be used here.
Be sure to use the link:https://neo4j.com/docs/cypher-refcard/current/?ref=browser-guide[Cypher Refcard] as a reference for learning Cypher syntax.
++++
</div>
++++
++++
<div class="col-lg-7">
++++
=== Dissecting a Cypher Statement
Let's look at a Cypher query that answers the question "For Users with over 1000 followers, how many tweets did they post?". Don't worry if this seems complex, we'll build up our understanding of Cypher as we move along.
.Click on the block to put the query in the top-most window on the query editor. Hit the triangular icon:play-circle[] button or press kbd:[Ctrl+Enter] to run it and see the resulting visualization.
[source,cypher]
----
MATCH (u:User)-[r1:POSTED]->(t:Tweet)
WHERE u.followers_count > 1000
WITH COUNT(t) AS num
RETURN num
----
[width=100,cols="1,3,2"]
|===
| find | `+MATCH (u:User)-[:POSTED]->(t:Tweet)+` | Search for an existing graph pattern
| filter | `WHERE u.followers_count > 1000` | Filter matching paths to only those matching a predicate
| aggregate | `WITH COUNT(t) AS num` | Count number of paths matched
| return | `RETURN num` | Specify columns to be returned by the statement
|===
++++
</div>
++++
== Querying The Graph - Overview
++++
<div class="col-lg-6">
++++
Neo4j Browser is an interactive query workbench tool for executing Cypher queries in Neo4j and working with the results in both graph visualization and tabular forms.
=== Graph Results
If our Cypher query returns graph objects (Nodes, Relationships, Paths) then Neo4j Browser will render a graph visualization for us to explore.
=== Tabular Results
Often the answer to our question however is tabular data - for example, the result of running an aggregation. We can also return rows of scalar values in Cypher. In that case Neo4j Browser will render a table of rows.
++++
</div>
++++
++++
<div class="col-lg-6">
++++
.Find tweets with the hashtag `thanksobama`
[source,cypher]
----
MATCH (u:User)-[:POSTED]->(t:Tweet)-[:HAS_TAG]->(ht:Hashtag {tag: "thanksobama"})
RETURN * LIMIT 50
----
.Overview query. What are the node labels in the graphs and how many of each?
[source, cypher]
----
MATCH (t)
RETURN LABELS(t), COUNT(*) AS count
ORDER BY count DESC
----
++++
</div>
++++
== Querying The Graph - Users
A simple type of query we can run is one that matches on a single node label. In this database we've added the node label `Troll` to any users that show up on the House list of Russian Twitter Trolls.
We can search for the Trolls with the most followers:
[source,cypher]
----
MATCH (u:Troll) WHERE EXISTS(u.followers_count)
RETURN u.screen_name AS screen_name, u.followers_count AS followers
ORDER BY followers DESC LIMIT 50
----
Or use an aggregation function like `COUNT()` to see the most popular locations that Troll users list in their profiles:
[source,cypher]
----
MATCH (u:Troll)
RETURN u.location, COUNT(u) AS num
ORDER BY num DESC LIMIT 100
----
Or use a predicate filter to search for a specific Troll by screen name:
[source,cypher]
----
MATCH (u:Troll)
WHERE u.screen_name = "LeroyLovesUSA"
RETURN u
----
== Querying The Graph - Tweets
We can specify more complex graph patterns using Cypher to search for users plus the tweets they've posted, and even the hashtags that those tweets contain.
++++
<div class="col-lg-7">
++++
.Tweets posted by the user LeroyLovesUSA with the hashtag thanksobama
[source,cypher]
----
MATCH (u:User)-[:POSTED]->(t:Tweet)-[:HAS_TAG]->(ht:Hashtag {tag: "thanksobama"})
WHERE u.screen_name = "LeroyLovesUSA"
RETURN *
----
We can include predicates to filter for paths matching the pattern and our predicates:
.Find tweets posted by Troll accounts that contain the word fraud
[source,cypher]
----
MATCH (t:Troll)-[:POSTED]->(tw:Tweet)
WHERE tw.text CONTAINS "fraud"
OPTIONAL MATCH p=(tw)-[:HAS_TAG|HAS_LINK|MENTIONS|IN_REPLY_TO]-(a)
RETURN * LIMIT 50
----
++++
</div>
++++
++++
<div class="col-lg-5">
++++
[subs=attributes]
++++
<img src="{img}/exampletweet1.png" class="img-responsive">
++++
++++
</div>
++++
== Tweet Volume
++++
<div class="col-lg-3">
++++
[subs=attributes]
++++
<img src="{img}/volume.png" style="max-height: 380px;" class="img-responsive">
++++
++++
</div>
++++
++++
<div class="col-lg-9">
++++
This chart from the link:https://www.nbcnews.com/tech/social-media/russian-trolls-went-attack-during-key-election-moments-n827176[NBC News article^] shows that Russian Twitter troll tweet volume spiked during key election related events.
++++
<p></p>
++++
++++
<p></p>
++++
.Query for tweet volume by day
[source,cypher]
----
MATCH (:Troll)-[:POSTED]->(t:Tweet) WHERE t.created_str > "2016-10-01"
RETURN substring(t.created_str,0,10) AS day,
COUNT(t) AS num ORDER BY day LIMIT 60
----
.Query for tweet volume by month
[source,cypher]
----
MATCH (:Troll)-[:POSTED]->(t:Tweet)
RETURN substring(t.created_str,0,7) AS month,
COUNT(t) AS num ORDER BY month DESC LIMIT 25
----
We can see that much of the tweet volume occurs leading up to and immediately following the 2016 US Election.
++++
</div>
++++
== Hashtags
++++
<div class="col-lg-9">
++++
Hashtags were used by the trolls to insert themselves into conversations and gain visibility.
.What are the most common hashtags used by the trolls?
[source,cypher]
----
MATCH (ht:Hashtag)<-[:HAS_TAG]-(tw:Tweet)<-[:POSTED]-(:Troll)
WITH ht, COUNT(tw) AS num ORDER BY num DESC
RETURN ht.tag AS hashtag, num LIMIT 10
----
.Most popular hashtags used by Trolls by day
[source,cypher]
----
MATCH (:Troll)-[:POSTED]->(t:Tweet)
WHERE t.created_str > "2016-10-01" AND t.created_str < "2016-11-30"
WITH t,substring(t.created_str,0,10) AS day, [(t)-[:HAS_TAG]->(ht:Hashtag) | ht.tag] AS tags
UNWIND tags AS tag
WITH day, tag, COUNT(t) AS num ORDER BY num DESC
RETURN day, COLLECT(tag)[..5] AS toptags ORDER BY day
----
++++
</div>
++++
++++
<div class="col-lg-3">
++++
[subs=attributes]
++++
<img src="{img}/hashtags.png" class="img-responsive" style="max-height: 380px;">
++++
This streamgraph from the link:https://www.nbcnews.com/tech/social-media/russian-trolls-went-attack-during-key-election-moments-n827176[NBC News article^] shows hashtag volume by category.
++++
</div>
++++
== Hashtags - `#RejectedDebateTopics`
=== `@WordOfHashtags`
One of the hashtags used by the Trolls was `#RejectedDebateTopics`. The Troll account `@WorldOfHashtags` used it as a way to annouce a "hashtag game" to encourage others to use the same hashtag. Hundreds of non-troll accounts ended up interacting with this acocount by using the `#RejectedDebateTopics` hashtag.
.What are the tweets posted by `@WorldOfHashtags` with the hashtag `#RejectedDebateTopics` ?
[source,cypher]
----
MATCH (u:Troll {screen_name: "WorldOfHashtags"})-[:POSTED]->(t:Tweet)-[:HAS_TAG]->(:Hashtag {tag: "rejecteddebatetopics"})
WHERE t.created_str > "2016-10-01"
RETURN t.text, t.created_str
ORDER BY t.created_at LIMIT 50
----
== Tweet Sources Used
A variety of applications were used by the Troll accounts for posting tweets.
.What are the common sources used for posting tweets?
[source,cypher]
----
MATCH (:Troll)-[:POSTED]->(tw:Tweet)-[:POSTED_VIA]->(s:Source)
RETURN s.name AS source, COUNT(tw) AS tweets
ORDER BY tweets DESC
----
== Original Content vs Replies and Retweets
Tweets can be retweets of other tweets or "original content" tweets that do not reference other tweets. Many of the Troll tweets are retweets and replies. We can filter out these retweets and replies and search only for "original content" tweets.
.Find original content tweets posted by user @TEN_GOP
[source,cypher]
----
MATCH p=(t:Troll {user_key: "ten_gop"})-[:POSTED]->(tw:Tweet)
WHERE NOT EXISTS((tw)-[:RETWEETED|IN_REPLY_TO]->(:Tweet))
OPTIONAL MATCH (ht:Hashtag)<-[:HAS_TAG]-(tw)
OPTIONAL MATCH (u:User)<-[:MENTIONS]-(tw)
RETURN p,ht,u LIMIT 25
----
== Inferred Relationships
++++
<div class="col-lg-9">
++++
Inferred relationships are important in graphs. For example, when a Troll account retweets another Troll's tweet we could say the trolls have an inferred *"AMPLIFIED"* relationship: one troll is amplifying the message of the other.
.Inferred AMPLIFIED relationships exist when a troll account retweets another troll
[source,cypher]
----
MATCH p=(r1:Troll)-[:POSTED]->(:Tweet)<-[:RETWEETED]-(:Tweet)<-[:POSTED]-(r2:Troll)
RETURN p LIMIT 1
----
++++
</div>
++++
++++
<div class="col-lg-3">
++++
[subs=attributes]
++++
<img src="{img}/amplified.png" class="img-responsive">
++++
++++
</div>
++++
== Graph Algorithms
++++
<div class="col-lg-9">
++++
link:https://neo4j.com/graph-analytics/[Graph algorithms^] are a way to apply analytics to the entire graph to further enhance our understanding of the data. These algorithms fall into three categories:
* **Centrality** - What are the most important nodes in the network? _PageRank, Betweenness Centrality, Closeness Centrality_
* **Community detection** - How can the graph be partitioned? _Union Find, Louvain, Label Propagation, Connected Components_
* **Pathfinding** - What are the shortest paths or best routes available given cost? _Minimum Weight Spanning Tree, All Pairs- and Single Source- Shortest Path, Dijkstra_
We can run these algorithms in Neo4j with Cypher using the link:https://github.com/neo4j-contrib/neo4j-graph-algorithms[Neo4j Graph Algorithms^] procedures.
++++
</div>
++++
++++
<div class="col-lg-3">
++++
.See what procedures are available in the algo package
[source,cypher]
----
CALL dbms.procedures()
YIELD name, signature, description
WITH * WHERE name STARTS WITH "algo"
RETURN *
----
++++
</div>
++++
== Centrality
++++
<div class="col-lg-9">
++++
Let's run PageRank over this inferred AMPLIFIED graph to find the most influential trolls
.Run PageRank over the inferred troll amplification graph. This will write the results back to a pagerank property on the nodes
[source,cypher]
----
CALL algo.pageRank("MATCH (t:Troll) RETURN id(t) AS id","MATCH (r1:Troll)-[:POSTED]->(:Tweet)<-[:RETWEETED]-(:Tweet)<-[:POSTED]-(r2:Troll) RETURN id(r2) as source, id(r1) as target", {graph:'cypher'})
----
.Find Trolls with highest PageRank score
[source,cypher]
----
MATCH (t:Troll) WHERE EXISTS(t.pagerank)
RETURN t.screen_name AS troll, t.pagerank AS pagerank ORDER BY pagerank DESC LIMIT 25
----
.What are the top trolls tweeting about?
[source,cypher]
----
MATCH (t:Troll) WHERE EXISTS(t.pagerank)
WITH t ORDER BY t.pagerank LIMIT 25
MATCH (t)-[:POSTED]->(tw:Tweet)-[:HAS_TAG]-(ht:Hashtag)
RETURN ht.tag, COUNT(tw) AS num ORDER BY num DESC LIMIT 25
----
++++
</div>
++++
++++
<div class="col-lg-3">
++++
[subs=attributes]
++++
<img src="{img}/pagerank.png" class="img-responsive">
++++
PageRank is a recursive graph algorithm that defines the importance of a node proportional to the importance and number of connected nodes in the graph.
++++
</div>
++++
== Community Detection
++++
<div class="col-lg-9">
++++
We can also run community detection over this inferred AMPLIFIED graph to see how the graph is partitioned
.Partition the graph into communities using the Label Propagation algorithm. An additional property `community` is added to each node specifying the community it has been assgined to by the algorithm.
[source,cypher]
----
CALL algo.labelPropagation("MATCH (t:Troll) RETURN id(t) AS id","MATCH (r1:Troll)-[:POSTED]->(t:Tweet)<-[:RETWEETED]-(:Tweet)<-[:POSTED]-(r2:Troll) RETURN id(r2) AS source, id(r1) AS target, COUNT(t) AS weight","OUTGOING", {graph:'cypher', write: true, iterations: 200})
----
We can then see which Trolls were assigned to each community:
[source,cypher]
----
MATCH (t:Troll) WHERE EXISTS(t.partition)
RETURN COLLECT(t.screen_name) AS members, t.partition AS community
ORDER BY SIZE(members) DESC LIMIT 10
----
And finally, we can see if there are certain themes that each community was focused on, by inspecting the most common hashtags used by each community:
[source,cypher]
----
MATCH (t:Troll) WHERE EXISTS(t.partition)
WITH COLLECT(t) AS members, t.partition AS community
ORDER BY SIZE(members) DESC LIMIT 10
UNWIND members AS t
MATCH (t)-[:POSTED]->(tw:Tweet)-[:HAS_TAG]->(ht:Hashtag)
WITH community, ht.tag AS tag, COUNT(tw) AS num ORDER BY num DESC
RETURN community, COLLECT(tag)[..10] AS toptags
LIMIT 10
----
++++
</div>
++++
++++
<div class="col-lg-3">
++++
[subs=attributes]
++++
<img src="{img}/community.png" class="img-responsive">
++++
++++
</div>
++++
//== Visualization
== Further exploration
++++
<div class="col-lg-4">
++++
=== Ideas
We've only just begun to explore the data. Here are some ideas for further data analysis:
* Location
* Entity extraction
* Temporal data analysis (are tweets posted at a certain time of day?)
* Clustering the graph
* Exploring most co-mentioned users and hashtags
++++
</div>
++++
++++
<div class="col-lg-5">
++++
=== Resources
* Web link:https://neo4j.com/docs/cypher-refcard/current/?ref=browser-guide[Cypher Refcard^]
* Web link:https://neo4j.com/docs/?ref=browser-guide[Neo4j Documentation^]
* Book (free download) link:http://graphdatabases.com/?ref=browser-guide[Graph Databases^]
* Blog link:https://medium.com/@david.allen_3172/using-nlp-in-neo4j-ac40bc92196f[Using NLP In Neo4j^]
* Blog link:http://www.lyonwj.com/2017/11/15/entity-extraction-russian-troll-tweets-neo4j/[NLP And Entity Extraction^]
* Article link:https://www.nbcnews.com/tech/social-media/russian-trolls-went-attack-during-key-election-moments-n827176[NBC News Article^]
. pass:a[<a play-topic='{GUIDES}/exercise.html'>Hands On Exercise</a>]
++++
</div>
++++
++++
<div class="col-lg-3">
++++
[subs=attributes]
++++
<img src="{img}/viz.png" class="img-responsive">
++++
++++
</div>
++++