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.Rmd
More file actions
1556 lines (1082 loc) · 26 KB
/
index.Rmd
File metadata and controls
1556 lines (1082 loc) · 26 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
---
title : ETL on R
subtitle : 非結構化資料處理
author : Cheng Yu Lin (aha) and Jia Wei Chen (jiawei)
job :
license : by-sa
framework : io2012 # {io2012, html5slides, shower, dzslides, ...}
highlighter : highlight.js # {highlight.js, prettify, highlight}
hitheme : tomorrow #
ext_widgets : {rCharts: libraries/nvd3}
widgets : [mathjax, quiz, bootstrap] # {mathjax, quiz, bootstrap}
mode : selfcontained # {standalone, draft}
---
<q> 什麼是非結構化資料? </q>
---
## 結構化資料
<img src = './resources/figures/structure_data.png' ></img>
---
## 半結構化資料
<img src = './resources/figures/semi_structure_data.png' ></img>
---
## 非結構化資料
<img src = './resources/figures/unstructure_data.png' ></img>
--- &vcenter
<q> 非結構化資料可以整理出什麼訊息呢? </q>
---
<img src = './resources/figures/keyword_analytics.png' ></img>
---
<img src = './resources/figures/google_flu.png' ></img>
---
<q> 我們這場 tutorial 要帶給大家什麼呢 ? </q>
---
## web scraping
<img src = './resources/figures/html_pair.png' ></img>
--- &vcenter
<img src = './resources/figures/result1.png' ></img>
--- &vcenter
<img src = './resources/figures/result2.png' ></img>
---
## CH1 抓取網路資料
---
## What is HTML?
### 一種有結構的標記資料
<img src = './resources/figures/html.png' ></img>
---
<img src = './resources/figures/html_pair.png' ></img>
--- &vcenter
<q> 其實它們是有結構的 </q>
---
## Document Object Model (DOM)
### html 呈現樹狀結構
<img src = './resources/figures/dom.png' ></img>
---
## 樹狀結構的內容
1. nodename
2. attribute
3. text
```r
<a href = "www.meetup.com/Taiwan-R">
Taiwan R User Group Website
</a>
```
```r
nodename : a
attribute : href with value "www.meetup.com/Taiwan-R"
text: "Taiwan R User Group Website"
```
---
## xpath 常用到的操作
- / finds the root node
- // selects from anywhere in the tree
- '.' selects current node
- '..' selects parent of current node
- @ selects attributes
---
## 如果想抓 a 的話
### we can use:
```r
"/../body/div/a" or "//a"
```
<img src = './resources/figures/dom.png' ></img>
---
## 如果指定 a 的 attribute 或 text
```r
If we had '<a href = "www.meetup.com/Taiwan-R">Taiwan R User Group Website</a>'
```
抓出 href = ’www.meetup.com/Taiwan-R’ 的 a
```r
"//a[@href = ’www.meetup.com/Taiwan-R’]"
```
抓出 text = ’Taiwan R User Group Website’ 的 a
```r
"//a[text() = ’Taiwan R User Group Website’]"
```
```r
nodename : a
attribute : href with value "www.meetup.com/Taiwan-R"
text: "Taiwan R User Group Website"
```
---
## 匯入套件
```{r message=FALSE}
require(DSC2014Tutorial)
require(XML)
require(stringr)
require(xts)
require(reshape2)
```
---
## 開啟下載好的 html 檔案
### [資料來源 - yahoo 新聞](https://tw.news.yahoo.com/real-estate/archive/1.html)
```{r}
# 讀取 ETL 相關檔案所在的路徑
path <- ETL_file('')
docs <- grep("*.html",dir(path), value = TRUE)
```
```{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)
})
```
---
## 抓出 title和 連結
```{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)
```
### text 結果
```{r echo=FALSE}
text[1:6]
```
---
<img src = './resources/figures/xpath_title.png' ></img>
---
## 一次抓多篇
```{r}
# lapply : 最後將回傳 list 型態的結果
title <- lapply(pages, function(p){
doc <- htmlTreeParse(p, useInternalNodes = TRUE)
xpathSApply(doc, "//ul/li/div/div/h4/a", xmlValue)
})
```
```{r echo=FALSE}
lapply(1:3, function(i){
title[[i]][1:2]
})
```
---
## 練習題1
### 請填入適當的 xpath 抓出新聞來源, 日期, 時間
```r
src_list <- xpathSApply(doc, ___ , xmlValue)
```
---
<img src = './resources/figures/xpath_time.png' ></img>
---
## 解答1
```r
src_list <- xpathSApply(doc, "//cite", xmlValue)
```
---
## (自行操作) 保存/讀取 抓下來的數據
### 先將結果合併為表格
```r
doc <- do.call(rbind, title)
```
### 寫入 / 讀回 數據
```r
writeLines(doc, "news_yahoo.txt")
news_yahoo <- readLines('news_yahoo.txt')
```
---
## 匯入這次用的資料
### [資料來源 - 鉅亨網](http://house.cnyes.com/News/tw_housenews/List.htm)
```{r message=FALSE, warning=FALSE}
path <- ETL_file('news.txt')
f <- file(path, encoding="UTF-8")
house_news <- readLines(f)
head(house_news)
```
---
## CH2 字串處理
---
## 正規表達 介紹
```r
\\d -> 數字
\\w -> 單字
. -> 任意字元(數字, 單字, 標點符號 ..)
? -> 出現 0或1 次
+ -> 出現 1次 以上
* -> 出現 0次 以上
^ -> 出現在行首
$ -> 出現在行尾
```
---
```{r}
x <- c("apples*14", "flour", "sugar*100", "milk*3")
```
### 批配2個數字
```{r}
str_extract(x, "\\d{2}")
```
### 批配2~3個數字
```{r}
str_extract(x, "\\d{2,3}")
```
---
```r
x <- c("apples*14", "flour", "sugar*100", "milk*3")
```
### 批配出現一次以上的a或p
```{r}
str_extract(x, "[ap]+")
```
### 也可以搭配{m,n}
```{r}
str_extract(x, "[ap]{1,4}")
```
---
## 練習題2
### 猜猜看這會批配出什麼東西?
```r
x <- c("apples*14", "flour", "allen", "aside")
str_extract(x, "a[elpn]+.?")
```
---
## 解答2
```{r}
x <- c("apples*14", "flour", "allen", "aside")
str_extract(x, "a[elpn]+.?")
```
---
## 進階一點的正規表達
### 抽取 log訊息時很有用的 () 批配
### 可以把 match 的字儲存倒 \\{n}
```{r}
x <-"2014-06-24富邦人壽信義區再插旗 A25案172億元奪標"
patten <- "(\\d{4}-\\d{2}-\\d{2}).+(信義.?).+"
gsub(patten, "\\1, \\2", x)
```
---
## 練習題3
請利用下面提示,填入適當的 Patten,產生出下面結果
```r
# 請利用 \\d{?}-\\d{?}-\\d{?} 抽取出時間
dates <- str_extract(house_news, ___)
# 請利用 \\d{?}-\\d{?}-\\d{?} 把時間取代為 ''
titles <- str_replace(house_news, ___, ___)
house_news <- cbind(dates, titles)
```
```{r echo=FALSE}
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)
```
```{r echo=FALSE}
head(house_news,3)
```
---
## 解答3
```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)
```
---
## 清除符號的方法介紹
### [[:punct:]] 表示標點符號
### [[:blank:]] 表示分隔符號
```{r}
str_replace_all("i l<ik>e ?R pr:ogr,a%mi!ng", "[[:punct:]]", "")
str_replace_all("a \t p \t p ", "[[:blank:]]", "")
```
---
## 如果想清除中文特殊符號 ...
### 這是 utf-8 編碼
```{r}
punctuation <- "\u3002 \uff1b \uff0c \uff1a \u201c \u201d \uff08 \uff09 \u3001 \uff1f \u300a \u300b"
punctuation
```
```{r}
str_replace_all("。 ; , : “ ” ( ) 、 ? 《 》", punctuation, "")
```
--- &vcenter
<q> 再來要介紹一個非常重要的 function </q>
---
## substr 用法
```{r}
substr('富邦人壽信義區再插旗 A25案172億元奪標', 1, 4)
substr('富邦人壽信義區再插旗 A25案172億元奪標', 5, 7)
```
--- &vcenter
<q>看來很簡單,那為什麼說它很重要呢?</q>
--- &vcenter
<q> 做個練習可能就有感覺了 </q>
---
## 練習題4
### 空格填 i or i+1,才呈現出下面使用範例?
```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)
}
```
```{r echo=FALSE}
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)
}
```
### 使用範例
```{r}
ngram('富邦人壽信義區再插旗', 2)
```
---
## 解答4
```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)
}
```
---
```{r}
sapply(titles, ngram, 2, USE.NAMES = FALSE)[1:3]
```
---
```{r}
sapply(titles, ngram, 3, USE.NAMES = FALSE)[1:3]
```
--- &vcenter
<q> 透過substr才能從文章裡抽出詞彙呀! </q>
---
## 把字段長度1~6都列舉出來,並分別儲存
```{r message=FALSE, warning=FALSE}
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))
```
--- &vcenter
<q> 但是有個問題 ! </q>
---
```{r}
sapply(titles, ngram, 3, USE.NAMES = FALSE)[1:3]
```
--- &vcenter
<q>注意到了嗎?</q>
<q>裡面有很多沒意義的字串</q>
--- &vcenter
<q>如何解決呢?</q>
--- &vcenter
<q> '房價偏' 是不是一個有意義的詞彙呢? </q>
--- &vcenter
<q> '房價' + '偏' 是否機率獨立?</q>
<q> '房' + '價偏' 是否機率獨立?</q>
--- &vcenter
<q> 服貿協議 </q>
<q> '服貿協' 右邊幾乎只能接 '議'</q>
<q> '貿協議' 左邊幾乎只能接 '服'</q>
--- &vcenter
<q> 利用這兩種性質來篩選掉沒意義的字串 </q>
--- &vcenter
<q> 但是程式該怎麼寫呢? </q>
--- &vcenter
<q> 繼續用 substr ! </q>
<q> 然後加上一點簡單的正規表達 </q>
---
## 練習題5
### 空格填 i or i+1,才能呈現出下一頁的使用範例?
```r
segmentWord <- function(word){
# nchar 算字串長度
n <- nchar(word)-1
seg <- lapply(1: n, function(i){
w1 <- substr(word, 1, i)
w2 <- substr(word, ___ , n+1)
c(w1,w2)
})
return(seg)
}
```
---
```{r echo=FALSE}
segmentWord <- function(word){
n <- nchar(word)-1
seg <- lapply(1: n, function(i){
w1 <- substr(word, 1, i)
w2 <- substr(word,i+1, n+1)
c(w1,w2)
})
return(seg)
}
```
```{r}
segmentWord('富邦銀')
segmentWord('大同區全')
```
---
## 解答5
```r
segmentWord <- function(word){
n <- nchar(word)-1
seg <- lapply(1: n, function(i){
w1 <- substr(word, 1, i)
w2 <- substr(word,i+1, n+1)
c(w1,w2)
})
return(seg)
}
```
--- &vcenter
<q>已經會把字串兩個兩個拆開</q>
<q>但是機率從哪來 ? </q>
---
## 操作
### 算 字段 出現次數
```{r}
words_freq <- table(unlist(piece))
```
```{r echo=FALSE}
words_freq[c('信義區', '北市', '房價', '房市', '上漲')]
```
---
## 操作
### 算出機率
```{r}
N <- sum(words_freq[piece[['1']]])
words_prob <- words_freq / N
```
```{r echo=FALSE}
tail(words_prob, 5)
```
--- &vcenter
<q> 以下這種性質要怎麼寫出程式呢? </q>
<q> '服貿協' 右邊幾乎只能接 '議'</q>
<q> '貿協議' 左邊幾乎只能接 '服'</q>
---
## paste, grep 用法
```{r}
paste("富邦" ,"人壽", sep='')
grep('富邦' , c('富邦金', '法人富邦', '台北富邦銀行'))
grep('^富邦' , c('富邦金', '法人富邦', '台北富邦銀行'))
grep('富邦$' , c('富邦金', '法人富邦', '台北富邦銀行'))
```
---
## 練習題6
### 請找出 '義區' 開頭/結尾 的字串
### 可以參考上一頁
```r
word <- '義區'
# 找出比 '義區' 字串長度 多1 的所有字串
BASE <- piece[[as.character(nchar(word)+1)]]
# 找出 '義區' 開頭的字串
PATTEN1 <- paste( __ , '義區', sep = '')
matchs1 <- grep(PATTEN1 , BASE, value = TRUE)
# 找出 '義區' 結尾的字串
PATTEN2 <- paste('義區' , __ , sep = '')
matchs2 <- grep(PATTEN2 , BASE, value = TRUE)
```
---
## 解答6
```{r}
word <- '義區'
# 找出比 '義區' 字串長度 多1 的所有字串
BASE <- piece[[as.character(nchar(word)+1)]]
# 找出 '義區' 開頭的字串
PATTEN1 <- paste('^', '義區', sep = '')
matchs1 <- grep(PATTEN1 ,BASE , value = TRUE)
# 找出 '義區' 結尾的字串
PATTEN2 <- paste('義區' ,'$', sep = '' )
matchs2 <- grep(PATTEN2 ,BASE , value = TRUE)
```
```{r echo=FALSE}
matchs1
matchs2
```
---
## 挑出單字長度 2~5 的 候選詞彙
```{r}
words_2_5 <- unique(unlist(piece[2:5]))
```
```{r echo=FALSE}
words_2_5[sample(length(words_2_5),15)]
```
--- &vcenter
## 長度2~5的 單字 出現次數分佈
```{r echo=FALSE, message=FALSE}
plot(words_freq[words_2_5], xlab = 'index', ylab = 'count')
```
---
## 找出 words_2_5 出現次數>2 的詞彙
```{r}
words <- names(which(words_freq[words_2_5] > 2))
```
```{r echo=FALSE}
tail(words_freq[words_2_5][words_freq[words_2_5] > 2], 15)
```
---
## CH3 進行聚合
---
## 前面 segmentWord 練習題的使用
### ex. 算'子字串'之間是否獨立
```{r}
cohesion <- function(word){
# 先把 word切成兩個子字串
seg <- segmentWord(word)
val <- sapply(seg, function(x){
f_word <- words_freq[word]
# 兩塊子字串的出現次數
f_x1 <- words_freq[x[1]]
f_x2 <- words_freq[x[2]]
# 兩塊子字串是否獨立
mi <- log2(N) + log2(f_word) - log2(f_x1) - log2(f_x2)
return(mi)
})
return (min(val))
}
```
---
## 進行計算
```{r warning=FALSE}
cohesion_val <- sapply(words, cohesion, USE.NAMES = FALSE)
names(cohesion_val) <- words
# 挑大於10的原因請看一下一頁
coh_words <- names(which(cohesion_val > 10))
```
```{r echo=FALSE}
tail(sort(cohesion_val), 10)
```
---
```{r echo=FALSE}
plot(cohesion_val)
```
---
### 計算單字左右兩邊可以串接其他單字的程度
```{r}
disorder <- function(word){
# 其實只是練習6的內容,加上算亂度
BASE <- piece[[as.character(nchar(word)+1)]]
PATTEN1 <- paste("^", word, sep = '')
matchs1 <- grep(PATTEN1, BASE, value = TRUE)
freq1 <- table(matchs1)
pre <- mean(-log2(freq1/sum(freq1)))
PATTEN2 <- paste(word, "$", sep = '')
matchs2 <- grep(PATTEN2, BASE, value = TRUE)
freq2 <- table(matchs2)
post <- mean(-log2(freq2/sum(freq2)))
index <- is.na(c(pre, post))
condition <- any(index)
return(ifelse(condition, c(pre, post)[!index], min(pre, post)))
}
```
---
## 進行計算
```{r}
disorder_val <- sapply(coh_words, disorder, USE.NAMES = FALSE)
names(disorder_val) <- coh_words
```
```{r echo=FALSE}
tail(sort(disorder_val), 15)
```
--- &vcenter
## 單字兩側的混亂程度分佈
```{r echo=FALSE}
plot(disorder_val, xlab='index', ylab = 'value')
```
---
## 挑出混亂程度 >1 的字串
```{r}
dis_words <- names(which(disorder_val > 1))
```
```{r echo=FALSE}
tail(dis_words, 20)
```
---
## 混亂程度的例子
### 兩側混亂程度高的字
```{r echo=FALSE}
tail(names(sort(disorder_val)), 15)
```
### 兩側混亂程度低的字
```{r echo=FALSE}
head(names(sort(disorder_val)), 15)
```
--- &vcenter
<q> 到這邊真是辛苦大家了 ... </q>
<q> 我們總算挑出有意義的字串了 歐耶! </q>
--- &vcenter
<q> 接下來我們算出關鍵字的出現次數 </q>
<q> 然後把各個出現次數合併起來就好了 </q>
--- &vcenter
<q>這步驟 用 R 做起來真的是很輕鬆愉快 </q>
<q> 讓我們介紹一個很強大的 function </q>
---
## str_count 用法
### 算 a, e 出現次數
```{r}
fruit <- c("apple", "banana", "pear", "pineapple")
lapply(c("a", "e"), function(x){
str_count(fruit, x)
})
```
---
## 練習題7
### dis_words 裡面每個單字在新聞 title 的出現次數是多少?
### 請填入 dis_words 和 title
```r
tmp <- lapply( __ , function(word){
# 算出 word 在title的出現次數
str_count( __ , word)
})
# 把算出來結果,用 column combind 成一張表格
words_tbl <- do.call(cbind, tmp)
colnames(words_tbl) <- dis_words
```
```{r echo=FALSE}
tmp <- lapply(dis_words, function(word){
str_count(titles, word)
})
words_tbl <- do.call(cbind, tmp)
colnames(words_tbl) <- dis_words
```
```{r echo=FALSE}
words_tbl[1:4, sample(ncol(words_tbl), 8)]
```
---
## 解答7
```r
# 把 dis_words裡面的字一個一個送進去做運算
tmp <- lapply(dis_words, function(word){
# 算出 word 在title的出現次數
str_count(titles, word)
})
# 把算出來結果,用 column combind 成一張表格
words_tbl <- do.call(cbind, tmp)
colnames(words_tbl) <- dis_words
words_tbl[1:6, sample(ncol(words_tbl), 8)]
```
--- &vcenter
<q> 我們今天的課程就到這邊 </q>
<q> 希望大家已經學到以下幾件事情 </q>
--- &vcenter
<q> xpath + web scraping 的概念 </q>
<q> 利用正規表達來抽出需要的資訊 </q>
<q> 瞭解兩種方法來取出有意義的字串 </q>
<q> 計算字串的出現次數,與整理成出現次數表格 </q>
--- &vcenter
<q> 後面的投影片可以回家自行服用</q>
<q> 不用客氣 </q>