在R中从多个HTML文件创建语料库

8

我想创建一个语料库用于收集下载的HTML文件,并在未来的文本挖掘中使用R进行读取。

基本上,这是我想要做的:

  • 从多个HTML文件中创建一个语料库。

我尝试使用DirSource:

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

但是它返回“无效的目录参数”
  • 一次性从语料库中读取所有的html文件。 不确定如何做。

  • 解析它们,将它们转换为纯文本,删除标签。 许多人建议使用XML,但我没有找到一种处理多个文件的方法。它们都是针对单个文件的。

非常感谢。


尝试在DirSource调用中使用反斜杠而不是正斜杠。 C:\ test - Brandon Bertelsen
CorpusDirSource命令属于哪个软件包? - CHP
4个回答

15

这应该就可以了。我在电脑上有一个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)

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
不错。只需在 list.files(...) 中添加参数 pattern=".html",这样文件夹中就可以有其他文件(例如用于下载数据的 R 脚本、README 和任何其他非 HTML 文件),当然除了文件名中包含 "html" 的文件。 - Oscar de León
为了让这个与 tm 0.6 兼容,请将你的语料库转换为 PlainTextDocument,否则你将无法创建 TDM。执行 a <- tm_map(a, PlainTextDocument)。 - viksit

2

这将纠正错误。

 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解析器示例。


0
我发现包boilerpipeR非常有用,可以提取网页的“核心”文本。

0

要将所有的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内容。

我稍后会发布有关解析它的内容,我很匆忙。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接