View source: R/create_tcorpus.r
create_tcorpus | R Documentation |
Create a tCorpus from raw text input. Input can be a character (or factor) vector, data.frame or quanteda corpus. If a data.frame is given, all columns other than the document id and text columns are included as meta data. If a quanteda corpus is given, the ids and texts are already specified, and the docvars will be included in the tCorpus as meta data.
create_tcorpus(x, ...)
## S3 method for class 'character'
create_tcorpus(
x,
doc_id = 1:length(x),
meta = NULL,
udpipe_model = NULL,
split_sentences = F,
max_sentences = NULL,
max_tokens = NULL,
udpipe_model_path = getwd(),
udpipe_cache = 3,
udpipe_cores = NULL,
udpipe_batchsize = 50,
use_parser = F,
remember_spaces = F,
verbose = T,
...
)
## S3 method for class 'data.frame'
create_tcorpus(
x,
text_columns = "text",
doc_column = "doc_id",
udpipe_model = NULL,
split_sentences = F,
max_sentences = NULL,
max_tokens = NULL,
udpipe_model_path = getwd(),
udpipe_cache = 3,
udpipe_cores = NULL,
udpipe_batchsize = 50,
use_parser = F,
remember_spaces = F,
verbose = T,
...
)
## S3 method for class 'factor'
create_tcorpus(x, ...)
## S3 method for class 'corpus'
create_tcorpus(x, ...)
x |
main input. can be a character (or factor) vector where each value is a full text, or a data.frame that has a column that contains full texts. If x (or a text_column in x) has leading or trailing whitespace, this is cut off (and you'll get a warning about it). |
... |
Arguments passed to create_tcorpus.character |
doc_id |
if x is a character/factor vector, doc_id can be used to specify document ids. This has to be a vector of the same length as x |
meta |
A data.frame with document meta information (e.g., date, source). The rows of the data.frame need to match the values of x |
udpipe_model |
Optionally, the name of a Universal Dependencies language model (e.g., "english-ewt", "dutch-alpino"), to use the udpipe package
( |
split_sentences |
Logical. If TRUE, the sentence number of tokens is also computed. (only if udpipe_model is not used) |
max_sentences |
An integer. Limits the number of sentences per document to the specified number. If set when split_sentences == FALSE, split_sentences will be set to TRUE. |
max_tokens |
An integer. Limits the number of tokens per document to the specified number |
udpipe_model_path |
If udpipe_model is used, this path wil be used to look for the model, and if the model doesn't yet exist it will be downloaded to this location. Defaults to working directory |
udpipe_cache |
The number of persistent caches to keep for inputs of udpipe. The caches store tokens in batches. This way, if a lot of data has to be parsed, or if R crashes, udpipe can continue from the latest batch instead of start over. The caches are stored in the corpustools_data folder (in udpipe_model_path). Only the most recent [udpipe_caches] caches will be stored. |
udpipe_cores |
If udpipe_model is used, this sets the number of parallel cores. If not specified, will use the same number of cores as used by data.table (or limited to OMP_THREAD_LIMIT). |
udpipe_batchsize |
In order to report progress and cache results, texts are parsed with udpipe in batches of 50. The price is that there will be some overhead for each batch, so for very large jobs it can be faster to increase the batchsize. If the number of texts divided by the number of parallel cores is lower than the batchsize, the texts are evenly distributed over cores. |
use_parser |
If TRUE, use dependency parser (only if udpipe_model is used) |
remember_spaces |
If TRUE, a column with spaces after each token and column with the start and end positions of tokens are included. Can turn it of for a bit more speed and less memory use, but some features won't work. |
verbose |
If TRUE, report progress. Only if x is large enough to require multiple sequential batches |
text_columns |
if x is a data.frame, this specifies the column(s) that contains text. If multiple columns are used, they are pasted together separated by a double line break. If remember_spaces is true, a "field" column is also added that show the column name for each token, and the start/end positions are local within these fields |
doc_column |
If x is a data.frame, this specifies the column with the document ids. |
By default, texts will only be tokenized, and basic preprocessing techniques (lowercasing, stemming) can be applied with the
preprocess
method. Alternatively, the udpipe package can be used to apply more advanced NLP preprocessing, by
using the udpipe_model argument.
For certain advanced features you need to set remember_spaces to true. We are often used to forgetting all about spaces when we do bag-of-word type stuff, and that's sad. With remember_spaces, the exact position of each token is remembered, including what type of space follows the token (like a space or a line break), and what text field the token came from (if multiple text_columns are specified in create_tcorpus.data.frame)
## ...
tc = create_tcorpus(c('Text one first sentence. Text one second sentence', 'Text two'))
tc$tokens
tc = create_tcorpus(c('Text one first sentence. Text one second sentence', 'Text two'),
split_sentences = TRUE)
tc$tokens
## with meta (easier to S3 method for data.frame)
meta = data.frame(doc_id = c(1,2), source = c('a','b'))
tc = create_tcorpus(c('Text one first sentence. Text one second sentence', 'Text two'),
split_sentences = TRUE,
doc_id = c(1,2),
meta = meta)
tc
d = data.frame(text = c('Text one first sentence. Text one second sentence.',
'Text two', 'Text three'),
date = c('2010-01-01','2010-01-01','2012-01-01'),
source = c('A','B','B'))
tc = create_tcorpus(d, split_sentences = TRUE)
tc
tc$tokens
## use multiple text columns
d$headline = c('Head one', 'Head two', 'Head three')
## use custom doc_id
d$doc_id = c('#1', '#2', '#3')
tc = create_tcorpus(d, text_columns = c('headline','text'), doc_column = 'doc_id',
split_sentences = TRUE)
tc
tc$tokens
## It makes little sense to have full texts as factors, but it tends to happen.
## The create_tcorpus S3 method for factors is essentially identical to the
## method for a character vector.
text = factor(c('Text one first sentence', 'Text one second sentence'))
tc = create_tcorpus(text)
tc$tokens
library(quanteda)
create_tcorpus(data_corpus_inaugural)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.