Package overview

In natural language processing, tokenization is the process of breaking human-readable text into machine readable components. The most obvious way to tokenize a text is to split the text into words. But there are many other ways to tokenize a text, the most useful of which are provided by this package.

The tokenizers in this package have a consistent interface. They all take either a character vector of any length, or a list where each element is a character vector of length one. The idea is that each element comprises a text. Then each function returns a list with the same length as the input vector, where each element in the list contains the tokens generated by the function. If the input character vector or list is named, then the names are preserved, so that the names can serve as identifiers.

Using the following sample text, the rest of this vignette demonstrates the different kinds of tokenizers in this package.

Word and word-stem tokenizers

The word tokenizer splits texts into words.

Word stemming is provided by the SnowballC package.

You can also provide a vector of stopwords which will be omitted. The stopwords package, which contains stopwords for many languages from several sources, is recommended. This argument also works with the n-gram and skip n-gram tokenizers.

An alternative word stemmer often used in NLP that preserves punctuation and separates common English contractions is the Penn Treebank tokenizer.

N-gram and skip n-gram tokenizers

An n-gram is a contiguous sequence of words containing at least n_min words and at most n words. This function will generate all such combinations of n-grams, omitting stopwords if desired.

A skip n-gram is like an n-gram in that it takes the n and n_min parameters. But rather than returning contiguous sequences of words, it will also return sequences of n-grams skipping words with gaps between 0 and the value of k. This function generates all such sequences, again omitting stopwords if desired. Note that the number of tokens returned can be very large.

Tweet tokenizer

Tokenizing tweets requires special attention, since usernames (@whoever) and hashtags (#hashtag) use special characters that might otherwise be stripped away.

Sentence and paragraph tokenizers

Sometimes it is desirable to split texts into sentences or paragraphs prior to tokenizing into other forms.

#> [[1]]
#> [1] "The question thus becomes a verbal one again; and our knowledge of all these early stages of thought and feeling is in any case so conjectural and imperfect that farther discussion would not be worth while."                                               
#> [2] "Religion, therefore, as I now ask you arbitrarily to take it, shall mean for us _the feelings, acts, and experiences of individual men in their solitude, so far as they apprehend themselves to stand in relation to whatever they may consider the divine_."
#> [3] "Since the relation may be either moral, physical, or ritual, it is evident that out of religion in the sense in which we take it, theologies, philosophies, and ecclesiastical organizations may secondarily grow."
#> [[1]]
#> [1] "The question thus becomes a verbal one again; and our knowledge of all these early stages of thought and feeling is in any case so conjectural and imperfect that farther discussion would not be worth while."                                                                                                                                                                                                                                                                   
#> [2] "Religion, therefore, as I now ask you arbitrarily to take it, shall mean for us _the feelings, acts, and experiences of individual men in their solitude, so far as they apprehend themselves to stand in relation to whatever they may consider the divine_. Since the relation may be either moral, physical, or ritual, it is evident that out of religion in the sense in which we take it, theologies, philosophies, and ecclesiastical organizations may secondarily grow. "

Text chunking

When one has a very long document, sometimes it is desirable to split the document into smaller chunks, each with the same length. This function chunks a document and gives it each of the chunks an ID to show their order. These chunks can then be further tokenized.

chunks <- chunk_text(mobydick, chunk_size = 100, doc_id = "mobydick")
length(chunks)
#> [1] 2195
chunks[5:6]
#> $`mobydick-0005`
#> [1] "of a poor devil of a sub sub appears to have gone through the long vaticans and street stalls of the earth picking up whatever random allusions to whales he could anyways find in any book whatsoever sacred or profane therefore you must not in every case at least take the higgledy piggledy whale statements however authentic in these extracts for veritable gospel cetology far from it as touching the ancient authors generally as well as the poets here appearing these extracts are solely valuable or entertaining as affording a glancing bird's eye view of what has been promiscuously said"
#> 
#> $`mobydick-0006`
#> [1] "thought fancied and sung of leviathan by many nations and generations including our own so fare thee well poor devil of a sub sub whose commentator i am thou belongest to that hopeless sallow tribe which no wine of this world will ever warm and for whom even pale sherry would be too rosy strong but with whom one sometimes loves to sit and feel poor devilish too and grow convivial upon tears and say to them bluntly with full eyes and empty glasses and in not altogether unpleasant sadness give it up sub subs for by how much the"
tokenize_words(chunks[5:6])
#> $`mobydick-0005`
#>  [1] "of"       "a"        "poor"     "devil"    "of"       "a"       
#>  [7] "sub"      "sub"      "appears"  "to"       "have"     "gone"    
#> [13] "through"  "the"      "long"     "vaticans" "and"      "street"  
#> [19] "stalls"   "of"       "the"      "earth"    "picking"  "up"      
#> [25] "whatever"
#>  [ reached getOption("max.print") -- omitted 75 entries ]
#> 
#> $`mobydick-0006`
#>  [1] "thought"     "fancied"     "and"         "sung"        "of"         
#>  [6] "leviathan"   "by"          "many"        "nations"     "and"        
#> [11] "generations" "including"   "our"         "own"         "so"         
#> [16] "fare"        "thee"        "well"        "poor"        "devil"      
#> [21] "of"          "a"           "sub"         "sub"         "whose"      
#>  [ reached getOption("max.print") -- omitted 75 entries ]

Counting words, characters, sentences

The package also offers functions for counting words, characters, and sentences in a format which works nicely with the rest of the functions.