R Markdown

Webscraping

Descarga de documentos a través de Webscraping en R para tener los textos de las Conferencias de la Mañanera del presidente Andrés Manuel López Obrador del 2023, del 01 de enero al 31 de Agosto

library(rvest)
library(magrittr)
#Esta librería aumenta la clairdad de la lectura de un archivo, medaynte %>% que es una sintaxis en cadena, de forma que el operador %> % coge el output ('la salida') de una sentencia de código y la convierte en el input ('el argumento') de una nueva sentencia.

url <- "https://www.gob.mx/presidencia/articulos/version-estenografica-conferencia-de-prensa-del-presidente-andres-manuel-lopez-obrador-del-9-de-agosto-de-2023"
pagina <- read_html(url)

# Recuerda reemplazar "XPATH_o_CSS_selector" con el selector adecuado
texto <- pagina %>%
  #html_nodes("XPATH_o_CSS_selector") %>%
  html_text()

fecha_conferencia <- "9_de_agosto_de_2023" # Esta es una forma simple, pero puedes obtener la fecha de la URL o de otra parte si es dinámica.
nombre_archivo <- paste0("conferencia_", fecha_conferencia, ".txt")

writeLines(texto, nombre_archivo)

fechas <- seq.Date(as.Date("2023-01-01"), as.Date("2023-08-31"), by="days")

#Guardando archivo en .txt
scrape_y_guarda <- function(fecha) {
  # Cambia el local a español
  Sys.setlocale("LC_TIME", "es_ES.UTF-8")
  # Formatea la fecha para la URL
  day <- as.numeric(format(fecha, "%d"))
  month <- format(fecha, "%B")
  #month_NUM <- FORMAT(FECHA, "%M")
  year <- format(fecha, "%Y")
  fecha_url <- paste(day, "de", month, "de", year, sep="-")
  url <- paste0("https://www.gob.mx/presidencia/articulos/version-estenografica-conferencia-de-prensa-del-presidente-andres-manuel-lopez-obrador-del-", fecha_url)
  
  pagina <- read_html(url)
  
  # Suponiendo que el contenido de la conferencia está en un selector específico, como <div class="contenido-conferencia"> 
 # (es necesario verificar y ajustar este selector de acuerdo al código fuente real de la página)
  texto <- pagina %>%
    #html_nodes(".contenido-conferencia") %>%  # <-- reemplaza ".contenido-conferencia" con el selector CSS correcto
    html_text()
  
  # Crea el nombre del archivo y lo guarda
  nombre_archivo <- paste0("conferencia_", gsub("-de-", "_", fecha_url), ".txt")
  writeLines(texto, nombre_archivo)
}

lapply(fechas, scrape_y_guarda)
## [[1]]
## NULL
## 
## [[2]]
## NULL
## 
## [[3]]
## NULL
## 
## [[4]]
## NULL
## 
## [[5]]
## NULL
## 
## [[6]]
## NULL
## 
## [[7]]
## NULL
## 
## [[8]]
## NULL
## 
## [[9]]
## NULL
## 
## [[10]]
## NULL
## 
## [[11]]
## NULL
## 
## [[12]]
## NULL
## 
## [[13]]
## NULL
## 
## [[14]]
## NULL
## 
## [[15]]
## NULL
## 
## [[16]]
## NULL
## 
## [[17]]
## NULL
## 
## [[18]]
## NULL
## 
## [[19]]
## NULL
## 
## [[20]]
## NULL
## 
## [[21]]
## NULL
## 
## [[22]]
## NULL
## 
## [[23]]
## NULL
## 
## [[24]]
## NULL
## 
## [[25]]
## NULL
## 
## [[26]]
## NULL
## 
## [[27]]
## NULL
## 
## [[28]]
## NULL
## 
## [[29]]
## NULL
## 
## [[30]]
## NULL
## 
## [[31]]
## NULL
## 
## [[32]]
## NULL
## 
## [[33]]
## NULL
## 
## [[34]]
## NULL
## 
## [[35]]
## NULL
## 
## [[36]]
## NULL
## 
## [[37]]
## NULL
## 
## [[38]]
## NULL
## 
## [[39]]
## NULL
## 
## [[40]]
## NULL
## 
## [[41]]
## NULL
## 
## [[42]]
## NULL
## 
## [[43]]
## NULL
## 
## [[44]]
## NULL
## 
## [[45]]
## NULL
## 
## [[46]]
## NULL
## 
## [[47]]
## NULL
## 
## [[48]]
## NULL
## 
## [[49]]
## NULL
## 
## [[50]]
## NULL
## 
## [[51]]
## NULL
## 
## [[52]]
## NULL
## 
## [[53]]
## NULL
## 
## [[54]]
## NULL
## 
## [[55]]
## NULL
## 
## [[56]]
## NULL
## 
## [[57]]
## NULL
## 
## [[58]]
## NULL
## 
## [[59]]
## NULL
## 
## [[60]]
## NULL
## 
## [[61]]
## NULL
## 
## [[62]]
## NULL
## 
## [[63]]
## NULL
## 
## [[64]]
## NULL
## 
## [[65]]
## NULL
## 
## [[66]]
## NULL
## 
## [[67]]
## NULL
## 
## [[68]]
## NULL
## 
## [[69]]
## NULL
## 
## [[70]]
## NULL
## 
## [[71]]
## NULL
## 
## [[72]]
## NULL
## 
## [[73]]
## NULL
## 
## [[74]]
## NULL
## 
## [[75]]
## NULL
## 
## [[76]]
## NULL
## 
## [[77]]
## NULL
## 
## [[78]]
## NULL
## 
## [[79]]
## NULL
## 
## [[80]]
## NULL
## 
## [[81]]
## NULL
## 
## [[82]]
## NULL
## 
## [[83]]
## NULL
## 
## [[84]]
## NULL
## 
## [[85]]
## NULL
## 
## [[86]]
## NULL
## 
## [[87]]
## NULL
## 
## [[88]]
## NULL
## 
## [[89]]
## NULL
## 
## [[90]]
## NULL
## 
## [[91]]
## NULL
## 
## [[92]]
## NULL
## 
## [[93]]
## NULL
## 
## [[94]]
## NULL
## 
## [[95]]
## NULL
## 
## [[96]]
## NULL
## 
## [[97]]
## NULL
## 
## [[98]]
## NULL
## 
## [[99]]
## NULL
## 
## [[100]]
## NULL
## 
## [[101]]
## NULL
## 
## [[102]]
## NULL
## 
## [[103]]
## NULL
## 
## [[104]]
## NULL
## 
## [[105]]
## NULL
## 
## [[106]]
## NULL
## 
## [[107]]
## NULL
## 
## [[108]]
## NULL
## 
## [[109]]
## NULL
## 
## [[110]]
## NULL
## 
## [[111]]
## NULL
## 
## [[112]]
## NULL
## 
## [[113]]
## NULL
## 
## [[114]]
## NULL
## 
## [[115]]
## NULL
## 
## [[116]]
## NULL
## 
## [[117]]
## NULL
## 
## [[118]]
## NULL
## 
## [[119]]
## NULL
## 
## [[120]]
## NULL
## 
## [[121]]
## NULL
## 
## [[122]]
## NULL
## 
## [[123]]
## NULL
## 
## [[124]]
## NULL
## 
## [[125]]
## NULL
## 
## [[126]]
## NULL
## 
## [[127]]
## NULL
## 
## [[128]]
## NULL
## 
## [[129]]
## NULL
## 
## [[130]]
## NULL
## 
## [[131]]
## NULL
## 
## [[132]]
## NULL
## 
## [[133]]
## NULL
## 
## [[134]]
## NULL
## 
## [[135]]
## NULL
## 
## [[136]]
## NULL
## 
## [[137]]
## NULL
## 
## [[138]]
## NULL
## 
## [[139]]
## NULL
## 
## [[140]]
## NULL
## 
## [[141]]
## NULL
## 
## [[142]]
## NULL
## 
## [[143]]
## NULL
## 
## [[144]]
## NULL
## 
## [[145]]
## NULL
## 
## [[146]]
## NULL
## 
## [[147]]
## NULL
## 
## [[148]]
## NULL
## 
## [[149]]
## NULL
## 
## [[150]]
## NULL
## 
## [[151]]
## NULL
## 
## [[152]]
## NULL
## 
## [[153]]
## NULL
## 
## [[154]]
## NULL
## 
## [[155]]
## NULL
## 
## [[156]]
## NULL
## 
## [[157]]
## NULL
## 
## [[158]]
## NULL
## 
## [[159]]
## NULL
## 
## [[160]]
## NULL
## 
## [[161]]
## NULL
## 
## [[162]]
## NULL
## 
## [[163]]
## NULL
## 
## [[164]]
## NULL
## 
## [[165]]
## NULL
## 
## [[166]]
## NULL
## 
## [[167]]
## NULL
## 
## [[168]]
## NULL
## 
## [[169]]
## NULL
## 
## [[170]]
## NULL
## 
## [[171]]
## NULL
## 
## [[172]]
## NULL
## 
## [[173]]
## NULL
## 
## [[174]]
## NULL
## 
## [[175]]
## NULL
## 
## [[176]]
## NULL
## 
## [[177]]
## NULL
## 
## [[178]]
## NULL
## 
## [[179]]
## NULL
## 
## [[180]]
## NULL
## 
## [[181]]
## NULL
## 
## [[182]]
## NULL
## 
## [[183]]
## NULL
## 
## [[184]]
## NULL
## 
## [[185]]
## NULL
## 
## [[186]]
## NULL
## 
## [[187]]
## NULL
## 
## [[188]]
## NULL
## 
## [[189]]
## NULL
## 
## [[190]]
## NULL
## 
## [[191]]
## NULL
## 
## [[192]]
## NULL
## 
## [[193]]
## NULL
## 
## [[194]]
## NULL
## 
## [[195]]
## NULL
## 
## [[196]]
## NULL
## 
## [[197]]
## NULL
## 
## [[198]]
## NULL
## 
## [[199]]
## NULL
## 
## [[200]]
## NULL
## 
## [[201]]
## NULL
## 
## [[202]]
## NULL
## 
## [[203]]
## NULL
## 
## [[204]]
## NULL
## 
## [[205]]
## NULL
## 
## [[206]]
## NULL
## 
## [[207]]
## NULL
## 
## [[208]]
## NULL
## 
## [[209]]
## NULL
## 
## [[210]]
## NULL
## 
## [[211]]
## NULL
## 
## [[212]]
## NULL
## 
## [[213]]
## NULL
## 
## [[214]]
## NULL
## 
## [[215]]
## NULL
## 
## [[216]]
## NULL
## 
## [[217]]
## NULL
## 
## [[218]]
## NULL
## 
## [[219]]
## NULL
## 
## [[220]]
## NULL
## 
## [[221]]
## NULL
## 
## [[222]]
## NULL
## 
## [[223]]
## NULL
## 
## [[224]]
## NULL
## 
## [[225]]
## NULL
## 
## [[226]]
## NULL
## 
## [[227]]
## NULL
## 
## [[228]]
## NULL
## 
## [[229]]
## NULL
## 
## [[230]]
## NULL
## 
## [[231]]
## NULL
## 
## [[232]]
## NULL
## 
## [[233]]
## NULL
## 
## [[234]]
## NULL
## 
## [[235]]
## NULL
## 
## [[236]]
## NULL
## 
## [[237]]
## NULL
## 
## [[238]]
## NULL
## 
## [[239]]
## NULL
## 
## [[240]]
## NULL
## 
## [[241]]
## NULL
## 
## [[242]]
## NULL
## 
## [[243]]
## NULL

Eliminar archivos que no contengan contenido de la mañanera

# Lista de archivos .txt en la carpeta
archivos_txt <- list.files(pattern = "*.txt")

