
textclean is a collection of tools to clean and normalize text. Many
of these tools have been taken from the qdap package and revamped to
be more intuitive, better named, and faster. Tools are geared at
checking for substrings that are not optimal for analysis and replacing
or removing them (normalizing) with more analysis friendly substrings
(see Sproat, Black, Chen, Kumar, Ostendorf, & Richards, 2001,
doi:10.1006/csla.2001.0169)
or extracting them into new variables. For example, emoticons are often
used in text but not always easily handled by analysis algorithms. The
replace_emoticon() function replaces emoticons with word equivalents.
Other R packages provide some of the same functionality (e.g.,
english, gsubfn, mgsub, stringi, stringr,
qdapRegex). textclean differs from these packages in that it is
designed to handle all of the common cleaning and normalization tasks
with a single, consistent, pre-configured toolset (note that
textclean uses many of these terrific packages as a backend). This
means that the researcher spends less time on munging, leading to
quicker analysis. This package is meant to be used jointly with the
textshape package, which
provides text extraction and reshaping functionality. textclean
works well with the
qdapRegex package which
provides tooling for substring substitution and extraction of pre-canned
regular expressions. In addition, the functions of textclean are
designed to work within the piping of the tidyverse framework by
consistently using the first argument of functions as the data source.
The textclean subbing and replacement tools are particularly
effective within a dplyr::mutate statement.
Table of Contents
Functions
The main functions, task category, & descriptions are summarized in the
table below:
| Function |
Task |
Description |
mgsub |
subbing |
Multiple gsub |
fgsub |
subbing |
Functional matching replacement gsub |
sub_holder |
subbing |
Hold a value prior to a strip |
swap |
subbing |
Simultaneously swap patterns 1 & 2 |
strip |
deletion |
Remove all non word characters |
drop_empty_row |
filter rows |
Remove empty rows |
drop_row/keep_row |
filter rows |
Filter rows matching a regex |
drop_NA |
filter rows |
Remove NA text rows |
drop_element/keep_element |
filter elements |
Filter matching elements from a vector |
match_tokens |
filter elements |
Filter out tokens from strings that match a regex criteria |
replace_contractions |
replacement |
Replace contractions with both words |
replace_date |
replacement |
Replace dates |
replace_email |
replacement |
Replace emails |
replace_emoji |
replacement |
Replace emojis with word equivalent or unique identifier |
replace_emoticon |
replacement |
Replace emoticons with word equivalent |
replace_grade |
replacement |
Replace grades (e.g., “A+”) with word equivalent |
replace_hash |
replacement |
Replace Twitter style hash tags (e.g., #rstats) |
replace_html |
replacement |
Replace HTML tags and symbols |
replace_incomplete |
replacement |
Replace incomplete sentence end-marks |
replace_internet_slang |
replacement |
Replace Internet slang with word equivalents |
replace_kern |
replacement |
Replace spaces for >2 letter, all cap, words containing spaces in between letters |
replace_misspelling |
replacement |
Replace misspelled words with their most likely replacement |
replace_money |
replacement |
Replace money in the form of $\d+.?\d{0,2} |
replace_names |
replacement |
Replace common first/last names |
replace_non_ascii |
replacement |
Replace non-ASCII with equivalent or remove |
replace_number |
replacement |
Replace common numbers |
replace_ordinal |
replacement |
Replace common ordinal number form |
replace_rating |
replacement |
Replace ratings (e.g., “10 out of 10”, “3 stars”) with word equivalent |
replace_symbol |
replacement |
Replace common symbols |
replace_tag |
replacement |
Replace Twitter style handle tag (e.g., @trinker) |
replace_time |
replacement |
Replace time stamps |
replace_to/replace_from |
replacement |
Remove from/to begin/end of string to/from a character(s) |
replace_tokens |
replacement |
Remove or replace a vector of tokens with a single value |
replace_url |
replacement |
Replace URLs |
replace_white |
replacement |
Replace regex white space characters |
replace_word_elongation |
replacement |
Replace word elongations with shortened form |
add_comma_space |
replacement |
Replace non-space after comma |
add_missing_endmark |
replacement |
Replace missing endmarks with desired symbol |
make_plural |
replacement |
Add plural endings to singular noun forms |
check_text |
check |
Text report of potential issues |
has_endmark |
check |
Check if an element has an end-mark |
Installation
To download the development version of textclean:
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/lexicon",
"trinker/textclean"
)
Contact
You are welcome to:
Contributing
Contributions are welcome from anyone subject to the following rules:
- Abide by the Code of Conduct.
- Follow the style conventions of the package (indentation, function &
argument naming, commenting, etc.)
- All contributions must be consistent with the package license
(GPL-2)
- Submit contributions as a pull request. Clearly state what the
changes are and try to keep the number of changes per pull request
as low as possible.
- If you make big changes, add your name to the ‘Author’ field.
Demonstration
Load the Packages/Data
library(dplyr)
library(textshape)
library(lexicon)
library(textclean)
Check Text
One of the most useful tools in textclean is check_text which
scans text variables and reports potential problems. Not all potential
problems are definite problems for analysis but the report provides an
overview of what may need further preparation. The report also provides
suggested functions for the reported problems. The report provides
information on the following:
- contraction - Text elements that contain contractions
- date - Text elements that contain dates
- digit - Text elements that contain digits/numbers
- email - Text elements that contain email addresses
- emoticon - Text elements that contain emoticons
- empty - Text elements that contain empty text cells (all white
space)
- escaped - Text elements that contain escaped back spaced
characters
- hash - Text elements that contain Twitter style hash tags (e.g.,
#rstats)
- html - Text elements that contain HTML markup
- incomplete - Text elements that contain incomplete sentences
(e.g., uses ending punctuation like ‘…’)
- kern - Text elements that contain kerning (e.g., ‘The B O M B!’)
- list_column - Text variable that is a list column
- missing_value - Text elements that contain missing values
- misspelled - Text elements that contain potentially misspelled
words
- no_alpha - Text elements that contain elements with no
alphabetic (a-z) letters
- no_endmark - Text elements that contain elements with missing
ending punctuation
- no_space_after_comma - Text elements that contain commas with
no space afterwards
- non_ascii - Text elements that contain non-ASCII text
- non_character - Text variable that is not a character column
(likely
factor)
- non_split_sentence - Text elements that contain unsplit
sentences (more than one sentence per element)
- tag - Text elements that contain Twitter style handle tags
(e.g., @trinker)
- time - Text elements that contain timestamps
- url - Text elements that contain URLs
Note that check_text is running multiple checks and may be slower on
larger texts. The user may provide a sample of text for review or use
the checks argument to specify the exact checks to conduct and thus
limit the compute time.
Here is an example: