-
Notifications
You must be signed in to change notification settings - Fork 20
/
WUSTL_1.tex
1267 lines (886 loc) · 35.9 KB
/
WUSTL_1.tex
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass{beamer}
%\usepackage[table]{xcolor}
\mode<presentation> {
\usetheme{Boadilla}
% \usetheme{Pittsburgh}
%\usefonttheme[2]{sans}
\renewcommand{\familydefault}{cmss}
%\usepackage{lmodern}
%\usepackage[T1]{fontenc}
%\usepackage{palatino}
%\usepackage{cmbright}
\setbeamercovered{transparent}
\useinnertheme{rectangles}
}
%\usepackage{normalem}{ulem}
%\usepackage{colortbl, textcomp}
\setbeamercolor{normal text}{fg=black}
\setbeamercolor{structure}{fg= black}
\definecolor{trial}{cmyk}{1,0,0, 0}
\definecolor{trial2}{cmyk}{0.00,0,1, 0}
\definecolor{darkgreen}{rgb}{0,.4, 0.1}
\usepackage{array}
\beamertemplatesolidbackgroundcolor{white} \setbeamercolor{alerted
text}{fg=red}
\setbeamertemplate{caption}[numbered]\newcounter{mylastframe}
%\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{colortbl}
%\usepackage[usenames, dvipsnames]{color}
%\setbeamertemplate{caption}[numbered]\newcounter{mylastframe}c
%\newcolumntype{Y}{\columncolor[cmyk]{0, 0, 1, 0}\raggedright}
%\newcolumntype{C}{\columncolor[cmyk]{1, 0, 0, 0}\raggedright}
%\newcolumntype{G}{\columncolor[rgb]{0, 1, 0}\raggedright}
%\newcolumntype{R}{\columncolor[rgb]{1, 0, 0}\raggedright}
%\begin{beamerboxesrounded}[upper=uppercol,lower=lowercol,shadow=true]{Block}
%$A = B$.
%\end{beamerboxesrounded}}
\renewcommand{\familydefault}{cmss}
%\usepackage[all]{xy}
\usepackage{tikz}
\usepackage{lipsum}
\newenvironment{changemargin}[3]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\topmargin}{#3}%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]}{\end{list}}
\usetikzlibrary{arrows}
%\usepackage{palatino}
%\usepackage{eulervm}
\usecolortheme{lily}
\newtheorem{com}{Comment}
\newtheorem{lem} {Lemma}
\newtheorem{prop}{Proposition}
\newtheorem{thm}{Theorem}
\newtheorem{defn}{Definition}
\newtheorem{cor}{Corollary}
\newtheorem{obs}{Observation}
\numberwithin{equation}{section}
%\usepackage[latin1]{inputenc}
\title[Text as Data] % (optional, nur bei langen Titeln nötig)
{Text as Data}
\author{Justin Grimmer}
\institute[University of Chicago]{Associate Professor\\Department of Political Science \\ University of Chicago}
\vspace{0.3in}
\date{November 6th, 2017}%[Big Data Workshop]
%\date{\today}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
%%Introduction: who is in the class
%%what are they working on
\begin{frame}
\frametitle{Text and Political Science}
A pre-2000's view of text in social science
\begin{itemize}
\item[-] Social interaction often occurs in texts \pause
\invisible<1>{\item[-] Social Scientists avoided studying texts/speech} \pause
\invisible<1-2>{\item[-] Why?} \pause
\begin{itemize}
\invisible<1-3>{\item[-] Hard to find} \pause
\invisible<1-4>{\item[-] Time Consuming} \pause
\invisible<1-5>{\item[-] Not generalizable (each new data set...new coding scheme)} \pause
\invisible<1-6>{\item[-] Difficult to store/search} \pause
\invisible<1-7>{\item[-] Idiosyncratic to coders/researcher} \pause
\invisible<1-8>{\item[-] Statistical methods/algorithms, computationally intensive}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
A post-2000's view of text in social science:
\vspace{0.25in}
\invisible<1>{ Massive collections of texts are increasingly used as a data source in social science: }
\begin{itemize}
\invisible<1-2>{\item[-] Congressional speeches, press releases, newsletters, ...}
\invisible<1-3>{\item[-] Facebook posts, tweets, emails, cell phone records, ...}
\invisible<1-4>{\item[-] Newspapers, magazines, news broadcasts, ... }
\invisible<1-5>{\item[-] Foreign news sources, treaties, sermons, fatwas, ...}
\end{itemize}
\pause \pause \pause \pause \pause
\end{frame}
\begin{frame}
%
\footnotesize
Why? \pause
\begin{itemize}
\invisible<1>{\item[-] Massive increase in availability of unstructured text (10 minutes of worldwide email = 1 LOC )} \pause
\invisible<1-2>{\item[-] Cheap storage: 1956: \$10,000 megabyte. 2014: $<<<<<$ \$0.0001 per megabyte (Unless you're sending an SMS) } \pause
\invisible<1-3>{\item[-] Explosion in methods and programs to analyze texts} \pause
\begin{itemize}
\invisible<1-4>{\item[-] Generalizable: one method can be used across many methods and to unify collections of texts} \pause
\invisible<1-5>{\item[-] Systematic: parameters/statistics demonstrate how models make coding decisions} \pause
\invisible<1-6>{\item[-] Cheap: easily applied to many new collections of texts, computing power is inexpensive} \pause
\end{itemize}
\invisible<1-7>{\item[-] \alert{Unchanged Demand}: Social life (politics, economic exchanges, social interactions) occurs in \alert{texts} } \pause
\begin{itemize}
\invisible<1-8>{\item[-] Laws} \pause
\invisible<1-9>{\item[-] Treaties } \pause
\invisible<1-10>{\item[-] News media} \pause
\invisible<1-11>{\item[-] Campaigns} \pause
\invisible<1-12>{\item[-] Political pundits} \pause
\invisible<1-13>{\item[-] Petitions } \pause
\invisible<1-14>{\item[-] Press Releases} \pause
\invisible<1-15>{\item[-] ...}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{What Can Text Methods Do?}
Haystack metaphor:\pause \invisible<1>{\alert{ Improve Reading}} \pause
\begin{itemize}
\invisible<1-2>{\item[-] Interpreting the meaning of a sentence or phrase $\leadsto$ Analyzing a straw of hay} \pause
\begin{itemize}
\invisible<1-3>{\item[-]Humans: amazing (Straussian political theory, analysis of English poetry)
\item[-] Computers: struggle } \pause
\end{itemize}
\invisible<1-4>{\item[-] Comparing, Organizing, and Classifying Texts$\leadsto$ Organizing hay stack} \pause
\begin{itemize}
\invisible<1-5>{\item[-] Humans: terrible. Tiny active memories
\item[-] Computers: amazing$\leadsto$ largely what we'll discuss today} \pause
\end{itemize}
\end{itemize}
\invisible<1-6>{What automated text methods don't do:} \pause
\begin{itemize}
\invisible<1-7>{\item[-] Develop a comprehensive statistical model of language
\item[-] Replace the need to read
\item[-] Develop a single tool + evaluation for all tasks }
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Texts are Deceptively Complex}
{\tt We've got some difficult days ahead. But it doesn't matter with me now. Because I've been to the mountaintop. And I don't mind. Like anybody, I would like to live a long life. Longevity has its place. But I'm not concerned about that now. }
\pause
\begin{itemize}
\invisible<1>{\item[-] Who is the {\tt I} ?} \pause
\invisible<1-2>{\item[-] Who is the {\tt We}? } \pause
\invisible<1-3>{\item[-] What is the {\tt mountaintop} (literal?)} \pause
\end{itemize}
\invisible<1-4>{Texts$\leadsto$ high dimensional, not self contained}
\end{frame}
\begin{frame}
\frametitle{Texts are Surprisingly Simple}
(Lamar Alexander (R-TN) Feb 10, 2005)
\begin{table}
\begin{tabular}{ll}
\hline
Word & No. Times Used in Press Release \\
\hline
department & 12\\
grant & 9 \\
program & 7 \\
firefight & 7 \\
secure & 5 \\
homeland & 4 \\
fund & 3 \\
award & 2 \\
safety & 2 \\
service & 2 \\
AFGP & 2 \\
support & 2 \\
equip & 2 \\
applaud & 2\\
assist & 2\\
prepared & 2 \\
\hline
\end{tabular}
\end{table}
\end{frame}
\begin{frame}
\frametitle{Texts are Surprisingly Simple (?)}
{\tt US Senators Bill Frist (R-TN) and Lamar Alexander (R-TN) today applauded the U S Department of Homeland Security for awarding a \$8,190 grant to the Tracy City Volunteer Fire Department under the 2004 Assistance to Firefighters Grant Program's (\alert{AFGP}) Fire Prevention and Safety Program...}
\end{frame}
\begin{frame}
\frametitle{Not just for ``big data" }
\pause
\invisible<1>{Manually develop categorization scheme for partitioning small (100) set of documents} \pause
\begin{itemize}
\invisible<1-2>{ \item[-] Bell$(n) = $ number of ways of partitioning $n$ objects}
\pause
\invisible<1-3>{ \item[-] Bell$(2)=2$ (AB, A B)}\pause
\invisible<1-4>{ \item[-] Bell$(3)=5$ (ABC, AB C, A BC, AC B, A B
C)} \pause
\invisible<1-5>{ \item[-] Bell$(5)=52$}\pause
\invisible<1-6>{ \item[-] Bell(100)}\pause \invisible<1-7>{$\approx 4.75 \times
10^{115}$ partitions} \pause
\invisible<1-8>{\item[-] \alert{Big Number}:}\pause \\
\invisible<1-9>{7 Billion RAs} \pause \\
\invisible<1-10>{ Impossibly Fast (enumerate one clustering every millisecond)} \pause \\
\invisible<1-11>{ Working around the clock (24/7/365)}\pause \\
\invisible<1-12>{$\approx 1.54\times 10^{84} \times $} \invisible<1-13>{($14,000,000,000$)} \invisible<1-14>{years}
\pause \pause\pause
\end{itemize}
\invisible<1-15>{\alert{Automated methods can help with even small problems}}
\end{frame}
\begin{frame}
\frametitle{Plan for the Course}
\begin{itemize}
\item[1)] 11/6, Morning: Acquiring, preprocessing, and comparing text
\item[2)] 11/6, Afternoon: \textbf{Discovery}: Vector Space Model of Text, Clustering Methods, Separating Words
\item[3)] 11/7, Morning: \textbf{Measurement}: Dictionary Methods, Hand Coding, Supervised Methods Part 1
\item[4)] 11/7, Afternoon: \textbf{Measurement}: Topic Models
\item[5)] 11/8, Morning (and Talk): \textbf{Causal Inference}: Train/Test Split, Analyst Induced SUTVA, Text as Dependent and Independent
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Four Principles of Automated Text Analysis}
Principle 1: All Quantitative Models of Language are Wrong---But Some are Useful \pause
\begin{itemize}
\invisible<1>{\item[-] Data generation process for text$\leadsto$ unknown} \pause
\invisible<1-2>{\item[-] Complexity of language:} \pause
\begin{itemize}
\invisible<1-3>{\item[-] Time flies like an arrow}\pause\invisible<1-4>{, fruit flies like a banana} \pause
\invisible<1-5>{\item[-] Make peace, not war}\pause\invisible<1-6>{ , Make war not peace (Spirling, 2013)} \pause
\invisible<1-7>{\item[-] ``Years from now, you'll look back and you'll say that this was the moment, this was the place where America remembered what it means to hope. " } \pause
\end{itemize}
\invisible<1-8>{\item[-] Models \alert{necessarily} fail to capture language$\leadsto$ useful for specific tasks}\pause
\invisible<1-9>{\item[-] \alert{Validation}$\leadsto$ demonstrate methods perform task }
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Four Principles of Automated Text Analysis}
Principle 2: Quantitative Methods Augment Humans, Not Replace Them \pause
\begin{itemize}
\invisible<1>{\item[-] \alert{Computer-Assisted} Reading} \pause
\invisible<1-2>{\item[-] Quantitative methods organize, direct, and suggest} \pause
\invisible<1-3>{\item[-] Humans: read and interpret }
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Four Principles of Automated Text Analysis}
Principle 3: There is no Globally Best Method for Automated Text Analysis \pause
\begin{itemize}
\invisible<1>{\item[-] Supervised methods$\leadsto$ known categories} \pause
\invisible<1-2>{\item[-] Unsupervised methods$\leadsto$ discover categories} \pause
\invisible<1-3>{\item[-] Debate$\leadsto$ acknowledge differences, resolved}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Four Principles of Automated Text Analysis}
Principle 4: Validate, Validate, Validate \pause
\begin{itemize}
\invisible<1>{\item[-] Quantitative methods$\leadsto$ variable performance across tasks} \pause
\invisible<1-2>{\item[-] \alert{Few theorems to guarantee performance}} \pause
\invisible<1-3>{\item[-] Apply methods $\leadsto$ validate} \pause
\invisible<1-4>{\item[-] \alert{Avoid}: blind application of methods}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Goal for Today: Document-Term Matrices}
\begin{eqnarray}
\boldsymbol{X} & = & \begin{pmatrix}
1 & 0 & 0 & \hdots & 3 \\
0 & 2 & 1 & \hdots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
0 & 0 & 0 & \hdots & 5 \\
\end{pmatrix} \nonumber
\end{eqnarray}
$\boldsymbol{X} = \alert<2>{N} \times \alert<3>{J}$ matrix
\begin{itemize}
\invisible<1>{\item[-] $N = $ Number of documents}
\invisible<1-2>{\item[-] $J = $ Number of features}
\end{itemize}
\pause \pause
\end{frame}
\begin{frame}
\frametitle{Learning From Text}
A plan for using texts
\begin{itemize}
\item[1)] Acquiring text data
\item[2)] Regular expression search in text
\item[3)] Creating document-term matrices (term-document matrices)
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Finding Text Data }
Many places to find text \pause
\invisible<1>{Goal: plain text (.txt) file. (UTF-8, ASCII) \\} \pause
\invisible<1-2>{(May also want to create an {\tt XML} or {\tt JSON} file) }
\end{frame}
\begin{frame}
\frametitle<1>{Plain Text}
\frametitle<2>{XML}
\only<1>{
{\tt
September 19, 2010 Sunday 10:46 AM EST\\
REP. FOXX VISITS LOCAL SCHOOLS, TALKS WITH STUDENTS ON CONSTITUTION DAY \\
LENGTH: 320 words\\
CLEMMONS, N.C., Sept. 17 -- Rep. Virginia Foxx, R-N.C. (5th CD), issued the following press release:\\
Congresswoman Virginia Foxx is celebrating Constitution Day today by visiting several schools in her district to talk with students about the Constitution and the individuals who helped create our charter document. She will visit Davie County High School, Forbush High School in Yadkin County and Piney Creek School in Alleghany County.
}
}
\only<2>{
{\tt
$<$DOC$>$\\
$<$DOCNO$>$101-levin-mi-1-19901027$<\//$DOCNO$>$\\
$<$TEXT$>$\\
Mr. LEVIN. Mr. President, today the House passed and sent to the President the Great Lakes Critical Programs Act. ... Mr. President, I commend and thank Ms. Bean for her exceptional efforts on the Great Lakes Critical Programs Act\\
$<\//$TEXT$>$\\
$<\//$DOC$>$
}
}
\end{frame}
\begin{frame}
\frametitle{JSON}
{\tt
\{"id":"tag:search.twitter.com,2005:287886850381713411",\\"objectType":"activity"...displayName":"Linda Bowersox",\\"postedTime":"2010-03-10T05:16:14.000Z"...\\"body":"@JeffFlake thank you for standing firm and voting NO on the \#FiscalCliff (via \#PJNET)","object"...
}
\end{frame}
\begin{frame}
\frametitle{Prepackaged Data Sources}
{\tt http://dfr.jstor.org}
\scalebox{0.5}{\includegraphics{dfr.png}}
\end{frame}
\begin{frame}
\frametitle{History of Home Style}
\only<1>{\scalebox{0.4}{\includegraphics{Home1.pdf}} }
\only<2>{\scalebox{0.4}{\includegraphics{Home2.pdf}} }
\only<3>{\scalebox{0.4}{\includegraphics{Home3.pdf}} }
\only<4>{\scalebox{0.4}{\includegraphics{Vote1.pdf}} }
\only<5>{\scalebox{0.4}{\includegraphics{Vote2.pdf}} }
\only<6>{\scalebox{0.4}{\includegraphics{Vote3.pdf}} }
\end{frame}
\begin{frame}
\frametitle{Prepackaged Data Sources}
Lexis Nexis (and other data base sources) \pause
\begin{itemize}
\invisible<1>{\item[1)] Batch search and download} \pause
\invisible<1-2>{\item[2)] \alert{Do not try to scrape Lexis Nexis(!!!!)}} \pause
\end{itemize}
\invisible<1-3>{Application Programming Interface (APIs)} \pause
\begin{itemize}
\invisible<1-4>{\item[-] Facilitate interaction with applications (like Twitter)} \pause
\invisible<1-5>{\item[-] Download data (often in JSON format)$\leadsto$ Twitter, Data.gov, ...}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Books, Archives, and Other Non-Digital Material}
\only<1>{\scalebox{0.35}{\includegraphics{ScannedBook.png}}}
\only<2->{
\begin{itemize}
\item[1)] Create images of texts
\invisible<1-2>{\item[2)] \alert{O}ptical \alert{C}haracter \alert{R}ecognition}
\begin{itemize}
\invisible<1-3>{\item[-] Built in Adobe Pro}
\invisible<1-4>{\item[-] Abbyy FineReader (Batch processing)}
\invisible<1-5>{\item[-] Tesseract (Google, command line tool)}
\end{itemize}
\invisible<1-6>{\item[3)] Also use, e-book formats...}
\end{itemize}
}
\pause \pause \pause \pause \pause \pause
\end{frame}
\begin{frame}
\frametitle{Acquiring Data from Web: Automated Web Collection}
\only<1>{\scalebox{0.5}{\includegraphics{tonko_press.png}}}
\only<2>{\scalebox{0.3}{\includegraphics{TonkoSource.png}}}
\only<3>{\begin{center}\scalebox{0.4}{\includegraphics{TonkoPyCode.png}} \end{center}}
\only<4-5>{\scalebox{0.35}{\includegraphics{TonkoPress.png}}}
%07Nov2009Tonko6.txt
\invisible<1-4>{Exercise: Scraping a Presidential Speech {\tt http://stanford.edu/$\sim$jgrimmer/Text14/HW2.pdf} :
\begin{itemize}
\item[-] {\tt http://www.crummy.com/software/BeautifulSoup/}
\item[-] Parse paragraphs, label speakers
\end{itemize}
}
\end{frame}
\begin{frame}
\frametitle{Acquiring Data from Web: Distributed Human Computing}
Amazon.com's \alert{Mechanical Turk}
\begin{itemize}
\item[-] Marketplace for \alert{H}uman \alert{I}tensive \alert{T}asks
\item[-] Requester (you): create HITs, offer \$ (about \$0.05 per task)
\item[-] Workers (bored + broke people): complete task
\item[-] Requester: evaluate and pay
\end{itemize}
Odesk, elance, ...
\end{frame}
\begin{frame}
\huge
You have text, now what?
\end{frame}
\begin{frame}
\frametitle{Regular Expressions (from Jurafsky Slides) }
\begin{center}
\scalebox{0.35}{\includegraphics{RegExXKCD.png}}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Systematic Searches}
A language for searching texts:
\begin{itemize}
\item[-] Count mentions of a person
\item[-] Calculate amount of money discussed
\item[-] Prepare texts for analysis: Identify where to ``split" a document
\item[-] ...
\end{itemize}
Provide a quick introduction here, with some examples
\end{frame}
\begin{frame}
\frametitle{Regular Expressions, Some Basics (from Jurafsky Slides) }
%%Do the slides where there are the basics of how to computer regular expressions
%%False Negative/ False positive rates.
%%Include cheating detection as a closely related tool.
%%Show how to apply this with a few examples before launching in earnest to the bag of words
%%Then, show how to do this in both python and R
%%python has the advantage of being easier to write files
%%the R example can show how to count incidence of something happening
%%wcopyfind for uptake and joint press releases
\begin{itemize}
\item[-] Disjunctions
\end{itemize}
\begin{center}
\begin{tabular} {lll}
\textbf{RE} & \textbf{Match} & \textbf{Example Patterns Matched}\\
{\tt [mM]oney } & Money or money & ``\underline{Money}" \\
{\tt [abc] } & `a', `b', \emph{or} `c' & ``Investing in Ir\underline{a}n" \\
& & ``is d\underline{a}ngerous \underline{b}usiness"\\
{\tt [1234567890]} & any digit & ``sitting on \$\underline{7}.\underline{5} billion dollars" \\
& & ``\underline{2}\underline{0}\underline{0}\underline{5} and \underline{2}\underline{0}\underline{0}\underline{6}, more than " \\
& & ``\$\underline{1}\underline{5}\underline{0} million dollars" \\
{\tt [$\backslash$.] } & A period &`` `Run!', he screamed\underline{.}"
\end{tabular}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Regular Expressions, Some Basics (from Jurafsky Slides) }
\begin{itemize}
\item[-] Ranges
\end{itemize}
\begin{center}
\begin{tabular} {lll}
\textbf{RE} & \textbf{Match} & \textbf{Example Patterns Matched}\\
{\tt [A-Z]} & an upper case letter & ``\underline{R}ep. \underline{A}nthony \underline{W}einer\\
& & (\underline{D}-\underline{B}rooklyn \& \underline{Q}ueens)" \\
{\tt [a-z]} & a lower case letter & ``ACORN'\underline{s}" \\
{\tt [0-9]} & a single digit & ``(\underline{9}th CD) "
\end{tabular}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Regular Expressions, Some Basics (from Jurafsky Slides) }
\begin{itemize}
\item[-] Negations
\end{itemize}
\begin{center}
\begin{tabular}{lll}
\textbf{RE} & \textbf{Match} & \textbf{Example Patterns Matched}\\
{\tt [\^{}A-Z] } & not an upper case letter & ``ACORN\alert{\underline{'}\underline{s}}" \\
{\tt[\^{}Ss] } & neither `S' nor `s' & ``\alert{\underline{ACORN'}}s" \\
{\tt[\^{}\textbackslash.] } & not a period & `` `\underline{Run!', he screamed}." \\
\end{tabular}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Regular Expressions, Some Basics (from Jurafsky Slides) }
\begin{itemize}
\item[-] Optional Characters: {\tt ?}, {\tt *}, {\tt +}
\end{itemize}
\begin{center}
\begin{tabular}{lll}
\textbf{RE} & \textbf{Match} & \textbf{Example Patterns Matched}\\
{\tt colou?r } & Words with {\tt u} 0 or 1 times& ``\underline{color}" or \\
& & ``\underline{colour} " \\
{\tt oo*h!} & Words with {\tt o} 0 or more times & ``\underline{oh!}" or \\
& & ``\underline{ooh!}" or \\
& & ``\underline{oooh!}" \\
{\tt o+h!} & Words with {\tt o} 1 or more times & ``\underline{oh!}" or \\
& & ``\underline{ooh!}" or \\
& & ``\underline{oooooh!}" or \\
\end{tabular}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Regular Expressions, Some Basics (from Jurafsky Slides) }
\begin{itemize}
\item[-] Wild Cards \alert{{\tt .} }
\end{itemize}
\begin{center}
\begin{tabular}{lll}
\textbf{RE} & \textbf{Match} & \textbf{Example Patterns Matched}\\
{\tt beg\alert{.}n} & Any word with ``beg" then ``n" & ``beg\textcolor{blue}{i}n" or \\
& & ``beg\textcolor{blue}{a}n" or \\
& & ``beg\textcolor{blue}{u}n" or \\
& & ``beg\textcolor{blue}{g}n" (Poor grammar!)
\end{tabular}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Regular Expressions, Some Basics (from Jurafsky Slides) }
\begin{itemize}
\item[-] Start of the line anchor \alert{\^{}}, end of the line anchor \alert{\$}
\end{itemize}
\begin{center}
\begin{tabular}{lll}
\textbf{RE} & \textbf{Match} & \textbf{Example Patterns Matched}\\
{\tt \alert{\^{}}[A-Z] } & Upper case start of line & ``\underline{P}alo Alto" \\
& & ``the town of \textcolor{gray}{P}alo Alto" \\
{\tt \alert{\^{}}[\^{}A-Z] } & Not upper case start of line & ``\underline{t}he town of Palo Alto" \\
& & ``\textcolor{gray}{P}alo Alto" \\
{\tt \alert{\^{}}.} & Start of line & ``\underline{P}alo Alto" \\
& & ``\underline{t}he town of Palo Alto" \\
{\tt .\alert{\$} } & Identify character that ends a line & ``Wait\alert{\underline{!}}" \\
& & ``This is the end\alert{\underline{.}}" \\
\end{tabular}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Regular Expressions, Some Basics (from Jurafsky Slides) }
\begin{itemize}
\item[-] ``Or"$|$ statements, Useful short hand
\end{itemize}
\begin{center}
\begin{tabular}{lll}
\textbf{RE} & \textbf{Match} & \textbf{Example Patterns Matched}\\
yours$|$mine & Matches``yours" or ``mine" & ``it's either \underline{yours} or \underline{mine}"\\
$\backslash$ d & Any digit & ``\underline{1}-Mississippi" \\
$\backslash$ D & Any non-digit & ``1\underline{-Mississippi}" \\
$\backslash$ s & Any whitespace character & ``1,\underline{ }2"\\
$\backslash$ S & Any non-whitespace character & ``\underline{1,} \underline{2}" \\
$\backslash$ w & Any alpha-numeric & ``\underline{1}-\underline{Mississippi} " \\
$\backslash$ W & Any non-alpha numeric & ``1\underline{-}Mississippi" \\
\end{tabular}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Regular Expressions, Some Basics (from Jurafsky Slides) }
Quick Example to Illuminate Differences:
A ``simple" example: identify all instances of \alert{{\tt the}}. \pause
\begin{itemize}
\invisible<1>{\item[-] \alert{{\tt the } }} \pause
\invisible<1-2>{\item[] Misses capitalized examples} \pause
\invisible<1-3>{\item[-] \alert{{\tt [tT]he}}} \pause
\invisible<1-4>{\item[] Returns words that are too long ({\tt theocrat}, {\tt theme} )} \pause
\invisible<1-5>{\item[-] \alert{[\^{}a-zA-Z][tT]he[\^{}a-zA-Z] }} \pause
\invisible<1-6>{\item[] Misses the first ``the" in a sentence } \pause
\invisible<1-7>{\item[-] \alert{(\^{} $|$ [\^{} a-zA-Z])[tT]he[\^{} a-zA-Z] } }
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{An Example: Searching for Tea Party Language}
\only<1-4>{Grimmer, Westwood, and Messing (2014): Criticism and credit \\}
\only<5>{Goodman, Grimmer, Parker, Zlotnik (2015): Criticism}
\begin{center}
\only<1-2>{\invisible<1, 3->{
\scalebox{0.475}{\includegraphics{BigGovernment.pdf}}
}}
\only<3>{
\scalebox{0.475}{\includegraphics{BudgetDeficit.pdf}}
}
\only<4>{
\scalebox{0.475}{\includegraphics{AntiDemRepPlot.pdf}}
}
\only<5>{
\scalebox{0.475}{\includegraphics{TeaPartShiftPress.pdf}}
}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Regular Expressions on Steroids: Cheating Detection Software}
\begin{itemize}
\item[-] WCopyFind:
\begin{footnotesize}
{\tt http://plagiarism.bloomfieldmedia.com/z-wordpress/software/wcopyfind/} \pause \end{footnotesize}
\invisible<1>{\item[-] What constitutes \alert{plagiarism}?} \pause
\invisible<1-2>{\item[-] \alert{Edit distance}: } \pause
\begin{itemize}
\invisible<1-3>{\item[-] Heuristically: how many letters to change from $a$ to $b$ } \pause
\end{itemize}
\invisible<1-4>{\item[-] Sets many parameters: } \pause
\begin{itemize}
\invisible<1-5>{\item[-] Number of differences between pair of ``strings"} \pause
\invisible<1-6>{\item[-] Length of character strings to consider} \pause
\invisible<1-7>{\item[-] Number of matching strings to constitute match} \pause
\end{itemize}
\invisible<1-8>{\item[-] Useful:} \pause
\begin{itemize}
\invisible<1-9>{\item[-] Media uptake} \pause
\invisible<1-10>{\item[-] Joint Press Releases}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Document Term Matrices}
Regular expressions and search are useful \pause \\
\invisible<1>{We want to use statistics/algorithms to characterize text}\pause \\
\invisible<1-2>{\alert{We'll put it in a document-term matrix} }
\end{frame}
\begin{frame}
\frametitle{Document Term Matrices}
Preprocessing$\leadsto$\alert{Simplify} text, make it useful \pause \\
\invisible<1>{\alert{Lower dimensionality} } \pause
\begin{itemize}
\invisible<1-2>{\item[-] \alert{For our purposes} } \pause
\end{itemize}
\invisible<1-3>{Remember: characterize the \alert{Hay stack} } \pause
\begin{itemize}
\invisible<1-4>{\item[-] If you want to analyze a straw of hay, these methods \alert{are unlikely to work}} \pause
\invisible<1-5>{\item[-] But even if you want to closely read texts, characterizing hay stack can be useful }
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Preprocessing for Quantitative Text Analysis}
{\huge \alert{One} (of many) }recipe for preprocessing: retain \alert{useful} information \pause
\begin{itemize}
\invisible<1>{\item[1)] Remove capitalization, punctuation} \pause
\invisible<1-2>{\item[2)] \alert{Discard Word Order} (Bag of Words Assumption) } \pause
\invisible<1-3>{\item[3)] \alert{Discard stop words} } \pause
\invisible<1-4>{\item[4)] \alert{Create Equivalence Class}: Stem, Lemmatize, or synonym} \pause
\invisible<1-5>{\item[5)] \alert{Discard less useful features}$\leadsto$ depends on application} \pause
\invisible<1-6>{\item[6)] Other reduction, specialization} \pause
\end{itemize}
\invisible<1-7>{\alert{Output}: Count vector, each element counts occurrence of stems } \pause \\
\invisible<1-8>{\alert{Provide tools to preprocess via this recipe} }
\end{frame}
\begin{frame}
\frametitle{Preprocessing Texts}
We're going to use the {\tt Natural Language Toolkit} {\tt (nltk)} to work with texts \\
\begin{itemize}
\item[-] Built in functionality
\item[-] Ensures we can customize our feature spaces
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Text Loaded into Python}
{\tt WUSTL\_1.py}
Gettysburg Address
\vspace{0.25in}
\begin{footnotesize}
{\tt
from BeautifulSoup import BeautifulSoup\\
from urllib import urlopen\\
import re, os\\
url = urlopen(`http://avalon.law.yale.edu/19th\_century/gettyb.asp').read()\\
soup = BeautifulSoup(url)\\
text = soup.p.contents$[$0$]$\\
}
\end{footnotesize}
\end{frame}
\begin{frame}
\frametitle{Preprocessing Texts}
Removing capitalization:
\begin{itemize}
\item[-] {\tt Python} : \alert{{\tt string.lower()}}
\item[-] {\tt R} : \alert{{\tt tolower(`string')}}
\end{itemize}
Removing punctuation
\begin{itemize}
\item[-] {\tt Python}: \alert{{\tt re.sub(`$\backslash$W', ` ', string)}}
\item[-] {\tt R} : \alert{{\tt gsub(`$\backslash\backslash$W', ` ', string)}}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Preprocessing Texts}
{\tt
text\_1 = text.lower()
text\_2 = re.sub(`$\backslash$W', ` ', text\_1)
}
\end{frame}
\begin{frame}
\frametitle{The Bag of Words Assumption}
\alert{Assumption: Discard Word Order}
\only<1>{{\tt Now we are engaged in a great civil war, testing whether that nation, or any nation} }
\only<2>{{\tt now we are engaged in a great civil war testing whether that nation or any nation}}
\only<3>{\alert{Unigrams}
\small
\begin{tabular}{ll}