textcleaner: Text Cleaner

View source: R/textcleaner.R

textcleanerR Documentation

Text Cleaner

Description

An automated cleaning function for spell-checking, de-pluralizing, removing duplicates, and binarizing text data

Usage

textcleaner(
  data = NULL,
  type = c("fluency", "free"),
  miss = 99,
  partBY = c("row", "col"),
  dictionary = NULL,
  spelling = c("UK", "US"),
  add.path = NULL,
  keepStrings = FALSE,
  allowPunctuations,
  allowNumbers = FALSE,
  keepLength = NULL,
  keepCue = FALSE,
  keepRepeats = FALSE,
  continue = NULL,
  reader = readline,
  search_fn = searcher::search_site,
  cores = 1,
  edit_fn = automated_edit
)

Arguments

data

Matrix or data frame.

For type = "fluency", data are expected to follow wide formatting (IDs are the row names and are not a column in the matrix or data frame; alternatively, a column named "ID", "subject", "participant", "part" or "sub" is used as the IDs and removed from the responses; failing that, a single column whose values are all different and contain digits (e.g., 1, 2, 3 or P01, P02) is taken to be the IDs; with none of these, rows are numbered):

row.names Response 1 Response 2 Response n
ID_1 1 2 n
ID_2 1 2 n
ID_n 1 2 n

For type = "free", data are expected to follow long formatting:

ID Cue Response
1 1 1
1 1 2
1 1 n
1 2 1
1 2 2
1 2 n
1 n 1
1 n 2
1 n n
2 1 1
2 1 2
2 1 n
2 2 1
2 2 2
2 2 n
2 n 1
2 n 2
2 n n
n 1 1
n 1 2
n 1 n
n 2 1
n 2 2
n 2 n
n n 1
n n 2
n n n
type

Character vector. Type of task to be preprocessed.

  • "fluency" — Verbal fluency data (e.g., categories, phonological, synonyms)

  • "free" — Free association data (e.g., cue terms or words)

miss

Numeric or character. Value for missing data. Defaults to 99

partBY

