Learn R Programming

⚠️There's a newer version (4.2.0) of this package.Take me there.

quanteda: Quantitative Analysis of Textual Data

quanteda is changing!

See the new, upcoming major API changes, to be incorporated into the next minor version 0.9.9 and for CRAN, in a "1.0" release.

About the package

See the Getting Started Vignette.

An R package for managing and analyzing text, by Ken Benoit and Paul Nulty.

quanteda makes it easy to manage texts in the form of a corpus, defined as a collection of texts that includes document-level variables specific to each text, as well as meta-data for documents and for the collection as a whole. quanteda includes tools to make it easy and fast to manuipulate the texts in a corpus, by performing the most common natural language processing tasks simply and quickly, such as tokenizing, stemming, or forming ngrams. quanteda's functions for tokenizing texts and forming multiple tokenized documents into a document-feature matrix are both extremely fast and extremely simple to use. quanteda can segment texts easily by words, paragraphs, sentences, or even user-supplied delimiters and tags.

Built on the text processing functions in the stringi package, which is in turn built on C++ implementation of the ICU libraries for Unicode text handling, quanteda pays special attention to fast and correct implementation of Unicode and the handling of text in any character set, following conversion internally to UTF-8.

quanteda is built for efficiency and speed, through its design around three infrastructures: the stringi package for text processing, the data.table package for indexing large documents efficiently, and the Matrix package for sparse matrix objects. If you can fit it into memory, quanteda will handle it quickly. (And eventually, we will make it possible to process objects even larger than available memory.)

quanteda is principally designed to allow users a fast and convenient method to go from a corpus of texts to a selected matrix of documents by features, after defining and selecting the documents and features. The package makes it easy to redefine documents, for instance by splitting them into sentences or paragraphs, or by tags, as well as to group them into larger documents by document variables, or to subset them based on logical conditions or combinations of document variables. The package also implements common NLP feature selection functions, such as removing stopwords and stemming in numerous languages, selecting words found in dictionaries, treating words as equivalent based on a user-defined "thesaurus", and trimming and weighting features based on document frequency, feature frequency, and related measures such as tf-idf.

