-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstopwords.R
More file actions
159 lines (139 loc) · 5.41 KB
/
Copy pathstopwords.R
File metadata and controls
159 lines (139 loc) · 5.41 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#' Collection of stopwords in multiple languages
#'
#' @description
#' This function returns character vectors of stopwords for different languages,
#' using the [ISO-639-1 language
#' codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes), and allows for
#' different sources of stopwords to be defined.
#'
#' The default source is the [`Snowball()`][data_stopwords_snowball]
#' stopwords collection but [`other()`][stopwords-package] sources are
#' also available.
#' @param language specify language of stopwords by ISO 639-1 code
#' @param source specify a stopwords source. To list the currently
#' available options, use [stopwords_getsources()].
#' @param simplify logical; if `TRUE` return a simple vector, if
#' `FALSE` return a list if the original word list was nested
#' @return a character vector containing the stopwords, or a list
#' of characters `simplify = FALSE`
#' @details
#' The language codes for each stopword list use the two-letter ISO
#' code from <https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes>.
#' For backwards compatibility, the full English names of the stopwords
#' from the \pkg{quanteda} package may also be used, although these are
#' deprecated.
#' @export
#'
#' @examples
#' stopwords("en")
#' stopwords("de")
stopwords <- function(language = "en", source = "snowball", simplify = TRUE) {
stopwords_options()
if (length(language) > 1)
stop("only one language may be specified")
if (length(source) > 1)
stop("only one source may be specified")
# for quanteda compability
if (missing(source) && tolower(language) == "smart") {
.Deprecated(old = paste0("stopwords(language = \"", language, "\")"),
new = "stopwords(source = \"smart\")")
source <- "smart"
language <- "en"
}
error <- create_error(
default = paste0("Language ", "\"", language, "\" not available in source \"", source, "\"."),
note = "See `?stopwords_getlanguages` for more information on supported languages."
)
if (nchar(language) > 2) {
language <- tryCatch(
lookup_iso_639_1(language, source),
error = function(message) error(message)
)
}
# for quanteda compability
if (missing(source) && tolower(language) %in% c("el", "ar", "zh")) {
language <- tolower(language)
.Deprecated(old = paste0("stopwords(language = \"", language, "\")"),
new = paste0("stopwords(language = \"", language, "\", source = \"misc\")"))
source <- "misc"
}
words <- tryCatch(
get_stopwords_data(source)[[language]],
error = function(message) error(message)
)
if (is.null(words)) {
error(paste0("Language \"", language, "\" not found."))
}
if (simplify) unlist(words, use.names = FALSE) else words
}
#' list available stopwords sources
#'
#' Returns a character vector of the stopword sources available from the
#' \pkg{stopwords} package.
#' @export
stopwords_getsources <- function() {
stopwords_options()
names(getOption("stopwords_sources"))
}
#' list available stopwords country codes
#'
#' Lists the available stopwords country codes for a given stopwords source.
#' See <https://en.wikipedia.org/wiki/ISO_639-1> for details of the language code.
#' @param source the source of the stopwords
#' @export
stopwords_getlanguages <- function(source) {
stopwords_options()
error <- create_error(
default = paste0("Source \"", source, "\" not found."),
note = "See `?stopwords_getsources` for a list of all available sources."
)
tryCatch(
names(get_stopwords_data(source)),
error = function(message) error(message)
)
}
#' return ISO-639-1 code for a given language name
#'
#' Looks up the two-character ISO-639-1 language code for a given
#' language name.
#' @importFrom stats na.omit
#' @keywords internal
#' @param language_name character; name of a language
#' @param source the short name for a language source, e.g. "snowball"
lookup_iso_639_1 <- function(language_name, source) {
language_data <- na.omit(ISOcodes::ISO_639_2[, c("Alpha_2", "Name")])
# remove Norwegian variants
language_data <- language_data[-which(language_data[["Alpha_2"]] %in% c("nn", "nb")), ]
# match the language to the name
language_code_index <- grep(language_name, language_data[["Name"]], ignore.case = TRUE)
if (!length(language_code_index)) {
if (!language_name %in% stopwords_getlanguages(source))
stop("Language \"", language_name, "\" not found for source \"", source, "\".")
language_name
} else if (length(language_code_index) > 1) {
message <- paste0("Multiple language codes found for \"", language_name, "\":\n",
paste0(language_data[language_code_index, 2], collapse = "\n"))
stop(message)
} else {
language_data[["Alpha_2"]][language_code_index]
}
}
# Create consistent error messages
create_error <- function(default, note, message = character(0)) {
function(message) {
message <- message[1] # ensure that condition is length 1
msg <- paste0(ifelse(missing(message) || message == "", default, message), "\n", note)
stop(msg, call. = FALSE)
}
}
# Retrieve data from sources
get_stopwords_data <- function(source) {
stopwords_options()
if (! source %in% names(getOption("stopwords_sources"))) {
message <- paste0("Source \"", source, "\" not found.")
stop(message, call. = FALSE)
}
data_object_name <- getOption("stopwords_sources")[source]
# this allows the data to be accessed without attaching the package
eval(parse(text = paste0("stopwords::", data_object_name)))
}