This repository was archived by the owner on May 24, 2025. It is now read-only.
forked from TaiwanRUserGroup/DSC2014Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
3375 lines (2874 loc) · 90 KB
/
index.html
File metadata and controls
3375 lines (2874 loc) · 90 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
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
<!DOCTYPE html>
<html>
<head>
<title>ETL on R</title>
<meta charset="utf-8">
<meta name="description" content="ETL on R">
<meta name="author" content="Cheng Yu Lin (aha) and Jia Wei Chen (jiawei)">
<meta name="generator" content="slidify" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link rel="stylesheet" href="libraries/frameworks/io2012/css/default.css" media="all" >
<link rel="stylesheet" href="libraries/frameworks/io2012/css/phone.css"
media="only screen and (max-device-width: 480px)" >
<link rel="stylesheet" href="libraries/frameworks/io2012/css/slidify.css" >
<link rel="stylesheet" href="libraries/highlighters/highlight.js/css/tomorrow.css" />
<base target="_blank"> <!-- This amazingness opens all links in a new tab. --> <link rel=stylesheet href="libraries/widgets/quiz/css/demo.css"></link>
<link rel=stylesheet href="libraries/widgets/bootstrap/css/bootstrap.css"></link>
<!-- Grab CDN jQuery, fall back to local if offline -->
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js"></script>
<script>window.jQuery || document.write('<script src="libraries/widgets/quiz/js/jquery.js"><\/script>')</script>
<script data-main="libraries/frameworks/io2012/js/slides"
src="libraries/frameworks/io2012/js/require-1.0.8.min.js">
</script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<!-- LOGO SLIDE -->
<slide class="title-slide segue nobackground">
<hgroup class="auto-fadein">
<h1>ETL on R</h1>
<h2>非結構化資料處理</h2>
<p>Cheng Yu Lin (aha) and Jia Wei Chen (jiawei)<br/></p>
</hgroup>
<article></article>
<footer class = 'license'>
<a href='http://creativecommons.org/licenses/by-sa/3.0/'>
<img width = '80px' src = 'http://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-sa.png'>
</a>
</footer>
</slide>
<!-- SLIDES -->
<slide class="" id="slide-1" style="background:;">
<article data-timings="">
<p><q> 什麼是非結構化資料? </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-2" style="background:;">
<hgroup>
<h2>結構化資料</h2>
</hgroup>
<article data-timings="">
<p><img src = './resources/figures/structure_data.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-3" style="background:;">
<hgroup>
<h2>半結構化資料</h2>
</hgroup>
<article data-timings="">
<p><img src = './resources/figures/semi_structure_data.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-4" style="background:;">
<hgroup>
<h2>非結構化資料</h2>
</hgroup>
<article data-timings="">
<p><img src = './resources/figures/unstructure_data.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-5">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> 非結構化資料可以整理出什麼訊息呢? </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-6" style="background:;">
<article data-timings="">
<p><img src = './resources/figures/keyword_analytics.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-7" style="background:;">
<article data-timings="">
<p><img src = './resources/figures/google_flu.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-8" style="background:;">
<article data-timings="">
<p><q> 我們這場 tutorial 要帶給大家什麼呢 ? </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-9" style="background:;">
<hgroup>
<h2>web scraping</h2>
</hgroup>
<article data-timings="">
<p><img src = './resources/figures/html_pair.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-10">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><img src = './resources/figures/result1.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-11">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><img src = './resources/figures/result2.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-12" style="background:;">
<hgroup>
<h2>CH1 抓取網路資料</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-13" style="background:;">
<hgroup>
<h2>What is HTML?</h2>
</hgroup>
<article data-timings="">
<h3>一種有結構的標記資料</h3>
<p><img src = './resources/figures/html.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-14" style="background:;">
<article data-timings="">
<p><img src = './resources/figures/html_pair.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-15">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> 其實它們是有結構的 </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-16" style="background:;">
<hgroup>
<h2>Document Object Model (DOM)</h2>
</hgroup>
<article data-timings="">
<h3>html 呈現樹狀結構</h3>
<p><img src = './resources/figures/dom.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-17" style="background:;">
<hgroup>
<h2>樹狀結構的內容</h2>
</hgroup>
<article data-timings="">
<ol>
<li>nodename</li>
<li>attribute</li>
<li>text</li>
</ol>
<pre><code class="r"><a href = "www.meetup.com/Taiwan-R">
Taiwan R User Group Website
</a>
</code></pre>
<pre><code class="r">nodename : a
attribute : href with value "www.meetup.com/Taiwan-R"
text: "Taiwan R User Group Website"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-18" style="background:;">
<hgroup>
<h2>xpath 常用到的操作</h2>
</hgroup>
<article data-timings="">
<ul>
<li><p>/ finds the root node</p></li>
<li><p>// selects from anywhere in the tree</p></li>
<li><p>'.' selects current node</p></li>
<li><p>'..' selects parent of current node</p></li>
<li><p>@ selects attributes</p></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-19" style="background:;">
<hgroup>
<h2>如果想抓 a 的話</h2>
</hgroup>
<article data-timings="">
<h3>we can use:</h3>
<pre><code class="r">"/../body/div/a" or "//a"
</code></pre>
<p><img src = './resources/figures/dom.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-20" style="background:;">
<hgroup>
<h2>如果指定 a 的 attribute 或 text</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">If we had '<a href = "www.meetup.com/Taiwan-R">Taiwan R User Group Website</a>'
</code></pre>
<p>抓出 href = ’www.meetup.com/Taiwan-R’ 的 a</p>
<pre><code class="r">"//a[@href = ’www.meetup.com/Taiwan-R’]"
</code></pre>
<p>抓出 text = ’Taiwan R User Group Website’ 的 a</p>
<pre><code class="r">"//a[text() = ’Taiwan R User Group Website’]"
</code></pre>
<pre><code class="r">nodename : a
attribute : href with value "www.meetup.com/Taiwan-R"
text: "Taiwan R User Group Website"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-21" style="background:;">
<hgroup>
<h2>匯入套件</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">require(DSC2014Tutorial)
require(XML)
require(stringr)
require(xts)
require(reshape2)
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-22" style="background:;">
<hgroup>
<h2>開啟下載好的 html 檔案</h2>
</hgroup>
<article data-timings="">
<h3><a href="https://tw.news.yahoo.com/real-estate/archive/1.html">資料來源 - yahoo 新聞</a></h3>
<pre><code class="r"># 讀取 ETL 相關檔案所在的路徑
path <- ETL_file('')
docs <- grep("*.html",dir(path), value = TRUE)
</code></pre>
<pre><code class="r">pages <- sapply(docs, function(d){
path <- ETL_file(d)
f <- file(path, encoding = 'UTF-8')
f_size <- file.info(ETL_file(d))$size
content <- readChar(f, f_size)
close(f)
return(content)
})
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-23" style="background:;">
<hgroup>
<h2>抓出 title和 連結</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">library(XML)
# 建立 DOM Tree
doc <- htmlTreeParse(pages[1], useInternalNodes = TRUE)
# 抓出 node a 的 attr
attr <- xpathApply(doc, "//ul/li/div/div/h4/a", xmlAttrs)
# 抓出 node a 的 text
text <- xpathSApply(doc, "//ul/li/div/div/h4/a", xmlValue)
</code></pre>
<h3>text 結果</h3>
<pre><code>## [1] "稅制改革座談 財長明督軍"
## [2] "缺地… 租稅引導產業南移"
## [3] "房地合一 建商:重稅擾民"
## [4] "租不起 「天天見麵」悄收台北店"
## [5] "《世紀專欄》地價稅納稅義務基準日為每年8月31日"
## [6] "苗房屋標準單價公告調整 7月起實施"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-24" style="background:;">
<article data-timings="">
<p><img src = './resources/figures/xpath_title.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-25" style="background:;">
<hgroup>
<h2>一次抓多篇</h2>
</hgroup>
<article data-timings="">
<pre><code class="r"># lapply : 最後將回傳 list 型態的結果
title <- lapply(pages, function(p){
doc <- htmlTreeParse(p, useInternalNodes = TRUE)
xpathSApply(doc, "//ul/li/div/div/h4/a", xmlValue)
})
</code></pre>
<pre><code>## [[1]]
## [1] "稅制改革座談 財長明督軍" "缺地… 租稅引導產業南移"
##
## [[2]]
## [1] "抗議一地兩價 遷建戶下跪向費鴻泰陳情"
## [2] "房產觀望氣氛 中市推案減兩成"
##
## [[3]]
## [1] "綠大地五股市區 千坪綠地" "在地專家"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-26" style="background:;">
<hgroup>
<h2>練習題1</h2>
</hgroup>
<article data-timings="">
<h3>請填入適當的 xpath 抓出新聞來源, 日期, 時間</h3>
<pre><code class="r">src_list <- xpathSApply(doc, ___ , xmlValue)
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-27" style="background:;">
<article data-timings="">
<p><img src = './resources/figures/xpath_time.png' ></img></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-28" style="background:;">
<hgroup>
<h2>解答1</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">src_list <- xpathSApply(doc, "//cite", xmlValue)
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-29" style="background:;">
<hgroup>
<h2>(自行操作) 保存/讀取 抓下來的數據</h2>
</hgroup>
<article data-timings="">
<h3>先將結果合併為表格</h3>
<pre><code class="r">doc <- do.call(rbind, title)
</code></pre>
<h3>寫入 / 讀回 數據</h3>
<pre><code class="r">writeLines(doc, "news_yahoo.txt")
news_yahoo <- readLines('news_yahoo.txt')
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-30" style="background:;">
<hgroup>
<h2>匯入這次用的資料</h2>
</hgroup>
<article data-timings="">
<h3><a href="http://house.cnyes.com/News/tw_housenews/List.htm">資料來源 - 鉅亨網</a></h3>
<pre><code class="r">path <- ETL_file('news.txt')
f <- file(path, encoding="UTF-8")
house_news <- readLines(f)
head(house_news)
</code></pre>
<pre><code>## [1] "2014-06-24富邦人壽信義區再插旗 A25案172億元奪標"
## [2] "2014-06-24北市A25地上權案 富邦權利金172.88億元得標"
## [3] "2014-06-23房市管制 央行將化明為暗"
## [4] "2014-06-20史上頭一遭!大直新地王 1坪212萬#超越信義之星直逼帝寶 中山新天價"
## [5] "2014-06-20大直帝景水花園實價首破200萬 北市3大豪宅板塊確立"
## [6] "2014-06-19樺福千金捷運宅標脫率89.4% 均價63萬元屬合理範圍"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-31" style="background:;">
<hgroup>
<h2>CH2 字串處理</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-32" style="background:;">
<hgroup>
<h2>正規表達 介紹</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">\\d -> 數字
\\w -> 單字
. -> 任意字元(數字, 單字, 標點符號 ..)
? -> 出現 0或1 次
+ -> 出現 1次 以上
* -> 出現 0次 以上
^ -> 出現在行首
$ -> 出現在行尾
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-33" style="background:;">
<article data-timings="">
<pre><code class="r">x <- c("apples*14", "flour", "sugar*100", "milk*3")
</code></pre>
<h3>批配2個數字</h3>
<pre><code class="r">str_extract(x, "\\d{2}")
</code></pre>
<pre><code>## [1] "14" NA "10" NA
</code></pre>
<h3>批配2~3個數字</h3>
<pre><code class="r">str_extract(x, "\\d{2,3}")
</code></pre>
<pre><code>## [1] "14" NA "100" NA
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-34" style="background:;">
<article data-timings="">
<pre><code class="r">x <- c("apples*14", "flour", "sugar*100", "milk*3")
</code></pre>
<h3>批配出現一次以上的a或p</h3>
<pre><code class="r">str_extract(x, "[ap]+")
</code></pre>
<pre><code>## [1] "app" NA "a" NA
</code></pre>
<h3>也可以搭配{m,n}</h3>
<pre><code class="r">str_extract(x, "[ap]{1,4}")
</code></pre>
<pre><code>## [1] "app" NA "a" NA
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-35" style="background:;">
<hgroup>
<h2>練習題2</h2>
</hgroup>
<article data-timings="">
<h3>猜猜看這會批配出什麼東西?</h3>
<pre><code class="r">x <- c("apples*14", "flour", "allen", "aside")
str_extract(x, "a[elpn]+.?")
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-36" style="background:;">
<hgroup>
<h2>解答2</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">x <- c("apples*14", "flour", "allen", "aside")
str_extract(x, "a[elpn]+.?")
</code></pre>
<pre><code>## [1] "apples" NA "allen" NA
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-37" style="background:;">
<hgroup>
<h2>進階一點的正規表達</h2>
</hgroup>
<article data-timings="">
<h3>抽取 log訊息時很有用的 () 批配</h3>
<h3>可以把 match 的字儲存倒 \{n}</h3>
<pre><code class="r">x <-"2014-06-24富邦人壽信義區再插旗 A25案172億元奪標"
patten <- "(\\d{4}-\\d{2}-\\d{2}).+(信義.?).+"
gsub(patten, "\\1, \\2", x)
</code></pre>
<pre><code>## [1] "2014-06-24, 信義區"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-38" style="background:;">
<hgroup>
<h2>練習題3</h2>
</hgroup>
<article data-timings="">
<p>請利用下面提示,填入適當的 Patten,產生出下面結果</p>
<pre><code class="r"># 請利用 \\d{?}-\\d{?}-\\d{?} 抽取出時間
dates <- str_extract(house_news, ___)
# 請利用 \\d{?}-\\d{?}-\\d{?} 把時間取代為 ''
titles <- str_replace(house_news, ___, ___)
house_news <- cbind(dates, titles)
</code></pre>
<pre><code>## dates titles
## [1,] "2014-06-24" "富邦人壽信義區再插旗 A25案172億元奪標"
## [2,] "2014-06-24" "北市A25地上權案 富邦權利金172.88億元得標"
## [3,] "2014-06-23" "房市管制 央行將化明為暗"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-39" style="background:;">
<hgroup>
<h2>解答3</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">dates <- str_extract(house_news, "\\d{4}-\\d{2}-\\d{2}")
titles <- str_replace(house_news, "\\d{4}-\\d{2}-\\d{2}", "")
house_news <- cbind(dates, titles)
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-40" style="background:;">
<hgroup>
<h2>清除符號的方法介紹</h2>
</hgroup>
<article data-timings="">
<h3>[[:punct:]] 表示標點符號</h3>
<h3>[[:blank:]] 表示分隔符號</h3>
<pre><code class="r">str_replace_all("i l<ik>e ?R pr:ogr,a%mi!ng", "[[:punct:]]", "")
</code></pre>
<pre><code>## [1] "i like R programing"
</code></pre>
<pre><code class="r">str_replace_all("a \t p \t p ", "[[:blank:]]", "")
</code></pre>
<pre><code>## [1] "app"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-41" style="background:;">
<hgroup>
<h2>如果想清除中文特殊符號 ...</h2>
</hgroup>
<article data-timings="">
<h3>這是 utf-8 編碼</h3>
<pre><code class="r">punctuation <- "\u3002 \uff1b \uff0c \uff1a \u201c \u201d \uff08 \uff09 \u3001 \uff1f \u300a \u300b"
punctuation
</code></pre>
<pre><code>## [1] "。 ; , : “ ” ( ) 、 ? 《 》"
</code></pre>
<pre><code class="r">str_replace_all("。 ; , : “ ” ( ) 、 ? 《 》", punctuation, "")
</code></pre>
<pre><code>## [1] ""
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-42">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> 再來要介紹一個非常重要的 function </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-43" style="background:;">
<hgroup>
<h2>substr 用法</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">substr('富邦人壽信義區再插旗 A25案172億元奪標', 1, 4)
</code></pre>
<pre><code>## [1] "富邦人壽"
</code></pre>
<pre><code class="r">substr('富邦人壽信義區再插旗 A25案172億元奪標', 5, 7)
</code></pre>
<pre><code>## [1] "信義區"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-44">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q>看來很簡單,那為什麼說它很重要呢?</q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-45">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> 做個練習可能就有感覺了 </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-46" style="background:;">
<hgroup>
<h2>練習題4</h2>
</hgroup>
<article data-timings="">
<h3>空格填 i or i+1,才呈現出下面使用範例?</h3>
<pre><code class="r">ngram <- function(sentence, n){
chunk <- c()
for(i in 1 : (nchar(sentence)-n+1)){
chunk <- append(chunk, substr(sentence, ___ , i+n-1))
}
return(chunk)
}
</code></pre>
<h3>使用範例</h3>
<pre><code class="r">ngram('富邦人壽信義區再插旗', 2)
</code></pre>
<pre><code>## [1] "富邦" "邦人" "人壽" "壽信" "信義" "義區" "區再" "再插" "插旗"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-47" style="background:;">
<hgroup>
<h2>解答4</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ngram <- function(sentence, n){
chunk <- c()
for(i in 1 : (nchar(sentence)-n+1)){
chunk <- append(chunk, substr(sentence, i, i+n-1))
}
return(chunk)
}
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-48" style="background:;">
<article data-timings="">
<pre><code class="r">sapply(titles, ngram, 2, USE.NAMES = FALSE)[1:3]
</code></pre>
<pre><code>## [[1]]
## [1] "富邦" "邦人" "人壽" "壽信" "信義" "義區" "區再" "再插" "插旗" "旗 "
## [11] " A" "A2" "25" "5案" "案1" "17" "72" "2億" "億元" "元奪"
## [21] "奪標"
##
## [[2]]
## [1] "北市" "市A" "A2" "25" "5地" "地上" "上權" "權案" "案 " " 富"
## [11] "富邦" "邦權" "權利" "利金" "金1" "17" "72" "2." ".8" "88"
## [21] "8億" "億元" "元得" "得標"
##
## [[3]]
## [1] "房市" "市管" "管制" "制 " " 央" "央行" "行將" "將化" "化明" "明為"
## [11] "為暗"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-49" style="background:;">
<article data-timings="">
<pre><code class="r">sapply(titles, ngram, 3, USE.NAMES = FALSE)[1:3]
</code></pre>
<pre><code>## [[1]]
## [1] "富邦人" "邦人壽" "人壽信" "壽信義" "信義區" "義區再" "區再插"
## [8] "再插旗" "插旗 " "旗 A" " A2" "A25" "25案" "5案1"
## [15] "案17" "172" "72億" "2億元" "億元奪" "元奪標"
##
## [[2]]
## [1] "北市A" "市A2" "A25" "25地" "5地上" "地上權" "上權案"
## [8] "權案 " "案 富" " 富邦" "富邦權" "邦權利" "權利金" "利金1"
## [15] "金17" "172" "72." "2.8" ".88" "88億" "8億元"
## [22] "億元得" "元得標"
##
## [[3]]
## [1] "房市管" "市管制" "管制 " "制 央" " 央行" "央行將" "行將化"
## [8] "將化明" "化明為" "明為暗"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-50">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> 透過substr才能從文章裡抽出詞彙呀! </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-51" style="background:;">
<hgroup>
<h2>把字段長度1~6都列舉出來,並分別儲存</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">titles <- gsub("\\s", "", titles)
piece <- list()
piece[['1']] <- unlist(sapply(titles, ngram, 1, USE.NAMES = FALSE))
piece[['2']] <- unlist(sapply(titles, ngram, 2, USE.NAMES = FALSE))
piece[['3']] <- unlist(sapply(titles, ngram, 3, USE.NAMES = FALSE))
piece[['4']] <- unlist(sapply(titles, ngram, 4, USE.NAMES = FALSE))
piece[['5']] <- unlist(sapply(titles, ngram, 5, USE.NAMES = FALSE))
piece[['6']] <- unlist(sapply(titles, ngram, 6, USE.NAMES = FALSE))
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-52">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> 但是有個問題 ! </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-53" style="background:;">
<article data-timings="">
<pre><code class="r">sapply(titles, ngram, 3, USE.NAMES = FALSE)[1:3]
</code></pre>
<pre><code>## [[1]]
## [1] "富邦人" "邦人壽" "人壽信" "壽信義" "信義區" "義區再" "區再插"
## [8] "再插旗" "插旗A" "旗A2" "A25" "25案" "5案1" "案17"
## [15] "172" "72億" "2億元" "億元奪" "元奪標"
##
## [[2]]
## [1] "北市A" "市A2" "A25" "25地" "5地上" "地上權" "上權案"
## [8] "權案富" "案富邦" "富邦權" "邦權利" "權利金" "利金1" "金17"
## [15] "172" "72." "2.8" ".88" "88億" "8億元" "億元得"
## [22] "元得標"
##
## [[3]]
## [1] "房市管" "市管制" "管制央" "制央行" "央行將" "行將化" "將化明" "化明為"
## [9] "明為暗"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-54">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q>注意到了嗎?</q>
<q>裡面有很多沒意義的字串</q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-55">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q>如何解決呢?</q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-56">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> '房價偏' 是不是一個有意義的詞彙呢? </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-57">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> '房價' + '偏' 是否機率獨立?</q></p>
<p><q> '房' + '價偏' 是否機率獨立?</q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-58">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> 服貿協議 </q></p>
<p><q> '服貿協' 右邊幾乎只能接 '議'</q></p>
<p><q> '貿協議' 左邊幾乎只能接 '服'</q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-59">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> 利用這兩種性質來篩選掉沒意義的字串 </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-60">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>
<p><q> 但是程式該怎麼寫呢? </q></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-61">
<hgroup>
</hgroup>
<article class = 'flexbox vcenter'>