8

ダウンロードした HTML ファイルのコレクションのコーパスを作成し、将来のテキスト マイニングのために R で読みたいと思います。

本質的に、これは私がやりたいことです:

  • 複数の html ファイルからコーパスを作成します。

私はDirSourceを使用しようとしました:

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

しかし、それは「無効なディレクトリパラメータ」を返します

  • コーパスから html ファイルを一括読み込みします。それを行う方法がわからない。

  • それらを解析し、プレーン テキストに変換し、タグを削除します。多くの人が XML の使用を提案しましたが、複数のファイルを処理する方法が見つかりませんでした。それらはすべて 1 つのファイル用です。

どうもありがとう。

4

4 に答える 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)

ここに画像の説明を入力

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

ここに画像の説明を入力

于 2013-02-22T07:38:22.437 に答える
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 パーサーの良い例です。

于 2013-02-22T04:12:57.460 に答える
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 コンテンツです。

急いでいるので、後で解析して投稿します。

于 2013-02-22T04:16:55.540 に答える