-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
662 lines (543 loc) · 45.1 KB
/
index.html
File metadata and controls
662 lines (543 loc) · 45.1 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Subjective Functions</title>
<link rel="stylesheet" href="https://dswalter.github.io/theme/css/main.css" />
<link href="https://dswalter.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Subjective Functions Atom Feed" />
<!--[if IE]>
<script src="https://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body id="index" class="home">
<header id="banner" class="body">
<h1><a href="https://dswalter.github.io/">Subjective Functions </a></h1>
<nav><ul>
<li><a href="https://dswalter.github.io/pages/about-me.html">About Me</a></li>
<li><a href="https://dswalter.github.io/category/blog.html">blog</a></li>
</ul></nav>
</header><!-- /#banner -->
<aside id="featured" class="body">
<article>
<h1 class="entry-title"><a href="https://dswalter.github.io/first-2016-presidential-debate-analysis.html">A Presidential Debate in N-grams</a></h1>
<footer class="post-info">
<abbr class="published" title="2016-09-28T10:55:00-04:00">
Published: Wed 28 September 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://dswalter.github.io/author/daniel-walter.html">Daniel Walter</a>
</address>
<p>In <a href="https://dswalter.github.io/category/blog.html">blog</a>.</p>
</footer><!-- /.post-info --><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">Counter</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">nltk.tokenize</span> <span class="kn">import</span> <span class="n">RegexpTokenizer</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pprint</span> <span class="kn">import</span> <span class="n">pprint</span>
<span class="kn">import</span> <span class="nn">seaborn</span> <span class="kn">as</span> <span class="nn">sns</span>
<span class="o">%</span><span class="n">matplotlib</span> <span class="n">inline</span>
</pre></div>
<h1>A Presidential Debate in N-grams</h1>
<p>While listening to the first presidential debate between Hillary Clinton and Donald Trump, I had a suspicion that a simple n-gram analysis would reveal some interesting insights into their respective styles of speech.
So I grabbed a transcript off the internet <a href="https://www.washingtonpost.com/news/the-fix/wp/2016/09/26/the-first-trump-clinton-presidential-debate-transcript-annotated/">(not the one here, but one like it)</a> and got to n-gramming.</p>
<p>(<em>N-grams</em> are phrases of length <em>n</em> from a text or speech. When n is 2, it's called a <em>bigram</em>; when n is 3 it's called a <em>trigram</em>. We use the term n-gram because it generalizes to whichever N we want.)</p>
<div class="highlight"><pre><span></span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"/data/data/sentiment/debate_transcript.txt"</span><span class="p">,</span> <span class="s2">"r"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">all_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span> <span class="k">if</span> <span class="n">line</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">'</span><span class="se">\n</span><span class="s1">'</span><span class="p">]</span>
</pre></div>
<p>What we get at the end of the function below is a list of 'tokens', or words, that a candidate has spoken. It takes care of a few formatting niceties: removing apostrophes so contractions are only one 'token', removing the candidates' names from their own speeches, changing the newline character to the word "NEWLINE", changing everything to lower case, and removing all punctuation.</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">tokenize_by_user</span><span class="p">(</span><span class="n">line_list</span><span class="p">,</span> <span class="n">identifying_string</span><span class="p">):</span>
<span class="sd">"""Reads in lines, filters based on the identifying string, and returns a list of tokens."""</span>
<span class="n">speaker_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">line_list</span> <span class="k">if</span> <span class="n">identifying_string</span> <span class="ow">in</span> <span class="n">line</span><span class="p">]</span>
<span class="n">speaker_text</span> <span class="o">=</span> <span class="s2">""</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">speaker_lines</span><span class="p">)</span>
<span class="n">strings_to_drop</span> <span class="o">=</span> <span class="p">[</span><span class="n">identifying_string</span><span class="p">,</span> <span class="s2">"’"</span><span class="p">]</span>
<span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">strings_to_drop</span><span class="p">:</span>
<span class="n">speaker_text</span> <span class="o">=</span> <span class="n">speaker_text</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="s2">""</span><span class="p">)</span>
<span class="n">speaker_text</span> <span class="o">=</span> <span class="n">speaker_text</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
<span class="n">speaker_text</span> <span class="o">=</span> <span class="n">speaker_text</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">,</span> <span class="s2">" NEWLINE "</span><span class="p">)</span>
<span class="n">tokenizer</span> <span class="o">=</span> <span class="n">RegexpTokenizer</span><span class="p">(</span><span class="sa">r</span><span class="s1">'\w+'</span><span class="p">)</span>
<span class="n">speaker_tokens</span> <span class="o">=</span> <span class="n">tokenizer</span><span class="o">.</span><span class="n">tokenize</span><span class="p">(</span><span class="n">speaker_text</span><span class="p">)</span>
<span class="k">return</span> <span class="n">speaker_tokens</span>
</pre></div>
<p>We need a list of tokens for both candidates.</p>
<div class="highlight"><pre><span></span><span class="n">trump_tokens</span> <span class="o">=</span> <span class="n">tokenize_by_user</span><span class="p">(</span><span class="n">all_lines</span><span class="p">,</span> <span class="s2">"Donald Trump:"</span><span class="p">)</span>
<span class="n">clinton_tokens</span> <span class="o">=</span> <span class="n">tokenize_by_user</span><span class="p">(</span><span class="n">all_lines</span><span class="p">,</span> <span class="s2">"Hillary Clinton:"</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="n">trump_counter</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">trump_tokens</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"Total number of words spoken by Trump: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">trump_tokens</span><span class="p">)))</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"Number of unique words spoken by Trump: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">trump_counter</span><span class="o">.</span><span class="n">keys</span><span class="p">())))</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"Ratio of unique words to total words: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">trump_counter</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span><span class="o">/</span><span class="nb">len</span><span class="p">(</span><span class="n">trump_tokens</span><span class="p">),</span><span class="mi">3</span><span class="p">)))</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="n">clinton_counter</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">clinton_tokens</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"Total number of words spoken by Clinton: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">clinton_tokens</span><span class="p">)))</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"Number of unique words spoken by Clinton: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">clinton_counter</span><span class="o">.</span><span class="n">keys</span><span class="p">())))</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"Ratio of unique words to total words: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">clinton_counter</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span><span class="o">/</span><span class="nb">len</span><span class="p">(</span><span class="n">clinton_tokens</span><span class="p">),</span><span class="mi">3</span><span class="p">)))</span>
</pre></div>
<div class="highlight"><pre><span></span>Total number of words spoken by Trump: 4153
Number of unique words spoken by Trump: 827
Ratio of unique words to total words: 0.199
Total number of words spoken by Clinton: 3099
Number of unique words spoken by Clinton: 843
Ratio of unique words to total words: 0.272
</pre></div>
<p>The disparity between their respective words spoken is sizable given the relatively tight structure of the debate. Mr. Trump seems to speak more quickly than Secretary Clinton, which is borne out in this dataset. <a href="http://www.nytimes.com/2016/09/28/us/politics/by-the-numbers-debate.html">The New York Times reports</a> he spoke for 44:23 compared to her 41:21, so while he spoke for more time overall, he must speak more quickly as well.</p>
<div class="highlight"><pre><span></span><span class="n">pprint</span><span class="p">(</span><span class="n">trump_counter</span><span class="o">.</span><span class="n">most_common</span><span class="p">(</span><span class="mi">20</span><span class="p">))</span>
</pre></div>
<div class="highlight"><pre><span></span>[('i', 137),
('NEWLINE', 136),
('the', 135),
('to', 129),
('and', 122),
('you', 111),
('that', 78),
('of', 74),
('a', 73),
('have', 71),
('it', 63),
('in', 40),
('we', 39),
('was', 38),
('its', 36),
('but', 36),
('very', 35),
('are', 34),
('not', 32),
('is', 32)]
</pre></div>
<div class="highlight"><pre><span></span><span class="n">pprint</span><span class="p">(</span><span class="n">clinton_counter</span><span class="o">.</span><span class="n">most_common</span><span class="p">(</span><span class="mi">20</span><span class="p">))</span>
</pre></div>
<div class="highlight"><pre><span></span>[('the', 119),
('and', 106),
('NEWLINE', 97),
('to', 90),
('i', 84),
('that', 73),
('a', 67),
('of', 62),
('you', 46),
('in', 44),
('we', 43),
('it', 38),
('well', 34),
('have', 34),
('is', 33),
('be', 33),
('for', 28),
('think', 28),
('not', 24),
('about', 24)]
</pre></div>
<p>As would be expected, these lists are mostly dominated by stop-words. Mr. Trump's most commonly spoken word, 'I', is Secretary Clinton's fourth most commonly spoken.</p>
<div class="highlight"><pre><span></span><span class="n">trump_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">'word'</span><span class="p">:</span><span class="n">trump_tokens</span><span class="p">})</span>
<span class="n">trump_df</span><span class="p">[</span><span class="s1">'wordlength'</span><span class="p">]</span><span class="o">=</span><span class="n">trump_df</span><span class="p">[</span><span class="s1">'word'</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">len</span><span class="p">)</span>
<span class="n">clinton_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">'word'</span><span class="p">:</span><span class="n">clinton_tokens</span><span class="p">})</span>
<span class="n">clinton_df</span><span class="p">[</span><span class="s1">'wordlength'</span><span class="p">]</span><span class="o">=</span><span class="n">clinton_df</span><span class="p">[</span><span class="s1">'word'</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">len</span><span class="p">)</span>
<span class="n">trump_df</span><span class="p">[</span><span class="s1">'wordlength'</span><span class="p">]</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">bins</span><span class="o">=</span><span class="mi">16</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.6</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'red'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">"A histogram of Donald Trump's Word Lengths"</span><span class="p">)</span>
<span class="n">clinton_df</span><span class="p">[</span><span class="s1">'wordlength'</span><span class="p">]</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">bins</span><span class="o">=</span><span class="mi">16</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.6</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'blue'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">"A histogram of Word Lengths (colors are based on party lines)"</span><span class="p">)</span>
</pre></div>
<p><img alt="png" src="https://dswalter.github.io/images/output_12_1.png"></p>
<p>In general, Mr. Trump used shorter words than Secretary Clinton.</p>
<h2>The n-grams part</h2>
<p>What we wanted to know was whether there were phrases that popped up frequently for each candidate, so we analyzed for n=2...10 whether there were repeated phrases. My implementation is naive, as it considers the entire spoken corpus of each candidate as one document. The <strong>find_ngrams</strong> function is a nice little piece of code I found on <a href="http://locallyoptimal.com/blog/2013/01/20/elegant-n-gram-generation-in-python/">Scott Triglia's</a> blog.</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">find_ngrams</span><span class="p">(</span><span class="n">input_list</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="n">input_list</span><span class="p">[</span><span class="n">i</span><span class="p">:]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
<span class="k">def</span> <span class="nf">print_ngrams</span><span class="p">(</span><span class="n">input_tokens</span><span class="p">,</span> <span class="n">n_grams</span><span class="p">,</span> <span class="n">candidate_name</span><span class="p">):</span>
<span class="sd">"""Starts with tokens and ends with only the top 15 n-grams that have been repeated</span>
<span class="sd"> where available"""</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"{}'s {}-grams"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">candidate_name</span><span class="p">,</span> <span class="n">n_grams</span><span class="p">))</span>
<span class="n">found_ngrams</span> <span class="o">=</span> <span class="n">find_ngrams</span><span class="p">(</span><span class="n">input_tokens</span><span class="p">,</span> <span class="n">n_grams</span><span class="p">)</span>
<span class="n">ngramcount</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">found_ngrams</span><span class="p">)</span>
<span class="n">repeat_keys</span> <span class="o">=</span> <span class="p">[</span><span class="n">key</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">ngramcount</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span> <span class="k">if</span> <span class="n">ngramcount</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">>=</span><span class="mi">2</span><span class="p">]</span>
<span class="n">relevant_entries</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">([[</span><span class="n">key</span><span class="p">,</span> <span class="n">ngramcount</span><span class="p">[</span><span class="n">key</span><span class="p">]]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">repeat_keys</span><span class="p">],</span> <span class="n">key</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"{} {}-grams were repeated"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">relevant_entries</span><span class="p">),</span><span class="n">n_grams</span><span class="p">))</span>
<span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">relevant_entries</span><span class="p">[:</span><span class="mi">15</span><span class="p">]:</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"'{}' : {} times "</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s2">" "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">entry</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">"NEWLINE"</span><span class="p">,</span><span class="s2">"-"</span><span class="p">),</span> <span class="n">entry</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="c1">#pprint(relevant_entries[:15])</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">relevant_entries</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="n">trump_repeats</span><span class="p">,</span> <span class="n">clinton_repeats</span> <span class="o">=</span> <span class="p">{},</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">n_gram</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">10</span><span class="p">):</span>
<span class="n">clinton_repeats</span><span class="p">[</span><span class="n">n_gram</span><span class="p">]</span> <span class="o">=</span> <span class="n">print_ngrams</span><span class="p">(</span><span class="n">clinton_tokens</span><span class="p">,</span> <span class="n">n_gram</span><span class="p">,</span> <span class="s1">'Hillary Clinton'</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span>Hillary Clinton's 2-grams
326 2-grams were repeated
'i think' : 27 times
'- well' : 24 times
'you know' : 14 times
'and i' : 14 times
'to be' : 13 times
'a lot' : 11 times
'i have' : 11 times
'of the' : 10 times
'- and' : 10 times
'and the' : 10 times
'in the' : 10 times
'well i' : 8 times
'to do' : 8 times
'we need' : 8 times
'need to' : 8 times
Hillary Clinton's 3-grams
111 3-grams were repeated
'a lot of' : 7 times
'and i think' : 7 times
'- well i' : 7 times
'we need to' : 6 times
'we should be' : 4 times
'the middle class' : 4 times
'need to do' : 4 times
'well i think' : 4 times
'the united states' : 3 times
'- and i' : 3 times
'i think we' : 3 times
'to the debt' : 3 times
'of the united' : 3 times
'i wrote about' : 3 times
'what kind of' : 3 times
Hillary Clinton's 4-grams
32 4-grams were repeated
'we need to do' : 4 times
'- well i think' : 4 times
'taxes on the wealthy' : 3 times
'and i think its' : 3 times
'of the united states' : 3 times
'a lot - well' : 2 times
'and the kind of' : 2 times
'million people lost their' : 2 times
'- there are different' : 2 times
'i think we need' : 2 times
'5 trillion to the' : 2 times
'thats a lot of' : 2 times
'add 5 trillion to' : 2 times
'they dont give you' : 2 times
'and you know what' : 2 times
Hillary Clinton's 5-grams
8 5-grams were repeated
'add 5 trillion to the' : 2 times
'and i think its important' : 2 times
'would add 5 trillion to' : 2 times
'what i have proposed would' : 2 times
'different - there are different' : 2 times
'there are different - there' : 2 times
'5 trillion to the debt' : 2 times
'are different - there are' : 2 times
Hillary Clinton's 6-grams
4 6-grams were repeated
'there are different - there are' : 2 times
'would add 5 trillion to the' : 2 times
'are different - there are different' : 2 times
'add 5 trillion to the debt' : 2 times
Hillary Clinton's 7-grams
2 7-grams were repeated
'would add 5 trillion to the debt' : 2 times
'there are different - there are different' : 2 times
Hillary Clinton's 8-grams
0 8-grams were repeated
Hillary Clinton's 9-grams
0 9-grams were repeated
</pre></div>
<h3>A few phrases seem notably rehearsed:</h3>
<div class="highlight"><pre><span></span>'taxes on the wealthy' : 3 times
'million people lost their' [jobs] : 2 times
'would add 5 trillion to the debt' : 2 times
</pre></div>
<h3>Then there is the repeated phrase:</h3>
<div class="highlight"><pre><span></span>'there are different - there are different' : 2 times,
</pre></div>
<p>which may have come during an interruption. It's interesting to note that there are no 8 or 9-grams.</p>
<div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">n_gram</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">10</span><span class="p">):</span>
<span class="n">trump_repeats</span><span class="p">[</span><span class="n">n_gram</span><span class="p">]</span> <span class="o">=</span> <span class="n">print_ngrams</span><span class="p">(</span><span class="n">trump_tokens</span><span class="p">,</span> <span class="n">n_gram</span><span class="p">,</span> <span class="s1">'Donald Trump'</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span>Donald Trump's 2-grams
614 2-grams were repeated
'going to' : 23 times
'and i' : 18 times
'have to' : 16 times
'you have' : 16 times
'- well' : 14 times
'want to' : 14 times
'of the' : 14 times
'i was' : 13 times
'our country' : 12 times
'i dont' : 12 times
'- i' : 12 times
'tell you' : 11 times
'we have' : 11 times
'look at' : 11 times
'- and' : 11 times
Donald Trump's 3-grams
329 3-grams were repeated
'i want to' : 7 times
'you have to' : 6 times
'theyre going to' : 6 times
'you look at' : 6 times
'will tell you' : 5 times
'are going to' : 5 times
'law and order' : 5 times
'we have a' : 4 times
'the other day' : 4 times
'we need law' : 4 times
'you are going' : 4 times
'you want to' : 4 times
'because i want' : 4 times
'i will tell' : 4 times
'want to get' : 4 times
Donald Trump's 4-grams
153 4-grams were repeated
'need law and order' : 4 times
'you are going to' : 4 times
'we need law and' : 4 times
'to be able to' : 4 times
'because i want to' : 4 times
'want to get on' : 4 times
'to get on to' : 4 times
'i will tell you' : 4 times
'i want to get' : 4 times
'the kind of thinking' : 3 times
'let me tell you' : 3 times
'a great thing for' : 3 times
'president obamas fault -' : 3 times
'it president obamas fault' : 3 times
'you have to be' : 3 times
Donald Trump's 5-grams
77 5-grams were repeated
'we need law and order' : 4 times
'i want to get on' : 4 times
'because i want to get' : 4 times
'want to get on to' : 4 times
'of thinking that our country' : 3 times
'have to be able to' : 3 times
'thinking that our country needs' : 3 times
'the kind of thinking that' : 3 times
'i was against the war' : 3 times
'where did you find this' : 3 times
'is it president obamas fault' : 3 times
'you have to be able' : 3 times
'kind of thinking that our' : 3 times
'it president obamas fault -' : 3 times
'whether its the iran deal' : 2 times
Donald Trump's 6-grams
36 6-grams were repeated
'i want to get on to' : 4 times
'because i want to get on' : 4 times
'is it president obamas fault -' : 3 times
'kind of thinking that our country' : 3 times
'the kind of thinking that our' : 3 times
'of thinking that our country needs' : 3 times
'you have to be able to' : 3 times
'- where did you find this' : 2 times
'youre telling the enemy everything you' : 2 times
'they have to help us out' : 2 times
'enemy everything you want to do' : 2 times
'where did you find this -' : 2 times
'have to be able to negotiate' : 2 times
'she spent hundreds of millions of' : 2 times
'- you called it the gold' : 2 times
Donald Trump's 7-grams
14 7-grams were repeated
'because i want to get on to' : 4 times
'the kind of thinking that our country' : 3 times
'kind of thinking that our country needs' : 3 times
'spent hundreds of millions of dollars on' : 2 times
'telling the enemy everything you want to' : 2 times
'when i look at whats going on' : 2 times
'the enemy everything you want to do' : 2 times
'- you called it the gold standard' : 2 times
'i want to make america great again' : 2 times
'she spent hundreds of millions of dollars' : 2 times
'you have to be able to negotiate' : 2 times
'should have been doing this for years' : 2 times
'thats the kind of thinking that our' : 2 times
'youre telling the enemy everything you want' : 2 times
Donald Trump's 8-grams
5 8-grams were repeated
'the kind of thinking that our country needs' : 3 times
'youre telling the enemy everything you want to' : 2 times
'telling the enemy everything you want to do' : 2 times
'she spent hundreds of millions of dollars on' : 2 times
'thats the kind of thinking that our country' : 2 times
Donald Trump's 9-grams
2 9-grams were repeated
'youre telling the enemy everything you want to do' : 2 times
'thats the kind of thinking that our country needs' : 2 times
</pre></div>
<h3>A few phrases seem notably rehearsed:</h3>
<div class="highlight"><pre><span></span>'we need law and order' : 4 times
'whether its the iran deal' : 2 times
'when i look at whats going on' : 2 times
'- you called it the gold standard' : 2 times
'i want to make america great again' : 2 times
'you have to be able to negotiate' : 2 times
'should have been doing this for years' : 2 times
'she spent hundreds of millions of dollars on' : 2 times
'youre telling the enemy everything you want to do' : 2 times
'thats the kind of thinking that our country needs' : 2 times
</pre></div>
<p>Mr. Trump had two repeated 9-grams, which are pretty sizable pieces of text. The last four n-grams all sound like they were soundbites that had been chosen specifically for this debate.</p>
<p>Finally, I thought it would be worth looking at the total number of n-grams for each speaker.</p>
<div class="highlight"><pre><span></span><span class="n">trump_repeat</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">trump_repeats</span><span class="p">)</span>
<span class="n">trump_repeat</span> <span class="o">=</span> <span class="n">trump_repeat</span><span class="o">/</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">trump_tokens</span><span class="p">)</span><span class="o">/</span><span class="nb">len</span><span class="p">(</span><span class="n">clinton_tokens</span><span class="p">))</span>
<span class="n">trump_repeat</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">kind</span><span class="o">=</span><span class="s1">'bar'</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'red'</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s1">'Number of repeated n-grams for Donald Trump'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">500</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="n">clinton_repeat</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">clinton_repeats</span><span class="p">)</span>
<span class="n">clinton_repeat</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">kind</span><span class="o">=</span><span class="s1">'bar'</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'blue'</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s1">'Number of repeated n-grams for Hillary Clinton'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">500</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
<p><img alt="png" src="https://dswalter.github.io/images/output_20_0.png"></p>
<p><img alt="png" src="https://dswalter.github.io/images/output_20_1.png"></p>
<p>For the images above, I accounted for the difference in total words spoken between the two by scaling Mr. Trump's counts by the same factor his total tokens outnumbered Secretary Clinton's. It is immmediately clear that Mr. Trump had more repeated phrase chunks of every size.</p>
<h1>Conclusions?</h1>
<p>From this cursory n-gram-based method of analysis, it appears that Mr. Trump had more repeated phrases, even relatively longer phrases, than Secretary Clinton. One explanation could be that Mr. Trump's debate preparation may have emphasized word-for-word talking points more than Secretary Clinton's.</p> </article>
</aside><!-- /#featured -->
<section id="content" class="body">
<h1>Other articles</h1>
<hr />
<ol id="posts-list" class="hfeed">
<li><article class="hentry">
<header>
<h1><a href="https://dswalter.github.io/best-things-of-2015.html" rel="bookmark"
title="Permalink to Best things of 2015">Best things of 2015</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2016-01-01T00:00:00-05:00">
Published: Fri 01 January 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://dswalter.github.io/author/daniel-walter.html">Daniel Walter</a>
</address>
<p>In <a href="https://dswalter.github.io/category/blog.html">blog</a>.</p>
</footer><!-- /.post-info --> <p>Here are a few things I appreciated in 2015:</p>
<h3>arXiv papers:</h3>
<ul>
<li>
<p><a href="http://arxiv.org/abs/1502.03167">Batch Normalization</a>- a simple conceptual idea beautifully executed. We've known for years that neural networks respond better to input data that follow certain distributions. Since the output of each layer is the input to the next, it's a good …</p></li></ul>
<a class="readmore" href="https://dswalter.github.io/best-things-of-2015.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="https://dswalter.github.io/machine-learnings-first-cheating-scandal.html" rel="bookmark"
title="Permalink to A Look into Machine Learning's First Cheating Scandal">A Look into Machine Learning's First Cheating Scandal</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2015-12-08T00:00:00-05:00">
Published: Tue 08 December 2015
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://dswalter.github.io/author/daniel-walter.html">Daniel Walter</a>
</address>
<p>In <a href="https://dswalter.github.io/category/blog.html">blog</a>.</p>
</footer><!-- /.post-info --> <p>At the end of May, the ImageNet Large Scale Visual Recognition Competition, or <a href="http://www.image-net.org/challenges/LSVRC/">LSVRC</a> (currently the word's top annual computer vision contest) announced that a team had violated competition policies by over-submitting results. The <a href="http://www.image-net.org/challenges/LSVRC/announcement-June-2-2015">follow-up post</a> named a team from Baidu (the Chinese search giant) as the perpetrators of this …</p>
<a class="readmore" href="https://dswalter.github.io/machine-learnings-first-cheating-scandal.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="https://dswalter.github.io/overfitting-regularization-hyperparameters.html" rel="bookmark"
title="Permalink to Overfitting, Regularization, and Hyperparameters">Overfitting, Regularization, and Hyperparameters</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2015-11-29T00:00:00-05:00">
Published: Sun 29 November 2015
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://dswalter.github.io/author/daniel-walter.html">Daniel Walter</a>
</address>
<p>In <a href="https://dswalter.github.io/category/blog.html">blog</a>.</p>
</footer><!-- /.post-info --> <p>(This lays the ground work for the next post, which was getting too long to be effective.)</p>
<h3>Overfitting</h3>
<p>One of the goals of machine learning is generalizability. A model that only works on the exact data it was trained on is effectively useless. Let's say you're tasked with creating a …</p>
<a class="readmore" href="https://dswalter.github.io/overfitting-regularization-hyperparameters.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="https://dswalter.github.io/machine-learning-is-magic.html" rel="bookmark"
title="Permalink to Machine Learning is Magic">Machine Learning is Magic</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2015-10-02T00:00:00-04:00">
Published: Fri 02 October 2015
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://dswalter.github.io/author/daniel-walter.html">Daniel Walter</a>
</address>
<p>In <a href="https://dswalter.github.io/category/blog.html">blog</a>.</p>
</footer><!-- /.post-info --> <p>Perhaps you have heard of the sophisticated, elegant <a href="https://github.com/panicsteve/cloud-to-butt">Cloud-to-Butt plugin</a>, which searches through the text on a website for the phrase "The Cloud" and replaces it with "My Butt". <a href="http://gizmodo.com/the-best-of-cloud-to-butt-the-only-extension-youll-eve-1685863609">This compilation</a> shows some of the most urbane results.</p>
<p>It struck me that if you replaced "Machine Learning Algorithm" with "Magic …</p>
<a class="readmore" href="https://dswalter.github.io/machine-learning-is-magic.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="https://dswalter.github.io/talking-talking-machines.html" rel="bookmark"
title="Permalink to Talking Talking Machines">Talking Talking Machines</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2015-09-17T00:00:00-04:00">
Published: Thu 17 September 2015
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://dswalter.github.io/author/daniel-walter.html">Daniel Walter</a>
</address>
<p>In <a href="https://dswalter.github.io/category/blog.html">blog</a>.</p>
</footer><!-- /.post-info --> <p><a href="http://www.thetalkingmachines.com/">Talking Machines</a> is currently THE machine learning podcast. Last fall when it started out, I was intrigued that someone was starting a podcast focusing specifically on machine learning. But I didn't really listen until this summer, which was my loss. Now that I'm catching up on it, I think it's …</p>
<a class="readmore" href="https://dswalter.github.io/talking-talking-machines.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="https://dswalter.github.io/why-blog.html" rel="bookmark"
title="Permalink to Why Blog?">Why Blog?</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2015-08-18T00:00:00-04:00">
Published: Tue 18 August 2015
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://dswalter.github.io/author/daniel-walter.html">Daniel Walter</a>
</address>
<p>In <a href="https://dswalter.github.io/category/blog.html">blog</a>.</p>
</footer><!-- /.post-info --> <p>A question anyone starting a blog in 2015 should answer: </p>
<h5>Why?</h5>
<p>Like a lot of people, I think things through more thoroughly and with more discipline when writing them down. If I just consider a topic, I can tend to get into habits of thought. So the act of writing …</p>
<a class="readmore" href="https://dswalter.github.io/why-blog.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
</ol><!-- /#posts-list -->
</section><!-- /#content -->
<section id="extras" class="body">
<div class="blogroll">
<h2>links</h2>
<ul>
<li><a href="http://getpelican.com/">Pelican</a></li>
<li><a href="http://python.org/">Python.org</a></li>
<li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
<li><a href="#">You can modify those links in your config file</a></li>
</ul>
</div><!-- /.blogroll -->
<div class="social">
<h2>social</h2>
<ul>
<li><a href="https://dswalter.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate">atom feed</a></li>
<li><a href="#">You can add links in your config file</a></li>
<li><a href="#">Another social link</a></li>
</ul>
</div><!-- /.social -->
</section><!-- /#extras -->
<footer id="contentinfo" class="body">
<address id="about" class="vcard body">
Proudly powered by <a href="http://getpelican.com/">Pelican</a>, which takes great advantage of <a href="http://python.org">Python</a>.
</address><!-- /#about -->
<p>The theme is by <a href="http://coding.smashingmagazine.com/2009/08/04/designing-a-html-5-layout-from-scratch/">Smashing Magazine</a>, thanks!</p>
</footer><!-- /#contentinfo -->
</body>
</html>