forked from quanteda/quanteda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas.dictionary.Rd
65 lines (58 loc) · 2.09 KB
/
as.dictionary.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/dictionaries.R
\name{as.dictionary}
\alias{as.dictionary}
\alias{is.dictionary}
\title{Coercion and checking functions for dictionary objects}
\usage{
as.dictionary(x, format = c("tidytext"), separator = " ",
tolower = FALSE)
is.dictionary(x)
}
\arguments{
\item{x}{a dictionary-like object to be coerced or checked}
\item{format}{input format for the object to be coerced to a
\link{dictionary}; current legal values are a data.frame with the fields
\code{word} and \code{sentiment} (as per the \strong{tidytext} package)}
\item{separator}{the character in between multi-word dictionary values. This
defaults to \code{" "}.}
\item{tolower}{if \code{TRUE}, convert all dictionary values to lowercase}
}
\value{
\code{as.dictionary} returns a \pkg{quanteda} \link{dictionary}
object. This conversion function differs from the \code{\link{dictionary}}
constructor function in that it converts an existing object rather than
creates one from components or from a file.
\code{is.dictionary} returns \code{TRUE} if an object is a
\pkg{quanteda} \link{dictionary}.
}
\description{
Convert a dictionary from a different format into a \pkg{quanteda}
dictionary, or check to see if an object is a dictionary.
}
\examples{
\dontrun{
data(sentiments, package = "tidytext")
as.dictionary(subset(sentiments, lexicon == "nrc"))
as.dictionary(subset(sentiments, lexicon == "bing"))
# to convert AFINN into polarities - adjust thresholds if desired
datafinn <- subset(sentiments, lexicon == "AFINN")
datafinn[["sentiment"]] <-
with(datafinn,
sentiment <- ifelse(score < 0, "negative",
ifelse(score > 0, "positive", "netural"))
)
with(datafinn, table(score, sentiment))
as.dictionary(datafinn)
dat <- data.frame(
word = c("Great", "Horrible"),
sentiment = c("positive", "negative")
)
as.dictionary(dat)
as.dictionary(dat, tolower = FALSE)
}
is.dictionary(dictionary(list(key1 = c("val1", "val2"), key2 = "val3")))
# [1] TRUE
is.dictionary(list(key1 = c("val1", "val2"), key2 = "val3"))
# [1] FALSE
}