« Analyse de sentiments avec R » : différence entre les versions

De EduTech Wiki
Aller à la navigation Aller à la recherche
Ligne 25 : Ligne 25 :
== Analyse de papiers de position sur EduTech Wiki Anglais ==
== Analyse de papiers de position sur EduTech Wiki Anglais ==


On prend la [[:en:category:Position_paper ||categorie "Position paper"]] de EduTechWiki. {{citation|These position papers were written by students enrolled in course Education 6620, Issues and Trends in Educational Computing at [http://www.mun.ca Memorial University of Newfoundland], [http://en.wikipedia.org/wiki/Newfoundland_and_Labrador Newfoundland and Labrador], Canada.}}. Evidémment, on devrait pas trouver des sentiments très contrastés dans un corpus très académique. On verra:
Pour tester ce paquet, on prend la [[:en:category:Position_paper|categorie "Position paper"]] de EduTechWiki. {{citation|These position papers were written by students enrolled in course Education 6620, Issues and Trends in Educational Computing at [http://www.mun.ca Memorial University of Newfoundland], [http://en.wikipedia.org/wiki/Newfoundland_and_Labrador Newfoundland and Labrador], Canada.}}. Evidémment, on ne devrait pas trouver des sentiments très contrastés dans un corpus très académique. On verra...


<source lang="matlab">
<source lang="matlab">

Version du 1 décembre 2014 à 02:18

Introduction

Analyse de sentiments avec sentiment

Installation

# Package installation and test

install.packages("devtools")
library("devtools")
install_github("sentiment", "andrie")
library("sentiment")

On peut tester un peu et regarder les dictionnaires qui viennent avec

sentiment(c("There is a terrible mistake in this work", "This is wonderful!"))
# Two dictonaries for English (built-in)
afinn96
afinn111
# Simple test
sentiment(c("There is a terrible mistake in this work", "This is wonderful!", "this is bloody brilliant"))

Analyse de papiers de position sur EduTech Wiki Anglais

Pour tester ce paquet, on prend la categorie "Position paper" de EduTechWiki. «These position papers were written by students enrolled in course Education 6620, Issues and Trends in Educational Computing at Memorial University of Newfoundland, Newfoundland and Labrador, Canada.». Evidémment, on ne devrait pas trouver des sentiments très contrastés dans un corpus très académique. On verra...

# ---- set working directory
# Linux
# setwd ("~/schneide/methodo/R")
# Windows
# setwd("s:/methodo/R")
setwd("c:/dks/methodo/R")
getwd()

library("sentiment")
library(tm)
library(XML)
library(tm.plugin.webmining)

# Get list of position papers from EduTechpospap
cat_pospap <- "http://edutechwiki.unige.ch/mediawiki/api.php?action=query&list=categorymembers&cmtitle=Category:Position_paper&cmlimit=500&cmtype=page&format=xml"
XML_list <- xmlTreeParse(cat_pospap,useInternalNodes = TRUE) 
XML_list
XML2_list <- xpathSApply(XML_list, "//cm")
title_list = sapply(XML2_list, function(el) xmlGetAttr(el, "title"))
id_list = sapply(XML2_list, function(el) xmlGetAttr(el, "pageid"))
title_list[[1]]
id_list[[1]]

# --- Identify the URLs for each page (article)
# début et fin de l'URL. Notez le "pageid" qui va nous sortir un article avec sa "pageid"
url_en_start <- "http://edutechwiki.unige.ch/mediawiki/api.php?action=parse&pageid="
url_en_end <- "&format=xml"
article_ids_list <- character(length(id_list))

for (i in 1:length(id_list)) {
  article_ids_list[i] <- (paste (url_en_start, id_list[i], url_en_end, sep=""))
}
# This is the list of articles
article_ids_list

# Define a reader function that will only read the "text" element
readMWXML <- 
  readXML (spec = list (content = list ("node", "//text"),
                        heading = list ("attribute", "//parse/@title")
  ),
  doc=PlainTextDocument())

# ----- download the page contents
pospap.source <- VCorpus(URISource(article_ids_list, encoding="UTF-8"),
                       readerControl=list(reader=readMWXML, language="en"))
names(pospap.source)

# On change les "id" (titres à la place d'URLs illisibles)
for (j in seq.int (pospap.source)) {
  meta(pospap.source[[j]],"id") <- title_list[j]
}

names(pospap.source)

# Ajouter une balise html autour du tout - c'est du bon vodoo
pospap.source <- tm_map (pospap.source, encloseHTML)
# Ecrire les fragments HTML dans des fichiers (inutile, mais permet l'inspection)
writeCorpus(pospap.source, path="./wiki_pospap_source")

# ------------------------------- Clean text into bags of words

pospap.cl1 <- tm_map(pospap.source, content_transformer(tolower))
pospap.cl2 <- tm_map(pospap.cl1, content_transformer(extractHTMLStrip))
pospap.cl2 <- tm_map (pospap.cl2, removePunctuation, preserve_intra_word_dashes = TRUE)
# curly quotes = \u2019
(kill_chars <- content_transformer (function(x, pattern) gsub(pattern, " ", x)))
pospap.cl2 <- tm_map (pospap.cl2, kill_chars, "\u2019")
pospap.cl2 <- tm_map (pospap.cl2, kill_chars,"'")
pospap.cl2 <- tm_map (pospap.cl2, kill_chars,"\\[modifier\\]")
pospap.cl2 <- tm_map (pospap.cl2, kill_chars,"[«»”“\"]")

pospap.essence <- tm_map (pospap.cl2, removeWords, stopwords("english"))
pospap.roots <- tm_map (pospap.essence, stemDocument, language="english")
pospap.roots <- tm_map (pospap.roots, stripWhitespace)

# test
pospap.roots[[1]]
class(pospap.racines)

for (docN in seq.int (pospap.roots)) {
  print (paste ( pospap.roots[[docN]]$meta$heading,
         " = ",
         sentiment (pospap.roots[[docN]]$content )))
}

Résultats bruts: On voit qu'il y ait une petite variance

  1. "Achievement = -0.357142857142857"
  2. "Active Learning = 0.594594594594595"
  3. "Assessments = 0.758620689655172"
  4. "At-risk Learners = -1.15"
  5. "Career and Guidance = 0.586206896551724"
  6. "Classroom management = 0.288888888888889"
  7. "Collaboration = 0.957446808510638"
  8. "Comprehension = -0.0833333333333333"
  9. "Constructivist learning = 0.941176470588235"
  10. "Critical Thinking = -0.6"
  11. "Differentiated learning = 0.909090909090909"
  12. "Early Childhood Education = 0.848484848484849"
  13. "English Language Arts = 0.277777777777778"
  14. "ESL = 0.157894736842105"
  15. "Hearing Imparied Learner = -0.0526315789473684"
  16. "Inclusive learning = 0.32258064516129"
  17. "Interaction = 0.736842105263158"
  18. "Learning in Rural Contexts = -2"
  19. "Lifelong Learning = 0.813953488372093"
  20. "Literacy = 0.025"
  21. "Mathematics = -0.0740740740740741"
  22. "Medicine = -0.175"
  23. "Music = -0.0833333333333333"
  24. "Non-Formal Learning = 0.0434782608695652"
  25. "Open Educational Resources = 0.631578947368421"
  26. "Parents = 0.357142857142857"
  27. "Personalized learning = 0.285714285714286"
  28. "Portfolio = 1.20588235294118"
  29. "Professional Learning Communities = 0.6875"
  30. "Reading = 1.11428571428571"
  31. "Reflecting = 0.387096774193548"
  32. "Science = 0.527777777777778"
  33. "Second Language Assessment = 0.909090909090909"
  34. "Vocational Learning = -0.1"
  35. "Workplace Learning = 0.205128205128205"
  36. "Writing = 0.6875"