# Frase específica que deseas buscar
frase_especifica <- "Conferencia de prensa del presidente Andrés Manuel López Obrador"
archivos <- lapply(archivos_txt, function(archivo) {
  contenido <- readLines(archivo, warn = FALSE)
  if (any(grepl(frase_especifica, contenido))) {
    return(archivo)
  } else {
    unlink(archivo)
    return(NULL)
  }
})

archivos <- Filter(Negate(is.null), archivos)
archivos
## [[1]]
## [1] "conferencia_1_agosto_2023.txt"
## 
## [[2]]
## [1] "conferencia_1_febrero_2023.txt"
## 
## [[3]]
## [1] "conferencia_1_junio_2023.txt"
## 
## [[4]]
## [1] "conferencia_1_marzo_2023.txt"
## 
## [[5]]
## [1] "conferencia_1_mayo_2023.txt"
## 
## [[6]]
## [1] "conferencia_10_abril_2023.txt"
## 
## [[7]]
## [1] "conferencia_10_agosto_2023.txt"
## 
## [[8]]
## [1] "conferencia_10_febrero_2023.txt"
## 
## [[9]]
## [1] "conferencia_10_marzo_2023.txt"
## 
## [[10]]
## [1] "conferencia_10_mayo_2023.txt"
## 
## [[11]]
## [1] "conferencia_11_abril_2023.txt"
## 
## [[12]]
## [1] "conferencia_11_agosto_2023.txt"
## 
## [[13]]
## [1] "conferencia_12_abril_2023.txt"
## 
## [[14]]
## [1] "conferencia_12_enero_2023.txt"
## 
## [[15]]
## [1] "conferencia_12_julio_2023.txt"
## 
## [[16]]
## [1] "conferencia_12_junio_2023.txt"
## 
## [[17]]
## [1] "conferencia_12_mayo_2023.txt"
## 
## [[18]]
## [1] "conferencia_13_abril_2023.txt"
## 
## [[19]]
## [1] "conferencia_13_enero_2023.txt"
## 
## [[20]]
## [1] "conferencia_13_febrero_2023.txt"
## 
## [[21]]
## [1] "conferencia_13_julio_2023.txt"
## 
## [[22]]
## [1] "conferencia_13_junio_2023.txt"
## 
## [[23]]
## [1] "conferencia_13_marzo_2023.txt"
## 
## [[24]]
## [1] "conferencia_14_abril_2023.txt"
## 
## [[25]]
## [1] "conferencia_14_agosto_2023.txt"
## 
## [[26]]
## [1] "conferencia_14_febrero_2023.txt"
## 
## [[27]]
## [1] "conferencia_14_junio_2023.txt"
## 
## [[28]]
## [1] "conferencia_14_marzo_2023.txt"
## 
## [[29]]
## [1] "conferencia_15_agosto_2023.txt"
## 
## [[30]]
## [1] "conferencia_15_febrero_2023.txt"
## 
## [[31]]
## [1] "conferencia_15_junio_2023.txt"
## 
## [[32]]
## [1] "conferencia_15_marzo_2023.txt"
## 
## [[33]]
## [1] "conferencia_16_agosto_2023.txt"
## 
## [[34]]
## [1] "conferencia_16_enero_2023.txt"
## 
## [[35]]
## [1] "conferencia_16_febrero_2023.txt"
## 
## [[36]]
## [1] "conferencia_16_junio_2023.txt"
## 
## [[37]]
## [1] "conferencia_16_marzo_2023.txt"
## 
## [[38]]
## [1] "conferencia_16_mayo_2023.txt"
## 
## [[39]]
## [1] "conferencia_17_abril_2023.txt"
## 
## [[40]]
## [1] "conferencia_17_agosto_2023.txt"
## 
## [[41]]
## [1] "conferencia_17_enero_2023.txt"
## 
## [[42]]
## [1] "conferencia_17_febrero_2023.txt"
## 
## [[43]]
## [1] "conferencia_17_marzo_2023.txt"
## 
## [[44]]
## [1] "conferencia_17_mayo_2023.txt"
## 
## [[45]]
## [1] "conferencia_18_abril_2023.txt"
## 
## [[46]]
## [1] "conferencia_18_enero_2023.txt"
## 
## [[47]]
## [1] "conferencia_18_julio_2023.txt"
## 
## [[48]]
## [1] "conferencia_18_marzo_2023.txt"
## 
## [[49]]
## [1] "conferencia_18_mayo_2023.txt"
## 
## [[50]]
## [1] "conferencia_19_abril_2023.txt"
## 
## [[51]]
## [1] "conferencia_19_enero_2023.txt"
## 
## [[52]]
## [1] "conferencia_19_julio_2023.txt"
## 
## [[53]]
## [1] "conferencia_19_junio_2023.txt"
## 
## [[54]]
## [1] "conferencia_19_mayo_2023.txt"
## 
## [[55]]
## [1] "conferencia_2_agosto_2023.txt"
## 
## [[56]]
## [1] "conferencia_2_enero_2023.txt"
## 
## [[57]]
## [1] "conferencia_2_febrero_2023.txt"
## 
## [[58]]
## [1] "conferencia_2_marzo_2023.txt"
## 
## [[59]]
## [1] "conferencia_2_mayo_2023.txt"
## 
## [[60]]
## [1] "conferencia_20_abril_2023.txt"
## 
## [[61]]
## [1] "conferencia_20_enero_2023.txt"
## 
## [[62]]
## [1] "conferencia_20_febrero_2023.txt"
## 
## [[63]]
## [1] "conferencia_20_julio_2023.txt"
## 
## [[64]]
## [1] "conferencia_20_junio_2023.txt"
## 
## [[65]]
## [1] "conferencia_21_abril_2023.txt"
## 
## [[66]]
## [1] "conferencia_21_agosto_2023.txt"
## 
## [[67]]
## [1] "conferencia_21_febrero_2023.txt"
## 
## [[68]]
## [1] "conferencia_21_julio_2023.txt"
## 
## [[69]]
## [1] "conferencia_21_junio_2023.txt"
## 
## [[70]]
## [1] "conferencia_21_marzo_2023.txt"
## 
## [[71]]
## [1] "conferencia_22_agosto_2023.txt"
## 
## [[72]]
## [1] "conferencia_22_febrero_2023.txt"
## 
## [[73]]
## [1] "conferencia_22_junio_2023.txt"
## 
## [[74]]
## [1] "conferencia_22_marzo_2023.txt"
## 
## [[75]]
## [1] "conferencia_23_agosto_2023.txt"
## 
## [[76]]
## [1] "conferencia_23_enero_2023.txt"
## 
## [[77]]
## [1] "conferencia_23_febrero_2023.txt"
## 
## [[78]]
## [1] "conferencia_23_junio_2023.txt"
## 
## [[79]]
## [1] "conferencia_23_marzo_2023.txt"
## 
## [[80]]
## [1] "conferencia_23_mayo_2023.txt"
## 
## [[81]]
## [1] "conferencia_24_abril_2023.txt"
## 
## [[82]]
## [1] "conferencia_24_agosto_2023.txt"
## 
## [[83]]
## [1] "conferencia_24_enero_2023.txt"
## 
## [[84]]
## [1] "conferencia_24_febrero_2023.txt"
## 
## [[85]]
## [1] "conferencia_24_julio_2023.txt"
## 
## [[86]]
## [1] "conferencia_24_marzo_2023.txt"
## 
## [[87]]
## [1] "conferencia_25_agosto_2023.txt"
## 
## [[88]]
## [1] "conferencia_25_enero_2023.txt"
## 
## [[89]]
## [1] "conferencia_25_julio_2023.txt"
## 
## [[90]]
## [1] "conferencia_25_mayo_2023.txt"
## 
## [[91]]
## [1] "conferencia_26_abril_2023.txt"
## 
## [[92]]
## [1] "conferencia_26_enero_2023.txt"
## 
## [[93]]
## [1] "conferencia_26_mayo_2023.txt"
## 
## [[94]]
## [1] "conferencia_27_abril_2023.txt"
## 
## [[95]]
## [1] "conferencia_27_enero_2023.txt"
## 
## [[96]]
## [1] "conferencia_27_julio_2023.txt"
## 
## [[97]]
## [1] "conferencia_27_junio_2023.txt"
## 
## [[98]]
## [1] "conferencia_28_abril_2023.txt"
## 
## [[99]]
## [1] "conferencia_28_febrero_2023.txt"
## 
## [[100]]
## [1] "conferencia_28_junio_2023.txt"
## 
## [[101]]
## [1] "conferencia_28_marzo_2023.txt"
## 
## [[102]]
## [1] "conferencia_29_agosto_2023.txt"
## 
## [[103]]
## [1] "conferencia_29_junio_2023.txt"
## 
## [[104]]
## [1] "conferencia_29_marzo_2023.txt"
## 
## [[105]]
## [1] "conferencia_29_mayo_2023.txt"
## 
## [[106]]
## [1] "conferencia_3_abril_2023.txt"
## 
## [[107]]
## [1] "conferencia_3_enero_2023.txt"
## 
## [[108]]
## [1] "conferencia_3_febrero_2023.txt"
## 
## [[109]]
## [1] "conferencia_3_marzo_2023.txt"
## 
## [[110]]
## [1] "conferencia_3_mayo_2023.txt"
## 
## [[111]]
## [1] "conferencia_30_agosto_2023.txt"
## 
## [[112]]
## [1] "conferencia_30_enero_2023.txt"
## 
## [[113]]
## [1] "conferencia_30_junio_2023.txt"
## 
## [[114]]
## [1] "conferencia_30_marzo_2023.txt"
## 
## [[115]]
## [1] "conferencia_30_mayo_2023.txt"
## 
## [[116]]
## [1] "conferencia_31_agosto_2023.txt"
## 
## [[117]]
## [1] "conferencia_31_enero_2023.txt"
## 
## [[118]]
## [1] "conferencia_31_julio_2023.txt"
## 
## [[119]]
## [1] "conferencia_31_marzo_2023.txt"
## 
## [[120]]
## [1] "conferencia_31_mayo_2023.txt"
## 
## [[121]]
## [1] "conferencia_4_abril_2023.txt"
## 
## [[122]]
## [1] "conferencia_4_enero_2023.txt"
## 
## [[123]]
## [1] "conferencia_4_mayo_2023.txt"
## 
## [[124]]
## [1] "conferencia_5_enero_2023.txt"
## 
## [[125]]
## [1] "conferencia_5_junio_2023.txt"
## 
## [[126]]
## [1] "conferencia_5_mayo_2023.txt"
## 
## [[127]]
## [1] "conferencia_6_enero_2023.txt"
## 
## [[128]]
## [1] "conferencia_6_febrero_2023.txt"
## 
## [[129]]
## [1] "conferencia_6_junio_2023.txt"
## 
## [[130]]
## [1] "conferencia_6_marzo_2023.txt"
## 
## [[131]]
## [1] "conferencia_7_febrero_2023.txt"
## 
## [[132]]
## [1] "conferencia_7_junio_2023.txt"
## 
## [[133]]
## [1] "conferencia_7_marzo_2023.txt"
## 
## [[134]]
## [1] "conferencia_8_agosto_2023.txt"
## 
## [[135]]
## [1] "conferencia_8_febrero_2023.txt"
## 
## [[136]]
## [1] "conferencia_8_junio_2023.txt"
## 
## [[137]]
## [1] "conferencia_8_marzo_2023.txt"
## 
## [[138]]
## [1] "conferencia_8_mayo_2023.txt"
## 
## [[139]]
## [1] "conferencia_9_agosto_2023.txt"
## 
## [[140]]
## [1] "conferencia_9_de_agosto_de_2023.txt"
## 
## [[141]]
## [1] "conferencia_9_enero_2023.txt"
## 
## [[142]]
## [1] "conferencia_9_febrero_2023.txt"
## 
## [[143]]
## [1] "conferencia_9_junio_2023.txt"
## 
## [[144]]
## [1] "conferencia_9_marzo_2023.txt"

