Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add checker for US indigenous tribe names to avoid #52

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
^README-.*\.png$
^cran-comments\.md$
^script\.R$
^available\.Rcheck$
^available.*\.tar\.gz$
^available.*\.tgz$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
.RData
README.html
script.R
available.Rcheck/
available*.tar.gz
available*.tgz
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Depends: R (>= 3.3.0)
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
Imports:
cli,
clisymbols,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(format,available_bioc)
S3method(format,available_cran)
S3method(format,available_github)
S3method(format,available_sentiment)
S3method(format,available_tribe_names)
S3method(format,available_urban)
S3method(format,available_valid_name)
S3method(format,available_wikipedia)
Expand All @@ -15,6 +16,7 @@ S3method(print,available_cran)
S3method(print,available_github)
S3method(print,available_query)
S3method(print,available_sentiment)
S3method(print,available_tribe_names)
S3method(print,available_urban)
S3method(print,available_valid_name)
S3method(print,available_wikipedia)
Expand All @@ -25,6 +27,7 @@ export(available_on_cran)
export(available_on_github)
export(get_bad_words)
export(get_sentiment)
export(get_tribe_names)
export(get_urban_data)
export(get_wikipidia)
export(get_wiktionary)
Expand Down
1 change: 1 addition & 0 deletions R/available.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ available <- function(name, browse = getOption("available.browse", TRUE), ...) {
function(term) {
compact(list(
get_bad_words(term),
get_tribe_names(term),
get_abbreviation(term),
get_wikipidia(term),
get_wiktionary(term),
Expand Down
238 changes: 238 additions & 0 deletions R/tribe_names.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@


tribe_names <- memoise::memoise(function() {
url <- "https://www.bia.gov/tribal-leaders-csv"

stop_words_in_tribe_names <-
tolower(
c("Tribe of Old Harbor",
"Village",
"River Band",
"Mills",
"Creek",
"Lagoon",
"Pine",
"Sandy",
"Valley",
"Lake",
"Grande",
"Nation of New York",
"Heights",
"Bay",
"Springs",
"Grand",
"Tribe",
"Nation",
"Eastern",
"Fort",
"of Texas",
"of Kansas",
"of Oklahoma",
"King",
"Little",
"Lower",
"Indian Nation",
"Indian Tribe",
"Point",
"IRA",
"Northern",
"Northwestern",
"of Utah",
"Indian Township",
"Pleasant Point",
"Bay",
"of Nebraska",
"Prairie",
"Pueblo of",
"Chapter",
"Nation of Missouri in Kansas and Nebraska",
"Oklahoma",
"United",
"Pass",
"Big",
"Bad",
"Arctic",
"Crow",
"Cow",
"Dry",
"Elk",
"Iowa",
"River",
"Hope",
"Pilot",
"Ruby",
"Ottawa",
"Band",
"Pyramid",
"Skull",
"Upper",
"Harbor",
"Inc",
"of Mississippi",
"Mountain",
"Turtle",
"South",
"Southern",
"Red",
"Round" ,
"Salt" ,
"Bear",
"Berry",
"Beaver",
"Augustine",
"California",
"Blue" ,
"Bishop",
"Cold",
"Craig" ,
"Forest",
"County",
"Lime",
"Lone",
"Mesa",
"Miami" ,
"Leech",
"Minnesota",
"Barrow",
"Eagle",
"False" ,
"Lay" ,
"New",
"Omaha",
"Wisconsin",
"Dot",
"Enterprise" ,
"Summit",
"Table",
"Spirit",
"Three",
"Affiliated",
"White",
"Devil",
"Crooked" ,
"Stony",
"Twin",
"Hills",
"Island",
"Jackson",
"Salmon" ,
"Absentee",
"Citizen",
"Island",
"Manchester",
"Earth",
"Mississippi",
"Council",
"Walker",
"Quartz",
"Platinum",
"Pit",
"Station",
"Petersburg",
"Wales",
"Traverse",
"Slope",
"Confederated",
"Circle",
"Indians Division",
"Indian Colony",
"and Sioux",
"Chicken",
"Ranch",
"Federated Indians of",
"Mission",
"Port",
"Cliff",
"wood",
"Nelson",
"Independence" ,
"Colorado",
"Lions",
"ding",
"Boise" ,
"age",
"rampart",
"-",
"\\.",
"'",
"&",
"\\(",
"\\)",
" "))

# get list and convert to lower case
words <- tolower(read.csv(url)[,2])
# remove stop words
words <- gsub(paste0(stop_words_in_tribe_names, collapse = "|"), "", words)
# remove non-alphanumerics
words <- gsub("[^[:alnum:]]", "", words)
# remove blank and short strings
words <- words[nchar(words) > 2]

unique(words)
})

#' Check for tribe names in pkg name
#'
#' @inheritParams available
#' @export
#' @seealso See \url{https://www.bia.gov/tribal-leaders-directory}
get_tribe_names <- function(name) {
# check each bad word to see if in package name
tribe <- grepl(glue_collapse(tribe_names(), "|"), name)

structure(name[tribe], class = "available_tribe_names")
}

#' @export

format.available_tribe_names <- function(x, ...) {
good <- crayon::green
bad <- crayon::combine_styles(crayon::bgRed, crayon::white)
paste0(
crayon::bold("Indigenous tribe names (recommend to not to use): "),
if (length(x) == 0) {
good(clisymbols::symbol$tick)
} else {
bad(glue_collapse(x, sep = ", ", last = " and "))
},
"\n"
)
}

#' @export

print.available_tribe_names <- function(x, ...) {
cat(format(x, ...))
invisible(x)
}

mark_tribe_names <- function(text, marker = NULL) {

if (is.null(marker)) {
marker <- crayon::combine_styles(crayon::white, crayon::bgRed)
}

vapply(
tolower(text),
mark_tribe_names1,
character(1),
marker = marker,
USE.NAMES = FALSE
)
}

mark_tribe_names1 <- function(text1, marker) {
word_pos <- gregexpr("\\b\\w+\\b", text1)
if (length(word_pos[[1]]) == 1 && word_pos == -1) return(text1)
start <- c(word_pos[[1]])
end <- start + attr(word_pos[[1]], "match.length") - 1
words <- substring(text1, start, end)
stemmed <- SnowballC::wordStem(words, language = "english")

tribe <- words %in% tribe_names() | stemmed %in% tribe_names()
regmatches(text1, word_pos) <- list(ifelse(
tribe, marker(words), words
))
text1
}
1 change: 0 additions & 1 deletion available.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Version: 1.0
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default
QuitChildProcessesOnExit: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
Expand Down
17 changes: 17 additions & 0 deletions man/get_tribe_names.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/testthat/test-tribenames.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
context("Check package name for indigenous tribe names")

test_that("Catches indigenous tribe names", {
expect_identical("snoqualmie", get_tribe_names("snoqualmie")[[1L]])
expect_identical("ute", get_tribe_names("ute")[[1L]])
})

test_that("Passes non-indigenous tribe names", {
expect_true(length(get_tribe_names("happy")) == 0)
expect_true(length(get_tribe_names("peerage")) == 0)
# we don't want to match with 'ute' in the middle of a word, how to do this?
# expect_true(length(get_tribe_names("dispute")) == 0)
})