आर

2013-02-22 41 views
6

में कई एचटीएमएल फाइलों से कॉर्पस बनाएं, मैं डाउनलोड की गई HTML फ़ाइलों के संग्रह के लिए कॉर्पस बनाना चाहता हूं, और फिर भविष्य में टेक्स्ट खनन के लिए उन्हें आर में पढ़ना चाहता हूं।आर

  • कई html फ़ाइलें से एक कोर्पस बनाएँ:

    मूलतः, यह मुझे क्या करना चाहते हैं।

मैं DirSource का उपयोग करने की कोशिश की:

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

लेकिन यह सब एक ही बार में "अवैध निर्देशिका पैरामीटर" कोर्पस से html फ़ाइलें में

  • पढ़ें देता है। यह सुनिश्चित नहीं है कि यह कैसे करें।

  • उन्हें पार्स करें, उन्हें सादा पाठ में परिवर्तित करें, टैग हटाएं। कई लोगों ने एक्सएमएल का उपयोग करने का सुझाव दिया, हालांकि, मुझे कई फाइलों को संसाधित करने का कोई तरीका नहीं मिला। वे सब एक ही फाइल के लिए हैं।

बहुत बहुत धन्यवाद।

+0

अपने DirSource कॉल में फ़ॉरवर्ड स्लैश के बजाय बैकस्लैश का उपयोग करने का प्रयास करें। 'सी: \ test' –

+0

'कॉर्पस' और 'DirSource' आदेश क्या पैकेज हैं? –

उत्तर

2

यह त्रुटि को सही करेगा।

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

लेकिन मुझे लगता है कि आपके एचटीएमएल को पढ़ने के लिए आपको एक्सएमएल रीडर का उपयोग करने की आवश्यकता है। कुछ ऐसा:

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

लेकिन आपको spec तर्क की आपूर्ति करने की आवश्यकता है, जो आपकी फ़ाइल संरचना के साथ निर्भर करता है। उदाहरण के लिए देखें readReut21578XML। यह एक्सएमएल/एचटीएमएल पार्सर का एक अच्छा उदाहरण है।

0

एक अनुसंधान वस्तु में सभी html फ़ाइलें पढ़ने के लिए आप

# 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 सामग्री है।

मैं बाद में इसे पार्सिंग पर पोस्ट करूंगा, मैं जल्दबाजी में हूं।

11

ऐसा करना चाहिए। यहां मेरे पास एचटीएमएल फाइलों (एसओ से एक यादृच्छिक नमूना) के कंप्यूटर पर एक फ़ोल्डर है और मैंने उनमें से एक कॉर्पस बनाया है, फिर एक दस्तावेज़ शब्द मैट्रिक्स और फिर कुछ मामूली पाठ खनन कार्यों को किया है।

# 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

अच्छा। बस 'pattern ='। Html ''में' list.files (...) 'पैरामीटर जोड़ें। इसलिए फ़ोल्डर के अंदर अन्य फाइलें हो सकती हैं (* उदा।* डेटा, रीडमे और किसी भी अन्य गैर-एचटीएमएल फ़ाइल को डाउनलोड करने के लिए आर स्क्रिप्ट, उनके नामों के भीतर "एचटीएमएल" के साथ पाठ्यक्रम फाइलों से बाहर निकलें। –

+0

धन्यवाद, अच्छी टिप। मैंने तदनुसार संपादित किया है। – Ben

+0

इसे टीएम 0.6 के साथ काम करने के लिए, अपने कॉर्पस को PlainTextDocument में कनवर्ट करें अन्यथा आप टीडीएम नहीं बना पाएंगे। एक <- tm_map (ए, PlainTextDocument) करें – viksit

0

मैं पैकेज boilerpipeR विशेष रूप से एक html पृष्ठ के केवल "मूल" पाठ निकालने के लिए उपयोगी पाया।