termco

termco is a suite of functions used to count and find terms and
substrings in strings. The tools can be used to build an expert rules,
regular expression based text classification model. The package wraps
the data.table and
stringi packages to
create fast data frame counts of regular expression terms and
substrings.
Table of Contents
Functions
The main function of termco is term_count. It is used to extract
regex term counts by grouping variable(s) as well as to generate
classification models.
Most of the functions count, search, plot terms, and covert
between output types, while a few remaining functions are used to train,
test and interpret models. Additionally, the probe_ family of
function generate lists of function calls or plots for given search
terms. The table below describes the functions, category of use, and
their description:
| Function |
Use Category |
Description |
term_count |
count |
Count regex term occurrence; modeling |
token_count |
count |
Count fixed token occurrence; modeling |
frequent_terms/all_words |
count |
Frequent terms |
important_terms |
count |
Important terms |
hierarchical_coverage_term |
count |
Unique coverage of a text vector by terms |
hierarchical_coverage_regex |
count |
Unique coverage of a text vector by regex |
frequent_ngrams |
count |
Weighted frequent ngram (2 & 3) collocations |
word_count |
count |
Count words |
term_before/term_after |
count |
Frequency of words before/after a regex term |
term_first |
count |
Frequency of words at the begining of strings |
colo |
search |
Regex output to find term collocations |
search_term |
search |
Search for regex terms |
match_word |
search |
Extract words from a text matching a regular expression |
search_term_collocations |
search |
Wrapper for search_term + frequent_terms |
classification_project |
modeling |
Make a classification modeling project template |
classification_template |
modeling |
Make a classification analysis script template |
as_dtm/as_tdm |
modeling |
Coerce term_count object into tm::DocumentTermMatrix/tm::TermDocumentMatrix |
split_data |
modeling |
Split data into train & test sets |
evaluate |
modeling |
Check accuracy of model against human coder |
classify |
modeling |
Assign n tags to text from a model |
get_text |
modeling |
Get the original text for model tags |
coverage |
modeling |
Coverage for term_count or search_term object |
uncovered/get_uncovered |
modeling |
Get the uncovered text from a model |
mutate_counts |
modeling |
Apply normalizing function to term count columns |
select_counts |
modeling |
Select columns without stripping count classes |
tag_co_occurrence |
modeling |
Explore co-occurrence of tags from a model |
validate_model/assign_validation_task |
modeling |
Human validation of a term_count model |
read_term_list |
read/write |
Read a term list from an external file |
write_term_list |
read/write |
Write a term list to an external file |
term_list_template |
read/write |
Write a term list template to an external file |
as_count |
convert |
Strip pretty printing from term_count object |
as_terms |
convert |
Convert a count matrix to list of term vectors |
as_term_list |
convert |
Convert a vector of terms into a named term list |
weight |
convert |
Weight a term_count object proportion/percent |
plot_ca |
plot |
Plot term_count object as 3-D correspondence analysis map |
plot_counts |
plot |
Horizontal bar plot of group counts |
plot_freq |
plot |
Vertical bar plot of frequencies of counts |
plot_cum_percent |
plot |
Plot frequent_terms object as cumulative percent |
probe_list |
probe |
Generate list of search_term function calls |
probe_colo_list |
probe |
Generate list of search_term_collocations function calls |
probe_colo_plot_list |
probe |
Generate list of search_term_collocationss + plot function calls |
probe_colo_plot |
probe |
Plot probe_colo_plot_list directly |
Installation
To download the development version of termco:
Download the zip
ball or tar
ball, decompress and
run R CMD INSTALL on it, or use the pacman package to install the
development version:
if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh(
"trinker/gofastr",
"trinker/termco"
)
Contact
You are welcome to:
Examples
The following examples demonstrate some of the functionality of
termco.
Load the Tools/Data
if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr, ggplot2, termco)
data(presidential_debates_2012)
Build Counts Dataframe
discoure_markers <- list(
response_cries = c("\\boh", "\\bah", "aha", "ouch", "yuk"),
back_channels = c("uh[- ]huh", "uhuh", "yeah"),
summons = "hey",
justification = "because"
)
counts <- presidential_debates_2012 %>%
with(term_count(dialogue, grouping.var = list(person, time), discoure_markers))
counts
## Coverage: 100%
## # A tibble: 10 x 7
## person time n.words response_cries back_channels summons justification
## <fct> <fct> <int> <chr> <chr> <chr> <chr>
## 1 OBAMA time~ 3599 3(.08%) 0 43(1.1~ 26(.72%)
## 2 OBAMA time~ 7477 2(.03%) 0 42(.56~ 29(.39%)
## 3 OBAMA time~ 7243 1(.01%) 1(.01%) 58(.80~ 33(.46%)
## 4 ROMNEY time~ 4085 0 0 27(.66~ 8(.20%)
## 5 ROMNEY time~ 7536 1(.01%) 3(.04%) 49(.65~ 20(.27%)
## 6 ROMNEY time~ 8303 5(.06%) 0 84(1.0~ 19(.23%)
## 7 CROWLEY time~ 1672 2(.12%) 0 4(.24%) 12(.72%)
## 8 LEHRER time~ 765 3(.39%) 3(.39%) 0 0
## 9 QUESTI~ time~ 583 2(.34%) 0 0 2(.34%)
## 10 SCHIEF~ time~ 1445 0 0 2(.14%) 6(.42%)
Printing
print(counts, pretty = FALSE)
## Coverage: 100%
## # A tibble: 10 x 7
## person time n.words response_cries back_channels summons justification
## <fct> <fct> <int> <int> <int> <int> <int>
## 1 OBAMA time~ 3599 3 0 43 26
## 2 OBAMA time~ 7477 2 0 42 29
## 3 OBAMA time~ 7243 1 1 58 33
## 4 ROMNEY time~ 4085 0 0 27 8
## 5 ROMNEY time~ 7536 1 3 49 20
## 6 ROMNEY time~ 8303 5 0 84 19
## 7 CROWLEY time~ 1672 2 0 4 12
## 8 LEHRER time~ 765 3 3 0 0
## 9 QUESTI~ time~ 583 2 0 0 2
## 10 SCHIEF~ time~ 1445 0 0 2 6
print(counts, zero.replace = "_")
## Coverage: 100%
## # A tibble: 10 x 7
## person time n.words response_cries back_channels summons justification
## <fct> <fct> <int> <chr> <chr> <chr> <chr>
## 1 OBAMA time~ 3599 3(.08%) _ 43(1.1~ 26(.72%)
## 2 OBAMA time~ 7477 2(.03%) _ 42(.56~ 29(.39%)
## 3 OBAMA time~ 7243 1(.01%) 1(.01%) 58(.80~ 33(.46%)
## 4 ROMNEY time~ 4085 _ _ 27(.66~ 8(.20%)
## 5 ROMNEY time~ 7536 1(.01%) 3(.04%) 49(.65~ 20(.27%)
## 6 ROMNEY time~ 8303 5(.06%) _ 84(1.0~ 19(.23%)
## 7 CROWLEY time~ 1672 2(.12%) _ 4(.24%) 12(.72%)
## 8 LEHRER time~ 765 3(.39%) 3(.39%) _ _
## 9 QUESTI~ time~ 583 2(.34%) _ _ 2(.34%)
## 10 SCHIEF~ time~ 1445 _ _ 2(.14%) 6(.42%)
Plotting

plot(counts, labels=TRUE)


Ngram Collocations
termco wraps the quanteda
package to examine important ngram collocations. quanteda’s
collocation function provides measures of: "lambda", "z", and
"frequency" to examine the strength of relationship between ngrams.
termco adds stopword removal, min/max character filtering, and
stemming to quanteda’s collocation as well as a generic plot
method.