1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| library(XML)
library(tm)
#Lecture des documents et stockage dans des variables
#Étape 1 : récupération des pages internet
#Étape 2 : affectation du texte uniquement à un vecteur
#--------------- Divers
divers1 <- htmlParse(file="http://en.wikipedia.org/wiki/Largest_organisms")
divers2 <- htmlParse(file="http://en.wikipedia.org/wiki/Human_rights")
divers3 <- htmlParse(file="http://en.wikipedia.org/wiki/Business_process")
divers4 <- htmlParse(file="http://en.wikipedia.org/wiki/Agriculture")
#--------------- Astronomie
astronomie1 <- htmlParse(file="http://en.wikipedia.org/wiki/Star")
astronomie2 <- htmlParse(file="http://en.wikipedia.org/wiki/Galaxy")
astronomie3 <- htmlParse(file="http://en.wikipedia.org/wiki/Planet")
#--------------- Alimentation
alimentation1 <- htmlParse(file="http://en.wikipedia.org/wiki/Bread")
alimentation2 <- htmlParse(file="http://en.wikipedia.org/wiki/Food")
alimentation3 <- htmlParse(file="http://en.wikipedia.org/wiki/Protein_(nutrient)")
#--------------- Chimie
chimie1 <- htmlParse(file="http://en.wikipedia.org/wiki/Hydrogen")
chimie2 <- htmlParse(file="http://en.wikipedia.org/wiki/Protein")
chimie3 <- htmlParse(file="http://en.wikipedia.org/wiki/Biochemistry")
#13 variables correspondant à autant d'articles étudiés
largest <- xpathSApply(divers1,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
human <- xpathSApply(divers2,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
business <- xpathSApply(divers3,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
agri <- xpathSApply(divers4,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
star <- xpathSApply(astronomie1,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
galaxy <- xpathSApply(astronomie2,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
planet <- xpathSApply(astronomie3,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
bread <- xpathSApply(alimentation1,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
food <- xpathSApply(alimentation2,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
protein_n <- xpathSApply(alimentation3,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
hydrogen <- xpathSApply(chimie1,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
protein <- xpathSApply(chimie2,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
biochemistry <- xpathSApply(chimie3,"//div[contains(@class, 'mw-content-ltr')]/p", xmlValue)
#Concaténation des vecteurs en un seul (par sujet)
divers <- c(largest, human, business, agri)
astronomie <- c(star, galaxy, planet)
alimentation <- c(bread, food, protein_n)
chimie <- c(hydrogen, protein, biochemistry)
#Nettoyage du vecteur
#supprime les chiffres contenus dans le texte
#On supprime aussi les autres caractères
#ce caractère apparait souvent
#cette suite également
#On passe tout en minuscule pour que les mots soient identiques.
nettoyer <- function(x){
x <- gsub("[0-9]+"," ",x)
x <- gsub("[\\’\\Â\"\\,\\.;'()/%£$:=-]"," ",x)
x <- gsub("\\["," ",x)
x <- gsub("\\]"," ",x)
#x <- gsub("Â"," ",x)
x <- gsub("–"," ",x)
x <- gsub("[ ]+", " ",x)
x <- tolower(x)
}
#Phase de nettoyage des données
divers <- nettoyer(divers)
astronomie <- nettoyer(astronomie)
alimentation <- nettoyer(alimentation)
chimie <- nettoyer(chimie)
#Phase de construction du corpus et de l'objet TermDocumentMatrix
langue <- stopwords("english")
creer_corpus <- function(x, langue){
#Supprime les stopwords sélectionnés dans le texte x
for (supp in langue){
supp <- paste(" ",supp, " ", sep="")
x <- gsub(supp, " ", x)
}
#Découpage en mots
mots <- strsplit(x, " ")
#Création du corpus
sour <- VectorSource(mots)
dico <- Corpus(sour, language="en")
dico <- tm_map(dico, stemDocument)
dictionnaire <- TermDocumentMatrix(dico)
return(dictionnaire)
}
creer_corpus(divers, langue)
creer_corpus(astronomie, langue)
creer_corpus(alimentation, langue)
creer_corpus(chimie, langue) |
Partager