且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用R读取PDF并进行数据挖掘

更新时间:2022-08-14 21:00:17

用R读取PDF并进行数据挖掘,例子如下:


[javascript] view plaincopyprint?
  1. # here is a pdf for mining  
  2. url "http://www.noisyroom.net/blog/RomneySpeech072912.pdf"  
  3. dest ".pdf")  
  4. download.file(url, dest, mode = "wb")  
  5.  
  6. # set path to pdftotxt.exe and convert pdf to text  
  7. exe "C:\\Program Files\\xpdfbin-win-3.03\\bin32\\pdftotext.exe"  
  8. system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = F)  
  9.  
  10. # get txt-file name and open it  
  11. filetxt ".pdf"".txt", dest)  
  12. shell.exec(filetxt); shell.exec(filetxt) # strangely the first try always throws an error..  
  13.  
  14. # do something with it, i.e. a simple word cloud  
  15. library(tm)  
  16. library(wordcloud)  
  17. library(Rstem)  
  18.   
  19. txt 
  20.   
  21. txt 
  22. txt "\\f", stopwords()))  
  23.   
  24. corpus 
  25. corpus 
  26. tdm 
  27.  
  28. # Stem words  
  29. d$stem "english")  
  30.  
  31. # and put words to column, otherwise they would be lost when aggregating  
  32. d$word 
  33.  
  34. # remove web address (very long string):  
  35.  
  36. # aggregate freqeuncy by word stem and  
  37. # keep first words..  
  38. agg_freq 
  39. agg_word function(x) x[1])  
  40.   
  41.  
  42. # sort by frequency  
  43.  
  44. # print wordcloud:  
  45. wordcloud(d$word, d$freq)  
  46.  
  47. # remove files  
  48. file.remove(dir(tempdir(), full.name=T)) # remove files