Limpieza del texto: que solo quede la transcripción de la mañanera y nada más

# Función para procesar un archivo
procesar_archivo <- function(archivo) {
  # Leer el contenido del archivo
  contenido <- readLines(archivo, warn = FALSE)
  
  # Buscar la primera aparición de "PRESIDENTE ANDRÉS MANUEL LÓPEZ OBRADOR:"
  inicio <- grep("PRESIDENTE ANDRÉS MANUEL LÓPEZ OBRADOR:", contenido, ignore.case = TRUE)
  
  # Buscar la primera aparición de "---"
  fin <- grep("---", contenido)
  
  # Verificar si se encontraron las marcas de inicio y fin
  if (length(inicio) > 0 && length(fin) > 0) {
    # Extraer el contenido entre las marcas de inicio y fin
    nuevo_contenido <- contenido[(inicio + 1):(fin - 1)]
    
    # Escribir el nuevo contenido de vuelta al archivo
    writeLines(nuevo_contenido, con = archivo)
  }
}

# Aplicar la función a cada archivo en la lista
lapply(archivos, procesar_archivo)
## Warning in (inicio + 1):(fin - 1): numerical expression has 100 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 59 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 22 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 25 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 53 elements: only
## the first used

## Warning in (inicio + 1):(fin - 1): numerical expression has 53 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 33 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 29 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 23 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 58 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 50 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 51 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 59 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 89 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 52 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 37 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 27 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 58 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 36 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 61 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 50 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 34 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 58 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 48 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 38 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 36 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 55 elements: only
## the first used

