2013-02-22 32 views
6

Tôi muốn tạo một Corpus cho tập hợp các tệp HTML đã tải xuống và sau đó đọc chúng trong R để khai thác văn bản trong tương lai.tạo một Corpus từ nhiều tệp html trong R

Về cơ bản, đây là những gì tôi muốn làm:

  • Tạo một Corpus từ nhiều file html.

Tôi cố gắng để sử dụng DirSource:

library(tm) 
a<- DirSource("C:/test") 
b<-Corpus(DirSource(a), readerControl=list(language="eng", reader=readPlain)) 

nhưng nó sẽ trả về "thông số thư mục không hợp lệ"

  • đọc trong file html từ Corpus tất cả cùng một lúc. Không chắc chắn cách thực hiện.

  • Phân tích cú pháp chúng, chuyển đổi chúng thành văn bản thuần tuý, xóa thẻ. Nhiều người đã đề xuất sử dụng XML, tuy nhiên, tôi không tìm thấy cách xử lý nhiều tệp. Tất cả chỉ dành cho một tệp duy nhất.

Cảm ơn rất nhiều.

+0

Hãy thử sử dụng dấu gạch chéo ngược thay vì dấu gạch chéo chuyển tiếp trong cuộc gọi DirSource của bạn. 'C: \ test' –

+0

Lệnh' Corpus' và 'DirSource' là gì? –

Trả lời

2

Điều này sẽ sửa lỗi.

b<-Corpus(a, ## I change DireSource(a) by a 
      readerControl=list(language="eng", reader=readPlain)) 

Nhưng tôi nghĩ rằng để đọc Html của bạn, bạn cần sử dụng trình đọc xml. Một cái gì đó như:

r <- Corpus(DirSource('c:\test'), 
      readerControl = list(reader = readXML),spec) 

Nhưng bạn cần cung cấp đối số spec, phụ thuộc vào cấu trúc tệp của bạn. xem ví dụ readReut21578XML. Đây là một ví dụ điển hình về trình phân tích cú pháp xml/html.

0

Để đọc tất cả các file html vào một đối tượng R bạn có thể sử dụng

# Set variables 
folder <- 'C:/test' 
extension <- '.htm' 

# Get the names of *.html files in the folder 
files <- list.files(path=folder, pattern=extension) 

# Read all the files into a list 
htmls <- lapply(X=files, 
       FUN=function(file){ 
       .con <- file(description=paste(folder, file, sep='/')) 
       .html <- readLines(.con) 
       close(.con) 
       names(.html) <- file 
       .html 
}) 

Điều đó sẽ cung cấp cho bạn một danh sách, và mỗi phần tử là nội dung HTML của mỗi tập tin.

Tôi sẽ đăng sau khi phân tích cú pháp, tôi đang vội.

11

Điều này sẽ thực hiện. Ở đây tôi có một thư mục trên máy tính của các tệp HTML (một mẫu ngẫu nhiên từ SO) và tôi đã tạo ra một kho dữ liệu, sau đó là một ma trận thuật ngữ tài liệu và sau đó thực hiện một vài nhiệm vụ khai thác văn bản tầm thường.

# get data 
setwd("C:/Downloads/html") # this folder has your HTML files 
html <- list.files(pattern="\\.(htm|html)$") # get just .htm and .html files 

# load packages 
library(tm) 
library(RCurl) 
library(XML) 
# get some code from github to convert HTML to text 
writeChar(con="htmlToText.R", (getURL(ssl.verifypeer = FALSE, "https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/htmlToText/htmlToText.R"))) 
source("htmlToText.R") 
# convert HTML to text 
html2txt <- lapply(html, htmlToText) 
# clean out non-ASCII characters 
html2txtclean <- sapply(html2txt, function(x) iconv(x, "latin1", "ASCII", sub="")) 

# make corpus for text mining 
corpus <- Corpus(VectorSource(html2txtclean)) 

# process text... 
skipWords <- function(x) removeWords(x, stopwords("english")) 
funcs <- list(tolower, removePunctuation, removeNumbers, stripWhitespace, skipWords) 
a <- tm_map(a, PlainTextDocument) 
a <- tm_map(corpus, FUN = tm_reduce, tmFuns = funcs) 
a.dtm1 <- TermDocumentMatrix(a, control = list(wordLengths = c(3,10))) 
newstopwords <- findFreqTerms(a.dtm1, lowfreq=10) # get most frequent words 
# remove most frequent words for this corpus 
a.dtm2 <- a.dtm1[!(a.dtm1$dimnames$Terms) %in% newstopwords,] 
inspect(a.dtm2) 

# carry on with typical things that can now be done, ie. cluster analysis 
a.dtm3 <- removeSparseTerms(a.dtm2, sparse=0.7) 
a.dtm.df <- as.data.frame(inspect(a.dtm3)) 
a.dtm.df.scale <- scale(a.dtm.df) 
d <- dist(a.dtm.df.scale, method = "euclidean") 
fit <- hclust(d, method="ward") 
plot(fit) 

enter image description here

# just for fun... 
library(wordcloud) 
library(RColorBrewer) 

m = as.matrix(t(a.dtm1)) 
# get word counts in decreasing order 
word_freqs = sort(colSums(m), decreasing=TRUE) 
# create a data frame with words and their frequencies 
dm = data.frame(word=names(word_freqs), freq=word_freqs) 
# plot wordcloud 
wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2")) 

enter image description here

+1

Rất đẹp. Chỉ cần thêm tham số 'pattern =". Html "' trong 'list.files (...)' để có thể có các tệp khác bên trong thư mục (* ví dụ:* R script để tải xuống dữ liệu, README và bất kỳ tệp không phải html nào khác, ngoại trừ các tệp khóa học có "html" bên trong tên của chúng. –

+0

Cảm ơn, mẹo tốt. Tôi đã chỉnh sửa cho phù hợp. – Ben

+0

Để có được điều này để làm việc với 0,6 tm, chuyển đổi corpus của bạn để PlainTextDocument nếu không bạn sẽ không thể tạo ra một TDM. Thực hiện <- tm_map (a, PlainTextDocument) – viksit

0

tôi thấy gói boilerpipeR đặc biệt hữu ích để giải nén chỉ có "cốt lõi" nội dung của một trang html.

Các vấn đề liên quan