Character. Are participants by row or column? Set to "row" for by row. Set to "col" for by column (the data are transposed first, so the column names become the participants' IDs, exactly as the row names are when participants are by row)

dictionary

Character vector. The name(s) of the dictionary (or dictionaries) to spell-check against, e.g., "animals", or a custom dictionary saved as <name>.dictionary.rds in the working directory or in add.path. It can also be a vector of the words themselves, such as a corpus or result$dictionary from an earlier run; as in SemNetDictionaries::load.dictionaries(), a vector of MORE than 10 elements is taken to be the words, and 10 or fewer are taken to be names. For type = "free", the spell-check is always against the cocaspell word list; dictionary then only supplies its list of common misspellings Dictionary to be used for more efficient text cleaning. Defaults to NULL, which will use general.dictionary

Use dictionaries() or find.dictionaries() for more options (See SemNetDictionaries for more details)

spelling

Character vector. English spelling to be used.

  • "UK" — For British spelling (e.g., colour, grey, programme, theatre)

  • "US" — For American spelling (e.g., color, gray, program, theater)

add.path

Character. Path to additional dictionaries to be found. DOES NOT search recursively (through all folders in path) to avoid time intensive search. Set to "choose" to open an interactive directory explorer

keepStrings

Boolean. Should strings be retained or separated? Defaults to FALSE. Set to TRUE to retain strings as strings. For fluency data, a string whose words are all in the dictionary (after correction) is still split into them, unless the string itself is a dictionary entry ("sea slug" stays "sea slug"; "marmoset otter" becomes "marmoset" and "otter")

allowPunctuations

Character vector. Allows punctuation characters to be included in responses. Defaults to "-". Set to "all" to keep all punctuation characters

allowNumbers

Boolean. Defaults to FALSE, which removes stand-alone numbers (e.g., list numbering such as "1."). Words that mix letters and digits (e.g., "d0g") are kept as written so that the spell-check can correct or flag them. Set to TRUE to keep all numbers in text

keepLength

Numeric. Maximum number of words allowed in a response. Defaults to NULL. Set a number to keep responses with words less than or equal to the number (e.g., 3 will keep responses with three or less words)

keepCue

Boolean. Should cue words be retained in the responses? Defaults to FALSE. Set to TRUE to allow cue words to be retained

keepRepeats

Boolean. Should repeated responses (perseverations) within a participant be retained in $responses$clean? Only applies to type = "fluency". Defaults to FALSE, which keeps only the first occurrence of each response. Set to TRUE to keep every occurrence (e.g., so that a later analysis, such as SNAFU, can count them). The binary response matrix is unaffected, and repeats are counted in $behavioral$Perseverations either way

continue

List. A result previously unfinished that still needs to be completed. Allows you to continue to manually spell-check your data after you've exited early (X) or it stopped on an error: those are the cases in which textcleaner() returns an unfinished result instead of the final output. A finished result (one that already holds $responses) cannot be passed back, because there is nothing left to continue. The result of a manual spell-check stage that has already finished (as the Shiny app hands over) is accepted too, to skip straight to finalizing the output. Defaults to NULL

reader

Function. The input reader used for every interactive prompt in the manual spell-check wizard. Defaults to readline. Exposed so the whole wizard can be driven programmatically (e.g. in automated tests) with a pre-scripted queue of answers instead of real console input

search_fn

Function. The function used to open a browser search for the "GOOGLE WORD"/ "GOOGLE STRING" manual spell-check options. Defaults to searcher::search_site

cores

Numeric. Number of computer cores to use in parallelized computations (the "ad hoc" moniker re-check after manual spell-check). Defaults to 1 (sequential). With more than one core the workers load the installed package, so SemNetCleaner must be installed (not loaded with devtools::load_all())

edit_fn

Function. The spreadsheet-editor function used to review automated spell-check changes. Defaults to automated_edit, a shiny/DT-based review app (requires those packages to be installed). Pass edit_fn = edit to use base R's lightweight edit() instead, with no extra dependencies. Exposed so this step can be driven programmatically instead of opening a real interactive editor

Details

Responses are cleaned before they are spell-checked: they are lowercased, accented and typographic characters are transliterated to ASCII ("caf\u00e9" becomes "cafe"), and characters with no ASCII equivalent (e.g., Chinese or Cyrillic text) are removed, so a response made only of such characters is treated as missing. The dictionaries supplied by SemNetDictionaries are English.

Value

A list of class "textcleaner". For type = "fluency":

responses

A list of three matrices:

  • original — The responses as given, after basic preparation: missing values (miss) and blank cells converted to NA; punctuation and stand-alone numbers removed (see allowPunctuations and allowNumbers); accented letters converted to plain ones (e.g., an accented "e" to a plain "e"); surrounding and repeated white space removed; and letters lowercased

  • clean — The spell-checked, de-pluralized responses with repeats removed (unless keepRepeats = TRUE); one row per participant, labeled by ID. This can be used as a final dataset for analyses (e.g., fluency of responses)

  • binary — A matrix where each row is a participant and each column a unique response: 1 if the participant gave the response and 0 if not

behavioral

A data frame, one row per participant, with the number of Perseverations (repeated responses), Intrusions (responses marked bad), and Appropriate (responses kept)

spellcheck

A list of three matrices: correspondence — every unique response (from) and what it became (to_1, to_2, ...); automated — the rows changed by the automated spell-check; manual — the rows that went through the manual wizard

moniker

The manual rows of spellcheck: the corrections made by hand. Consider submitting them to SemNetDictionaries

dictionary, added_words

Only present if words were added to the dictionary and "In my results" was chosen when asked where to save them (or a file could not be written). dictionary is the whole dictionary, the one used plus the added words, so it can be passed back as dictionary = result$dictionary. added_words is only the words added during the session

For type = "free", data holds the original and clean long-format data, responses$clean is a response-by-cue frequency matrix, and behavioral, spellcheck and the dictionary objects are as above. If the manual spell-check is exited early, the object instead holds the saved state and can be passed back with continue =

Author(s)

Alexander Christensen <alexpaulchristensen@gmail.com>

References

Christensen, A. P., & Kenett, Y. N. (2021). Semantic network analysis (SemNA): A tutorial on preprocessing, estimating, and analyzing semantic networks. Psychological Methods. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1037/met0000463")}

Hornik, K., & Murdoch, D. (2010). Watch Your Spelling!. The R Journal, 3, 22-28.

Examples

if(interactive())
{
  # Full example with the manual spell-check
  clean <- textcleaner(open.animals[, -c(1, 2)], partBY = "row", dictionary = "animals")
}


SemNetCleaner documentation built on Oct. 16, 2026, 5:07 p.m.