## Warning in (inicio + 1):(fin - 1): numerical expression has 55 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 21 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 41 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 37 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 62 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 11 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 54 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 63 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 30 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 23 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 35 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 30 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 43 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 56 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 25 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 51 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 45 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 32 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 10 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 30 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 33 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 57 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 41 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 20 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 43 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 52 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 40 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 37 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 56 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 36 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 47 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 35 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 28 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 35 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 47 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 42 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 65 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 20 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 60 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 42 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 46 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 22 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 29 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 46 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 37 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 14 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 46 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 59 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 74 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 63 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 64 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 42 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 81 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 43 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 72 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 35 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 60 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 62 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 67 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 18 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 70 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 35 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 77 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 60 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 62 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 26 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 45 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 35 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 49 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 33 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 37 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 43 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 61 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 56 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 26 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 43 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 37 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 27 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 17 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 54 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 66 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 22 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 63 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 53 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 30 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 32 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 56 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 43 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 33 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 38 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 33 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 47 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 32 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 50 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 68 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 38 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 70 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 41 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 49 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 28 elements: only
## the first used

## Warning in (inicio + 1):(fin - 1): numerical expression has 28 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 68 elements: only
## the first used

## Warning in (inicio + 1):(fin - 1): numerical expression has 68 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 8 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 46 elements: only
## the first used
## Warning in (inicio + 1):(fin - 1): numerical expression has 35 elements: only
## the first used
## [[1]]
## NULL
## 
## [[2]]
## NULL
## 
## [[3]]
## NULL
## 
## [[4]]
## NULL
## 
## [[5]]
## NULL
## 
## [[6]]
## NULL
## 
## [[7]]
## NULL
## 
## [[8]]
## NULL
## 
## [[9]]
## NULL
## 
## [[10]]
## NULL
## 
## [[11]]
## NULL
## 
## [[12]]
## NULL
## 
## [[13]]
## NULL
## 
## [[14]]
## NULL
## 
## [[15]]
## NULL
## 
## [[16]]
## NULL
## 
## [[17]]
## NULL
## 
## [[18]]
## NULL
## 
## [[19]]
## NULL
## 
## [[20]]
## NULL
## 
## [[21]]
## NULL
## 
## [[22]]
## NULL
## 
## [[23]]
## NULL
## 
## [[24]]
## NULL
## 
## [[25]]
## NULL
## 
## [[26]]
## NULL
## 
## [[27]]
## NULL
## 
## [[28]]
## NULL
## 
## [[29]]
## NULL
## 
## [[30]]
## NULL
## 
## [[31]]
## NULL
## 
## [[32]]
## NULL
## 
## [[33]]
## NULL
## 
## [[34]]
## NULL
## 
## [[35]]
## NULL
## 
## [[36]]
## NULL
## 
## [[37]]
## NULL
## 
## [[38]]
## NULL
## 
## [[39]]
## NULL
## 
## [[40]]
## NULL
## 
## [[41]]
## NULL
## 
## [[42]]
## NULL
## 
## [[43]]
## NULL
## 
## [[44]]
## NULL
## 
## [[45]]
## NULL
## 
## [[46]]
## NULL
## 
## [[47]]
## NULL
## 
## [[48]]
## NULL
## 
## [[49]]
## NULL
## 
## [[50]]
## NULL
## 
## [[51]]
## NULL
## 
## [[52]]
## NULL
## 
## [[53]]
## NULL
## 
## [[54]]
## NULL
## 
## [[55]]
## NULL
## 
## [[56]]
## NULL
## 
## [[57]]
## NULL
## 
## [[58]]
## NULL
## 
## [[59]]
## NULL
## 
## [[60]]
## NULL
## 
## [[61]]
## NULL
## 
## [[62]]
## NULL
## 
## [[63]]
## NULL
## 
## [[64]]
## NULL
## 
## [[65]]
## NULL
## 
## [[66]]
## NULL
## 
## [[67]]
## NULL
## 
## [[68]]
## NULL
## 
## [[69]]
## NULL
## 
## [[70]]
## NULL
## 
## [[71]]
## NULL
## 
## [[72]]
## NULL
## 
## [[73]]
## NULL
## 
## [[74]]
## NULL
## 
## [[75]]
## NULL
## 
## [[76]]
## NULL
## 
## [[77]]
## NULL
## 
## [[78]]
## NULL
## 
## [[79]]
## NULL
## 
## [[80]]
## NULL
## 
## [[81]]
## NULL
## 
## [[82]]
## NULL
## 
## [[83]]
## NULL
## 
## [[84]]
## NULL
## 
## [[85]]
## NULL
## 
## [[86]]
## NULL
## 
## [[87]]
## NULL
## 
## [[88]]
## NULL
## 
## [[89]]
## NULL
## 
## [[90]]
## NULL
## 
## [[91]]
## NULL
## 
## [[92]]
## NULL
## 
## [[93]]
## NULL
## 
## [[94]]
## NULL
## 
## [[95]]
## NULL
## 
## [[96]]
## NULL
## 
## [[97]]
## NULL
## 
## [[98]]
## NULL
## 
## [[99]]
## NULL
## 
## [[100]]
## NULL
## 
## [[101]]
## NULL
## 
## [[102]]
## NULL
## 
## [[103]]
## NULL
## 
## [[104]]
## NULL
## 
## [[105]]
## NULL
## 
## [[106]]
## NULL
## 
## [[107]]
## NULL
## 
## [[108]]
## NULL
## 
## [[109]]
## NULL
## 
## [[110]]
## NULL
## 
## [[111]]
## NULL
## 
## [[112]]
## NULL
## 
## [[113]]
## NULL
## 
## [[114]]
## NULL
## 
## [[115]]
## NULL
## 
## [[116]]
## NULL
## 
## [[117]]
## NULL
## 
## [[118]]
## NULL
## 
## [[119]]
## NULL
## 
## [[120]]
## NULL
## 
## [[121]]
## NULL
## 
## [[122]]
## NULL
## 
## [[123]]
## NULL
## 
## [[124]]
## NULL
## 
## [[125]]
## NULL
## 
## [[126]]
## NULL
## 
## [[127]]
## NULL
## 
## [[128]]
## NULL
## 
## [[129]]
## NULL
## 
## [[130]]
## NULL
## 
## [[131]]
## NULL
## 
## [[132]]
## NULL
## 
## [[133]]
## NULL
## 
## [[134]]
## NULL
## 
## [[135]]
## NULL
## 
## [[136]]
## NULL
## 
## [[137]]
## NULL
## 
## [[138]]
## NULL
## 
## [[139]]
## NULL
## 
## [[140]]
## NULL
## 
## [[141]]
## NULL
## 
## [[142]]
## NULL
## 
## [[143]]
## NULL
## 
## [[144]]
## NULL

