簡體   English   中英

從R中的許多html文件創建一個語料庫

[英]create a Corpus from many html files in R

我想創建一個語料庫來收集下載的HTML文件,然后在R中讀取它們以供將來的文本挖掘。

從本質上講,這就是我想要做的:

  • 從多個html文件創建語料庫。

我嘗試使用DirSource:

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

但它返回“無效的目錄參數”

  • 一次讀入來自語料庫的html文件。 不知道怎么做。

  • 解析它們,將它們轉換為純文本,刪除標簽。 很多人建議使用XML,但是,我找不到處理多個文件的方法。 它們都是一個文件。

非常感謝。

這應該做到這一點。 在這里,我的計算機上有一個HTML文件的文件夾(來自SO的隨機樣本),我從中創建了一個語料庫,然后是一個文檔術語矩陣,然后完成了一些簡單的文本挖掘任務。

# 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)

在此輸入圖像描述

# 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"))

在此輸入圖像描述

這將糾正錯誤。

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

但我想讀你的Html你需要使用xml閱讀器。 就像是 :

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

但是你需要提供spec參數,這取決於你的文件結構。 例如,參見readReut21578XML 這是xml / html解析器的一個很好的例子。

要將所有html文件讀入R對象,您可以使用

# 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
})

這將為您提供一個列表,每個元素都是每個文件的HTML內容。

我稍后會解析它,我很着急。

我發現包samppipeR對於僅提取html頁面的“核心”文本特別有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM