

First we have the output column name that will be created as the text is unnested into it ( word, in this case), and then the input column that the text comes from ( text, in this case). The two basic arguments to unnest_tokens used here are column names. Library ( tidytext ) text_df %>% unnest_tokens ( word, text ) #> # A tibble: 20 × 2 #> line word #> #> 1 1 because #> 2 1 i #> 3 1 could #> 4 1 not #> 5 1 stop #> 6 1 for #> 7 1 death #> 8 2 he #> 9 2 kindly #> 10 2 stopped #> # … with 10 more rows Let’s hold off on exploring corpus and document-term matrix objects until Chapter 5, and get down to the basics of converting text to a tidy format. The value in the matrix is typically word count or tf-idf (see Chapter 3). Document-term matrix: This is a sparse matrix describing a collection (i.e., a corpus) of documents with one row for each document and one column for each term.

Corpus: These types of objects typically contain raw strings annotated with additional metadata and details.String: Text can, of course, be stored as strings, i.e., character vectors, within R, and often text data is first read into memory in this form.

This is worth contrasting with the ways text is often stored in text mining approaches. Structuring text data in this way means that it conforms to tidy data principles and can be manipulated with a set of consistent tools. The models can then be re-converted into a tidy form for interpretation and visualization with ggplot2.ġ.1 Contrasting tidy text with other data structuresĪs we stated above, we define the tidy text format as being a table with one-token-per-row. This allows, for example, a workflow where importing, filtering, and processing is done using dplyr and other tidy tools, after which the data is converted into a document-term matrix for machine learning applications. The package includes functions to tidy() objects (see the broom package ) from popular text mining R packages such as tm ( Feinerer, Hornik, and Meyer 2008) and quanteda ( Benoit and Nulty 2016). We’ve found these tidy tools extend naturally to many text analyses and explorations.Īt the same time, the tidytext package doesn’t expect a user to keep text data in a tidy form at all times during an analysis. By keeping the input and output in tidy tables, users can transition fluidly between these packages. Tidy data sets allow manipulation with a standard set of “tidy” tools, including popular packages such as dplyr ( Wickham and Francois 2016), tidyr ( Wickham 2016), ggplot2 ( Wickham 2009), and broom ( Robinson 2017). In the tidytext package, we provide functionality to tokenize by commonly used units of text like these and convert to a one-term-per-row format. For tidy text mining, the token that is stored in each row is most often a single word, but can also be an n-gram, sentence, or paragraph. This one-token-per-row structure is in contrast to the ways text is often stored in current analyses, perhaps as strings or in a document-term matrix. A token is a meaningful unit of text, such as a word, that we are interested in using for analysis, and tokenization is the process of splitting text into tokens. We thus define the tidy text format as being a table with one-token-per-row.