Hacer que todos los archivos sean un solo .txt

# Ruta donde se almacenará el archivo combinado
archivo_combinado <- "archivo_combinado.txt"

# Función para combinar contenido de archivos
combinar_archivos <- function(archivos, archivo_salida) {
  # Inicializar un vector para almacenar el contenido combinado
  contenido_combinado <- character(0)
  
  # Iterar sobre los archivos y combinar su contenido
  for (archivo in archivos) {
    contenido <- readLines(archivo, warn = FALSE)
    contenido_combinado <- c(contenido_combinado, contenido)
  }
  
  # Escribir el contenido combinado en el archivo de salida
  writeLines(contenido_combinado, con = archivo_salida)
}

# Llamar a la función para combinar los archivos
combinar_archivos(archivos, archivo_combinado)

# Mensaje de confirmación
cat("Los archivos se han combinado en", archivo_combinado, "\n")
## Los archivos se han combinado en archivo_combinado.txt

Limpieza de archivos

# Ruta donde se almacenará el archivo combinado
archivo_combinado <- "archivo_combinado.txt"

# Función para combinar archivos y eliminar líneas con "(INICIA VIDEO)" y "(FINALIZA VIDEO)"
combinar_archivos <- function(archivos, archivo_salida) {
  # Inicializar un vector para almacenar el contenido combinado
  contenido_combinado <- character(0)
  
  # Iterar sobre los archivos y combinar su contenido
  for (archivo in archivos) {
    contenido <- readLines(archivo, warn = FALSE)
    
    # Eliminar líneas que contienen "(INICIA VIDEO)" y "(FINALIZA VIDEO)"
    contenido_limpio <- contenido[!grepl("\\(INICIA VIDEO\\)|\\(FINALIZA VIDEO\\)", contenido)]
    
    contenido_combinado <- c(contenido_combinado, contenido_limpio)
  }
  
  # Escribir el contenido combinado en el archivo de salida
  writeLines(contenido_combinado, con = archivo_salida)
}

# Llamar a la función para combinar los archivos
combinar_archivos(archivos, archivo_combinado)

# Mensaje de confirmación
cat("Los archivos se han combinado y las líneas con (INICIA VIDEO) y (FINALIZA VIDEO) se han eliminado en", archivo_combinado, "\n")
## Los archivos se han combinado y las líneas con (INICIA VIDEO) y (FINALIZA VIDEO) se han eliminado en archivo_combinado.txt

Limpieza de texto: minúsculas y eliminar signos de puntuación

# Ruta donde se almacenará el archivo combinado
archivo_combinado <- "archivo_combinado.txt"

# Función para combinar archivos, eliminar líneas con "(INICIA VIDEO)" y "(FINALIZA VIDEO)",
# y realizar la transformación a minúsculas y eliminación de signos de puntuación
combinar_archivos <- function(archivos, archivo_salida) {
  # Inicializar un vector para almacenar el contenido combinado
  contenido_combinado <- character(0)
  
  # Definir patrón de signos de puntuación a eliminar
  patron_puntuacion <- "[[:punct:]]+"
  
  # Iterar sobre los archivos y combinar su contenido
  for (archivo in archivos) {
    contenido <- readLines(archivo, warn = FALSE)
    
    # Eliminar líneas que contienen "(INICIA VIDEO)" y "(FINALIZA VIDEO)"
    contenido_limpio <- contenido[!grepl("\\(INICIA VIDEO\\)|\\(FINALIZA VIDEO\\)", contenido)]
    
    # Transformar a minúsculas y eliminar signos de puntuación
    contenido_limpio <- tolower(contenido_limpio)
    contenido_limpio <- gsub(patron_puntuacion, "", contenido_limpio)
    
    contenido_combinado <- c(contenido_combinado, contenido_limpio)
  }
  
  # Escribir el contenido combinado en el archivo de salida
  writeLines(contenido_combinado, con = archivo_salida)
}

