## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## -----------------------------------------------------------------------------
library(textclassificationtutorial)

## -----------------------------------------------------------------------------
html_file <- system.file(
  "extdata", "sample_nursing_vacancy.html",
  package = "textclassificationtutorial"
)
vacancy_text <- extract_html_text(html_file)
substr(vacancy_text, 1, 200)

## ----eval = FALSE-------------------------------------------------------------
# pages <- extract_html_dir("inst/extdata/vacancypages")

## ----eval = FALSE-------------------------------------------------------------
# extract_html_text(html_file, selector = "div.content")
# extract_html_text(html_file, xpath = "//div[@class='content']")

## -----------------------------------------------------------------------------
sentences <- split_sentences(vacancy_text)
head(sentences)

## -----------------------------------------------------------------------------
german_stopwords <- c(
  "der", "die", "das", "den", "dem", "des", "und", "oder", "mit",
  "für", "von", "zu", "im", "in", "auf", "ein", "eine"
)

clean <- preprocess_text(
  sentences,
  lowercase = TRUE,
  remove_punctuation = TRUE,
  remove_numbers = TRUE,
  stopwords = german_stopwords,
  min_token_length = 2
)
clean <- clean[nzchar(clean)]
head(clean)

## -----------------------------------------------------------------------------
dtm <- document_term_matrix(
  clean,
  min_doc_freq = 2,
  max_doc_prop = 0.95
)
dtm

## -----------------------------------------------------------------------------
weighted <- tf_idf(dtm)
keywords <- extract_keywords(dtm, n = 3)
head(keywords, 12)

## -----------------------------------------------------------------------------
similarity <- cosine_similarity(weighted)
round(similarity[1:min(5, nrow(similarity)),
                 1:min(5, ncol(similarity))], 2)

## -----------------------------------------------------------------------------
training_text <- c(
  "analyze data statistical model",
  "build predictive model data",
  "create dashboard analyze metrics",
  "provide nursing care patient",
  "support patient clinical care",
  "coordinate nurse patient treatment"
)
training_labels <- c("data", "data", "data", "care", "care", "care")

training_dtm <- document_term_matrix(training_text)
model <- fit_naive_bayes(training_dtm, training_labels, laplace = 1)
model

predicted <- predict(model, training_dtm)
classification_metrics(training_labels, predicted, positive = "data")