Once constructed, a quanteda "dfm"" can be easily analyzed using either quanteda's built-in tools for scaling document positions, or used with a number of other text analytic tools, such as:

  • topic models (including converters for direct use with the topicmodels, LDA, and stm packages)

  • document scaling (using quanteda's own functions for the "wordfish" and "Wordscores" models, direct use with the ca package for correspondence analysis, or scaling with the austin package)

  • machine learning through a variety of other packages that take matrix or matrix-like inputs.

Additional features of quanteda include:

  • the ability to explore texts using key-words-in-context;

  • fast computation of a variety of readability indexes;

  • fast computation of a variety of lexical diversity measures;

  • quick computation of word or document association measures, for clustering or to compute similarity scores for other purposes; and

  • a comprehensive suite of descriptive statistics on text such as the number of sentences, words, characters, or syllables per document.

Planned features coming soon to quanteda are:

  • bootstrapping methods for texts that makes it easy to resample texts from pre-defined units, to facilitate computation of confidence intervals on textual statistics using techniques of non-parametric bootstrapping, but applied to the original texts as data.

  • expansion of predictive and analytic methods called through the standard interface called textmodel(). Current model types include correspondence analysis, "Wordscores", "Wordfish", and Naive Bayes.

  • Addition of settings to corpus projects, that will propogate through downstream objects.

  • Addition of a history that will propogate through downstream objects.

Acknowledgements: This research was supported by the European Research Council grant ERC-2011-StG 283794-QUANTESS.

How to Install

As of version 0.8.0, the GitHub master repository will always contain the development version of quanteda, while the CRAN version will contain the latest "stable" version. You therefore have two options for installing the package:

  1. From CRAN, using your R package installer, or simply

    install.packages("quanteda")
  2. (For the development version) From GitHub, using

    # devtools packaged required to install quanteda from Github
    devtools::install_github("kbenoit/quanteda")

    Because this compiles some C++ source code, you will need a compiler installed. If you are using a Windows platform, this means you will need also to install the Rtools software available from CRAN. If you are using OS X, you will need to to install XCode, available for free from the App Store, or if you prefer a lighter footprint set of tools, just the Xcode command line tools, using the command xcode-select --install from the Terminal.

  3. (Optional) You can install some additional corpus data from quantedaData using

    devtools::install_github("kbenoit/quantedaData")

Example usage

library(quanteda)
#> quanteda version 0.9.9.2
#> 
#> Attaching package: 'quanteda'
#> The following object is masked from 'package:base':
#> 
#>     sample
# create a corpus from the immigration texts from UK party platforms
uk2010immigCorpus <- corpus(ukimmigTexts,
                            docvars=data.frame(party=names(ukimmigTexts)),
                            notes="Immigration-related sections of 2010 UK party manifestos",
                            enc="UTF-8")
#> Warning in corpus.character(ukimmigTexts, docvars = data.frame(party =
#> names(ukimmigTexts)), : Argument enc not used.
uk2010immigCorpus
#> Corpus consisting of 9 documents and 1 docvar.
summary(uk2010immigCorpus, showmeta=TRUE)
#> Corpus consisting of 9 documents.
#> 
#>          Text Types Tokens Sentences        party
#>           BNP  1126   3330        88          BNP
#>     Coalition   144    268         4    Coalition
#>  Conservative   252    503        15 Conservative
#>        Greens   325    687        21       Greens
#>        Labour   296    703        29       Labour
#>        LibDem   257    499        14       LibDem
#>            PC    80    118         5           PC
#>           SNP    90    136         4          SNP
#>          UKIP   346    739        27         UKIP
#> 
#> Source:  /Users/kbenoit/Dropbox (Personal)/GitHub/quanteda/* on x86_64 by kbenoit
#> Created: Wed Oct 19 19:51:22 2016
#> Notes:   Immigration-related sections of 2010 UK party manifestos

# key words in context for "deport", 3 words of context
kwic(uk2010immigCorpus, "deport", 3)
#>                        contextPre keyword                contextPost
#>  [BNP, 159]        The BNP will [  deport ] all foreigners convicted
#> [BNP, 1970]                . 2. [  Deport ] all illegal immigrants  
#> [BNP, 1976] immigrants We shall [  deport ] all illegal immigrants  
#> [BNP, 2621]  Criminals We shall [  deport ] all criminal entrants

# create a dfm, removing stopwords
mydfm <- dfm(uk2010immigCorpus, ignoredFeatures=c("will", stopwords("english")))
#> Creating a dfm from a corpus ...
#> 
#>    ... lowercasing
#> 
#>    ... tokenizing
#> 
#>    ... indexing documents: 9 documents
#> 
#>    ... indexing features:
#> 1,585 feature types
#> 
#> ...
#> removed 97 features, from 175 supplied (glob) feature types
#>    ... created a 9 x 1489 sparse dfm
#>    ... complete. 
#> Elapsed time: 0.03 seconds.
dim(mydfm)              # basic dimensions of the dfm
#> [1]    9 1489
topfeatures(mydfm, 20)  # 20 top words
#> immigration     british      people      asylum     britain          uk 
#>          66          37          35          29          28          27 
#>      system  population     country         new  immigrants      ensure 
#>          27          21          20          19          17          17 
#>       shall citizenship      social    national         bnp     illegal 
#>          17          16          14          14          13          13 
#>        work     percent 
#>          13          12
plot(mydfm, min.freq = 6, random.order = FALSE)             # word cloud     

Documentation

In-depth tutorials in the form of a gitbook will be available here here.

Examples for any function can also be seen using (for instance, for corpus()):

example(corpus)

There are also some demo functions that show off some of the package capabilities, such as demo(quanteda).

Copy Link

Version

Install

install.packages('quanteda')

Monthly Downloads

22,050

Version

0.9.8.5

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Kenneth Benoit

Last Published

October 31st, 2016

Functions in quanteda (0.9.8.5)

as.data.frame,dfm-method

coerce a dfm to a data.frame
applyDictionary

apply a dictionary or thesaurus to an object
docnames

get or set document names
compress

compress a dfm by combining similarly named dimensions
corpus

constructor for corpus objects
docvars

get or set for document-level variables
docfreq

compute the (weighted) document frequency of a feature
changeunits

change the document units of a corpus
dfm

create a document-feature matrix
cbind.dfm

Combine dfm objects by Rows or Columns
inaugCorpus

A corpus of US presidential inaugural addresses from 1789-2013
encodedTexts

encoded texts for testing
head.dfm

Return the first or last part of a dfm
encoding

detect the encoding of texts
head.tokenSequences

print a tokenSequences objects
features

extract the feature labels from a dfm
findSequences

find sequences of tokens
encodedTextFiles

a .zip file of texts containing a variety of differently encoded texts
ie2010Corpus

Irish budget speeches from 2010
exampleString

A paragraph of text for testing various text-based functions
joinTokens

join tokens function
metadoc

get or set document-level meta-data
LBGexample

dfm with example data from Table 1 of Laver Benoit and Garry (2003)
lexdiv

calculate lexical diversity
ndoc

get the number of documents or features
ngrams

Create ngrams and skipgrams
metacorpus

get or set corpus metadata
kwic

List key words in context from a text or a corpus of texts.
mobydickText

Project Gutenberg text of Herman Melville's Moby Dick
nsentence

count the number of sentences
quanteda-package

An R package for the quantitative analysis of textual data
print.tokenSequences

print a tokenSequences objects
plot.kwic

plot the dispersion of key word(s)
readability

calculate readability
ntoken

count the number of tokens or types
print.tokenizedTexts

print a tokenizedTexts objects
plot.dfm

plot features as a wordcloud
predict.textmodel_NB_fitted

prediction method for Naive Bayes classifier objects
print.dfm

print a dfm object
phrasetotoken

convert phrases into single tokens
sort.dfm

sort a dfm by one or more margins
removeFeatures

remove features from an object
similarity

compute similarities between documents and/or features
segment

segment texts into component elements
settings

Get or set the corpus settings
show,dictionary-method

print a dictionary object
selectFeatures

select features from an object
selectFeaturesOLD

old version of selectFeatures.tokenizedTexts
scrabble

compute the Scrabble letter values of text
sample

Randomly sample documents or features
subset.corpus

extract a subset of a corpus
stopwords

access built-in stopwords
syllables

count syllables in a text
tail.tokenSequences

print a tokenSequences objects
summary.corpus

summarize a corpus or a vector of texts
textmodel_ca

correspondence analysis of a document-feature matrix
textmodel_NB

Naive Bayes classifier for texts
textmodel_wordfish

wordfish text model
texts

get corpus texts
tfidf

compute tf-idf weights from a dfm
weight

weight the feature frequencies in a dfm
wordstem

stem words
ukimmigTexts

Immigration-related sections of 2010 UK party manifestos
trim

Trim a dfm using threshold-based or random feature selection
toLower

Convert texts to lower (or upper) case
topfeatures

list the most frequent features