# Llamar a la función para combinar los archivos
combinar_archivos(archivos, archivo_combinado)

# Mensaje de confirmación
cat("Los archivos se han combinado, las líneas con (INICIA VIDEO) y (FINALIZA VIDEO) se han eliminado,", 
    "y se ha realizado la transformación a minúsculas y eliminación de signos de puntuación en", archivo_combinado, "\n")
## Los archivos se han combinado, las líneas con (INICIA VIDEO) y (FINALIZA VIDEO) se han eliminado, y se ha realizado la transformación a minúsculas y eliminación de signos de puntuación en archivo_combinado.txt

Eliminar superlativos y preoposiciones

# Ruta al archivo .txt que deseas procesar
ruta_archivo <- "archivo_combinado.txt"

# Leer el contenido del archivo
texto <- tolower(readLines(ruta_archivo, warn = FALSE, encoding = "UTF-8"))

# Lista de superlativos en español (puedes personalizarla según tus necesidades)
superlativos <- c("mejor", "peor", "mayor", "menor", "más", "menos", "tanto", "tan", "al", "las", "el", "o", "y", "que" )

# Lista de preposiciones en español (puedes personalizarla según tus necesidades)
preposiciones <- c("a", "ante", "bajo", "con", "de", "desde", "en", "entre", "hacia", "hasta", "para", "por", "sin", "sobre", "tras")

# Función para eliminar superlativos, saltos de línea y preposiciones
limpiar_texto <- function(texto, superlativos, preposiciones) {
  # Eliminar saltos de línea
  texto_limpio <- gsub("\\n", " ", texto)
  
  # Tokenizar el texto
  palabras <- unlist(strsplit(texto_limpio, " "))
  
  # Eliminar superlativos y preposiciones
  palabras_filtradas <- palabras[!(palabras %in% superlativos | palabras %in% preposiciones)]
  
  # Reconstruir el texto limpio
  texto_final <- paste(palabras_filtradas, collapse = " ")
  
  return(texto_final)
}

# Llamar a la función para limpiar el texto
texto_limpiado <- limpiar_texto(texto, superlativos, preposiciones)

# Guardar el texto limpiado en un nuevo archivo
writeLines(texto_limpiado, con = "texto_limpiado.txt", useBytes = TRUE)

# Mensaje de confirmación
cat("El texto ha sido limpiado y se ha guardado en texto_limpiado.txt\n")
## El texto ha sido limpiado y se ha guardado en texto_limpiado.txt
# Cargar la biblioteca tm
library(tm)
## Loading required package: NLP
# Ruta al archivo .txt que deseas procesar
ruta_archivo <- "texto_limpiado.txt"

# Leer el contenido del archivo
texto2 <- tolower(readLines(ruta_archivo, warn = FALSE, encoding = "UTF-8"))

# Crear un corpus a partir del texto
corpus <- Corpus(VectorSource(texto2))

# Definir una lista de stopwords en español
stopwords_es <- stopwords("es")

