2017-03-29 12 views
1

내가 웹 페이지에서 문자열의 키워드를 계산하기 위해 노력하고있어 : 단어 또는없는 경우카운트 키워드는

#get the URL 
u <- "http://www.dlink.com/it/it" 
doc <- getURL(u) 

#get the text from the body 
html <- htmlTreeParse(doc, useInternal = TRUE) 
txt <- xpathApply(html, "//body//text()[not(ancestor::script)][not(ancestor::style)][not(ancestor::noscript)]", xmlValue) 
txt<-toString(txt) 
txt 

#clean 
str_replace_all(txt, "[\r\n\t,]" , "") 

search <- c("Wi-Fi","Router","Switch","ADSL") 
search 
stri_detect_fixed(txt, search) 

sum(stri_detect_fixed(text, search)) 

불행하게도 계산 만, 대신에, 나는 얼마나 많은 키워드를 계산하려면 (예 : Wi-Fi가 두 번있을 경우 +2), stringi 라이브러리를 사용하는 아이디어는 있습니까?

+2

아마'(TXT, 검색)'를 stri_count_fixed? –

답변

1

사용 stri_count_fixed

library(stringi) 

stri_count_fixed(txt, search) 
[1] 3 2 5 1 

sum(stri_count_fixed(txt, search)) 
[1] 11 
+0

대단히 감사합니다! –