-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tidy_dtm & tidy_colo_dtm close #3
- Loading branch information
Showing
15 changed files
with
447 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#' Convert a \code{\link[tm]{DocumentTermMatrix}}/\code{\link[tm]{TermDocumentMatrix}} into Collocating Words in Tidy Form | ||
#' | ||
#' Converts non-zero elements of a | ||
#' \code{\link[tm]{DocumentTermMatrix}}/\code{\link[tm]{TermDocumentMatrix}} into | ||
#' a tidy data set made of collocating words. | ||
#' | ||
#' @param x A \code{\link[tm]{DocumentTermMatrix}}/\code{\link[tm]{TermDocumentMatrix}}. | ||
#' @param \ldots ignored. | ||
#' @return Returns a tidied data.frame. | ||
#' @rdname tidy_colo_dtm | ||
#' @export | ||
#' @seealso \code{\link[textshape]{unique_pairs}} | ||
#' @examples | ||
#' tidied <- tidy_colo_dtm(simple_dtm) | ||
#' tidied | ||
#' unique_pairs(tidied) | ||
#' | ||
#' \dontrun{ | ||
#' if (!require("pacman")) install.packages("pacman") | ||
#' pacman::p_load_current_gh('trinker/gofastr', 'trinker/lexicon') | ||
#' pacman::p_load(tidyverse, magrittr, ggstance) | ||
#' | ||
#' my_dtm <- with(presidential_debates_2012, q_dtm(dialogue, paste(time, tot, sep = "_"))) | ||
#' | ||
#' tidy_colo_dtm(my_dtm) %>% | ||
#' tbl_df() %>% | ||
#' filter(!term_1 %in% c('i', lexicon::sw_onix) & !term_2 %in% lexicon::sw_onix) %>% | ||
#' filter(term_1 != term_2) %>% | ||
#' unique_pairs() %>% | ||
#' filter(n > 15) %>% | ||
#' complete(term_1, term_2, fill = list(n = 0)) %>% | ||
#' ggplot(aes(x = term_1, y = term_2, fill = n)) + | ||
#' geom_tile() + | ||
#' scale_fill_gradient(low= 'white', high = 'red') + | ||
#' theme(axis.text.x = element_text(angle = 45, hjust = 1)) | ||
#' } | ||
tidy_colo_tdm <- function(x, ...){ | ||
|
||
term_1 <- NULL | ||
|
||
x <- slam::as.simple_triplet_matrix(slam::tcrossprod_simple_triplet_matrix(x, y = NULL)) | ||
|
||
data.table::data.table( | ||
term_1 = x[['dimnames']][['Terms']][x[['i']]], | ||
term_2 = x[['dimnames']][['Terms']][x[['j']]], | ||
n = x[['v']] | ||
)[order(term_1), ] | ||
} | ||
|
||
|
||
|
||
#' @rdname tidy_colo_dtm | ||
#' @export | ||
tidy_colo_dtm <- function(x, ...){ | ||
|
||
term_1 <- NULL | ||
|
||
x <- slam::as.simple_triplet_matrix(slam::crossprod_simple_triplet_matrix(x, y = NULL)) | ||
|
||
data.table::data.table( | ||
term_1 = x[['dimnames']][['Terms']][x[['i']]], | ||
term_2 = x[['dimnames']][['Terms']][x[['j']]], | ||
n = x[['v']] | ||
)[order(term_1), ] | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#' Convert a \code{\link[tm]{DocumentTermMatrix}}/\code{\link[tm]{TermDocumentMatrix}} | ||
#' into Tidy Form | ||
#' | ||
#' Converts non-zero elements of a | ||
#' \code{\link[tm]{DocumentTermMatrix}}/\code{\link[tm]{TermDocumentMatrix}} into | ||
#' a tidy data set. | ||
#' | ||
#' | ||
#' @param x A \code{\link[tm]{DocumentTermMatrix}}/\code{\link[tm]{TermDocumentMatrix}}. | ||
#' @param \ldots ignored. | ||
#' @return Returns a tidied data.frame. | ||
#' @rdname tidy_dtm | ||
#' @export | ||
#' @examples | ||
#' data(simple_dtm) | ||
#' | ||
#' tidy_dtm(simple_dtm) | ||
#' | ||
#' \dontrun{ | ||
#' if (!require("pacman")) install.packages("pacman") | ||
#' pacman::p_load_current_gh('trinker/gofastr') | ||
#' pacman::p_load(tidyverse, magrittr, ggstance) | ||
#' | ||
#' my_dtm <- with(presidential_debates_2012, q_dtm(dialogue, paste(time, tot, sep = "_"))) | ||
#' | ||
#' tidy_dtm(my_dtm) %>% | ||
#' tidyr::extract(doc, c("time", "turn", "sentence"), "(\\d)_(\\d+)\\.(\\d+)") %>% | ||
#' mutate( | ||
#' time = as.numeric(time), | ||
#' turn = as.numeric(turn), | ||
#' sentence = as.numeric(sentence) | ||
#' ) %>% | ||
#' tbl_df() %T>% | ||
#' print() %>% | ||
#' group_by(time, term) %>% | ||
#' summarize(n = sum(n)) %>% | ||
#' group_by(time) %>% | ||
#' arrange(desc(n)) %>% | ||
#' slice(1:10) %>% | ||
#' mutate( | ||
#' term = factor(paste(term, time, sep = "__"), | ||
#' levels = rev(paste(term, time, sep = "__"))) | ||
#' ) %>% | ||
#' ggplot(aes(x = n, y = term)) + | ||
#' geom_barh(stat='identity') + | ||
#' facet_wrap(~time, ncol=2, scales = 'free_y') + | ||
#' scale_y_discrete(labels = function(x) gsub("__.+$", "", x)) | ||
#' } | ||
tidy_dtm <- function(x, ...){ | ||
|
||
doc <- NULL | ||
|
||
data.table::data.table( | ||
doc = x[['dimnames']][['Docs']][x[['i']]], | ||
term = x[['dimnames']][['Terms']][x[['j']]], | ||
n = x[['v']], | ||
i = x[['i']], | ||
j = x[['j']] | ||
)[order(doc), ] | ||
} | ||
|
||
|
||
|
||
#' @rdname tidy_dtm | ||
#' @export | ||
tidy_tdm <- function(x, ...){ | ||
|
||
doc <- NULL | ||
|
||
out <- data.table::data.table( | ||
doc = x[['dimnames']][['Docs']][x[['j']]], | ||
term = x[['dimnames']][['Terms']][x[['i']]], | ||
n = x[['v']], | ||
i = x[['j']], | ||
j = x[['i']] | ||
)[order(doc), ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#' Extract Only Unique Pairs of Collocating Words in \code{\link[textshape]{tidy_colo_dtm}} | ||
#' | ||
#' \code{\link[textshape]{tidy_colo_dtm}} utilizes the entire matrix to generate | ||
#' the tidied data.frame. This means that the upper and lower triangles are | ||
#' used redundantly. This function eliminates this redundancy by dropping one | ||
#' set of the pairs from a tidied data.frame. | ||
#' | ||
#' @param x A \code{\link[base]{data.frame}} with two columns that contain | ||
#' redundant pairs. | ||
#' @param col1 A string naming column 1. | ||
#' @param col2 A string naming column 2. | ||
#' @param \ldots ignored. | ||
#' @return Returns a filtered \code{\link[base]{data.frame}}. | ||
#' @export | ||
#' @seealso \code{\link[textshape]{tidy_colo_dtm}} | ||
#' @examples | ||
#' dat <- data.frame( | ||
#' term_1 = LETTERS[1:10], | ||
#' term_2 = LETTERS[10:1], | ||
#' stringsAsFactors = FALSE | ||
#' ) | ||
#' | ||
#' unique_pairs(dat) | ||
unique_pairs <- function(x, col1 = 'term_1', col2 = 'term_2', ...) { | ||
|
||
UseMethod('unique_pairs') | ||
} | ||
|
||
#' @export | ||
#' @rdname unique_pairs | ||
#' @method unique_pairs default | ||
unique_pairs.default <- function(x, col1 = 'term_1', col2 = 'term_2', ...) { | ||
|
||
x[!duplicated(apply(data.table::data.table(x[, c(col1, col2)]), 1, sorter)),] | ||
} | ||
|
||
#' @export | ||
#' @rdname unique_pairs | ||
#' @method unique_pairs data.table | ||
unique_pairs.data.table <- function(x, col1 = 'term_1', col2 = 'term_2', ...) { | ||
|
||
x[!duplicated(apply(data.table::data.table(x[, c(col1, col2), with = FALSE]), 1, sorter)),] | ||
} | ||
|
||
sorter <- function(x) paste(sort(x), collapse = "") | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.