# Eliminar las stopwords del corpus
corpus_sin_stopwords <- tm_map(corpus, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(corpus, content_transformer(tolower)):
## transformation drops documents
corpus_sin_stopwords <- tm_map(corpus_sin_stopwords, removePunctuation)
## Warning in tm_map.SimpleCorpus(corpus_sin_stopwords, removePunctuation):
## transformation drops documents
corpus_sin_stopwords <- tm_map(corpus_sin_stopwords, removeWords, stopwords_es)
## Warning in tm_map.SimpleCorpus(corpus_sin_stopwords, removeWords,
## stopwords_es): transformation drops documents
# Obtener el texto limpio
texto_limpiado <- unlist(sapply(corpus_sin_stopwords, as.character))

# Guardar el texto limpiado en un nuevo archivo
writeLines(texto_limpiado, con = "texto_limpiado.txt", useBytes = TRUE)

# Mensaje de confirmación
cat("El texto ha sido limpiado de stopwords y se ha guardado en texto_limpiado.txt\n")
## El texto ha sido limpiado de stopwords y se ha guardado en texto_limpiado.txt

Repetir el ánalisis

#Nuevo DTM
dtm <- DocumentTermMatrix(corpus)

#Ver la matriz
inspect(dtm)
## <<DocumentTermMatrix (documents: 1, terms: 49381)>>
## Non-/sparse entries: 49381/0
## Sparsity           : 0%
## Maximal term length: 738
## Weighting          : term frequency (tf)
## Sample             :
##     Terms
## Docs como   del  hay lópez   los pero porque presidente también   una
##    1 8361 18688 7328  7004 27956 9794   9555      12586    7562 13141
#Sumar las columnas para obtener el conteo total de cada término 
conteo_total <- colSums(as.matrix(dtm)) 

#Ordenar y mostrar los términos más comunes 
terminos_comunes <- sort(conteo_total, decreasing = TRUE)
head(terminos_comunes, 100) #Mostrar los 50 términos más comunes
##         los         del         una  presidente        pero      porque 
##       27956       18688       13141       12586        9794        9555 
##        como     también         hay       lópez        está         eso 
##        8361        7562        7328        7004        6892        6819 
##        este      manuel      andrés         mil    entonces         muy 
##        6734        6643        6570        5935        5801        5689 
##          sí        todo         son      méxico       vamos        esto 
##        5574        5201        5076        5062        4892        4637 
##         qué         nos        esta     obrador       tiene        pues 
##        4536        4400        4388        4338        4323        4132 
##        aquí       todos       están         fue         ver       bueno 
##        4094        3989        3968        3464        3443        3407 
##         les         ahí    gobierno     tenemos     estamos       ahora 
##        3348        3321        3315        3248        3230        3058 
##         dos      cuando     estados      estado        nada    millones 
##        3021        2992        2937        2922        2919        2918 
##         ese        cómo         era       mucho       estos       pesos 
##        2905        2624        2595        2587        2581        2558 
##         han    nosotros        caso         así      tienen       donde 
##        2524        2515        2513        2507        2423        2376 
##      ciento      pueblo         esa        país         sea       gente 
##        2327        2319        2289        2284        2281        2266 
##    nacional       señor         sus         van       ellos      unidos 
##        2225        2202        2202        2183        2179        2157 
##         ser       usted       hacer        tres        años         año 
##        2124        2074        2073        2009        1991        1941 
##        días        bien       había        hace   seguridad       poder 
##        1906        1868        1866        1823        1812        1792 
##       parte       puede  importante        tema       decir         hoy 
##        1760        1699        1650        1647        1598        1590 
##       mismo      además        tren      manera      tiempo información 
##        1582        1573        1570        1563        1554        1537 
##    pregunta       estas    personas          él 
##        1530        1514        1510        1487
#Instalar y cargar el paquete worldCloud 
#install.packages("worldcloud")
library(wordcloud) #Nube de palabras como la de menti
## Loading required package: RColorBrewer
#Crear la nube de palabras 
wordcloud (names(terminos_comunes), terminos_comunes, min.freq = 1, max.words = 100, random.order = FALSE)

palabras_eliminar <- c("null", "presidente", "lópez", "andrés", "manuel", "gtagset", "function", "var", "pagetype", "interlocutor", "obrador", "entonces", "pues", "...", "aquí", "ahí", "else","lang", "gagobtrackerset", "subgroup", "pagesgroup", "—", "estenográfica", "‘", "—", "…", "organizationname", "interlocutora", "cómo")
corpus <- tm_map(corpus, removeWords, palabras_eliminar)
## Warning in tm_map.SimpleCorpus(corpus, removeWords, palabras_eliminar):
## transformation drops documents
dtm  <- DocumentTermMatrix(corpus)

inspect(dtm)
## <<DocumentTermMatrix (documents: 1, terms: 47791)>>
## Non-/sparse entries: 47791/0
## Sparsity           : 0%
## Maximal term length: 738
## Weighting          : term frequency (tf)
## Sample             :
##     Terms
## Docs como está este esto méxico pero porque también todo vamos
##    1 8331 6860 6715 4619   5060 9768   9540    7532 5195  4879
conteo_total <- colSums(as.matrix(dtm))

terminos_comunes <- sort(conteo_total, decreasing = TRUE)
head(terminos_comunes, 100)
##        pero      porque        como     también        está        este 
##        9768        9540        8331        7532        6860        6715 
##        todo      méxico       vamos        esto        esta       tiene 
##        5195        5060        4879        4619        4383        4305 
##       todos       están       bueno    gobierno     tenemos     estamos 
##        3978        3967        3397        3304        3234        3228 
##       ahora      cuando     estados    millones        nada      estado 
##        3037        2985        2935        2917        2916        2914 
##       mucho       estos       pesos    nosotros        caso      tienen 
##        2586        2577        2553        2512        2502        2421 
##       donde      ciento      pueblo        país       gente    nacional 
##        2367        2321        2318        2277        2264        2209 
##       señor       ellos      unidos       hacer       usted        tres 
##        2201        2174        2154        2072        2067        2001 
##        años        días        bien       había        hace   seguridad 
##        1978        1904        1867        1861        1820        1811 
##       poder       parte       puede  importante        tema    pregunta 
##        1792        1743        1696        1645        1644        1640 
##       decir       mismo      además      manera        tren      tiempo 
##        1593        1578        1567        1557        1557        1553 
## información       estas    personas      contra     general       todas 
##        1525        1512        1507        1481        1476        1440 
##       antes       luego     gracias       tener       estar       hemos 
##        1430        1402        1397        1393        1387        1385 
##        otro        sólo       otros    haciendo      ciudad     nuestro 
##        1378        1358        1337        1304        1263        1253 
##       salud     trabajo  secretaría      buenos      garcía        maya 
##        1229        1205        1198        1176        1174        1162 
##      cuatro      dinero      medios      estaba     siempre     ejemplo 
##        1160        1158        1158        1151        1151        1141 
##        creo     pública        ayer        toda        otra      muchas 
##        1122        1119        1108        1104        1103        1099 
##      social      fueron     ustedes      muchos 
##        1095        1056        1037        1015
wordcloud(names(terminos_comunes), terminos_comunes, min.freq = 1, max.words = 100, random.order = FALSE)

#Análisis La segunda palabra más mencionada es “México”, lo cual tiene mucho sentido, ya que el presidente debe expresarse sobre temas del país que rige. Relacionado con esto, la siguiente palabra más mencionada es “Gobierno”. Una palabra interesante que se ha utilizado 702 veces en la mañanera es la palabra “bueno”, si bien no parece tener gran importancia por sí sola, al ver que otra de las palabras más usadas es “pueblo” se puede entender que muchas veces se usan estas dos palabras juntas, “pueblo bueno”, la cual es una frase con un fin populista que el presidente ha usado desde el inicio de su candidatura a la presidencia. Claramente la palabra “bueno” es mucho más utilizada que “pueblo”, esto se debe a que “bueno” puede ser usado en muchas ocasiones, incluso como muletilla. Sin embargo, cabe mencionar que no necesariamente todas las veces que se dijó la palabra “pueblo” en las mañaneras, fue para decir “pueblo bueno”.

Dentro de las palabras más utilizadas se puede inferir cuales con los temas públicos más dialogados en las mañaneras, por ejemplo las palabras “seguridad”, “salud” y “agua”, siendo seguridad la más mencionada de las 3. La importancia hacia el tema de seguridad en las mañaneras puede deberse a la propuesta política de Andrés Manuel por incorporar la Guardia Nacional a la Secretaria de la Defensa Nacional, siendo una propuesta basada en la Seguridad.

Por último, un par de palabras a destacar dentro de las 100 más utilizadas son “tren” y “maya”. La relación entre ambas palabras es muy lógica, ya que es el proyecto tren maya, el cual es uno de los proyectos de infraestructura más grandes del sexenio de Andrés Manuel López Obrador.