--- title: "Mallet_with_R" author: "Jean-Baptiste Camps, modifié par Alice Dionnet" date: "3-9 février 2020 modification Septembre 2022" output: html_document --- ```{r} Sys.setenv(JAVA_HOME="C:/Program Files/Java/jdk-10.0.1/") ``` ```{r} install.packages("cowplot") install.packages("dplyr") install.packages("FactoMineR") install.packages("ggpubr") ``` #Special R java ```{r} install.packages("ggplot2") install.packages("ellipsis") install.packages("rJava") library ("rJava") library ("mallet") library ("knitr") library ("factoextra") install.packages('Deducer','http://www.rforge.net/',type='source') library ('Deducer') ``` ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ## Création des modèles de sujet ```{r} d = read.csv(file = "dishonored_lemma.tsv", sep = "\t", quote = '\"', header = TRUE, encoding="UTF-8") # On a maintenant nrow(d) #échantillons # Import des documents library("mallet") docs = mallet::mallet.import(rownames(d), as.character(d[,1]),"DISH_stopwords.txt", token.regexp = "[\\S]+") ``` # Créons le modèle à entraîner: ```{r} topic.model <- MalletLDA(num.topics = 10, alpha.sum = 1, beta = 0.1) ``` #Charger un modèle # ```{r} # topic.model <- load(file = "modeleDAO_5topics_quete_environnement_spirituel_queteprincipale_actions") #``` #Chargeons les docs ```{r} topic.model$loadDocuments(docs) ``` # Optimisation des hyperparamètres (\code{alpha} and \code{beta}) toutes les 20 itérations, après 50 itérations initiales. ```{r} topic.model$setAlphaOptimization(20, 50) ``` # Now train a model. Note that hyperparameter optimization is on, by default. We can specify the number of iterations. Here we'll use a large-ish round number. ```{r} topic.model$train(500) #topic.model$train(100) # On peut regarder comment les valeurs ont changé # topic.model$getAlpha() # topic.model$model$beta ``` ## Visualisation interactive avec LDAvis ```{r} install.packages("LDAvis") # Si besoin: install.packages("servr") ``` ```{r} library('servr') library("LDAvis") source("LDAvis_function.R") makeLDAvis(topic.model, outDir = tempfile(), openBrowser = TRUE) ``` ## Sauvegarde d'un modèle #```{r} #save(topic.model, file = "modeleDISH") #```