mkearney / chr Goto Github PK
View Code? Open in Web Editor NEW๐ค Lightweight R package for manipulating [string] characters
Home Page: https://chr.mikewk.com
License: Other
๐ค Lightweight R package for manipulating [string] characters
Home Page: https://chr.mikewk.com
License: Other
Any interest in a function that determines if inputs are anagrams of each other? I created an anagrams pkg using Rcpp/c++, but in the process created pure base R versions of the functions that I'm not using. If interested I'd be happy to submit a PR....if not, no worries.
Here's the basic idea:
# Test for anagrams that are the same length as the input string.
chr_anagram("stac", c("cats are great", "tacs", "frogs", "cats", "ts"))
#> [1] FALSE TRUE FALSE TRUE FALSE
# Use arg "value" to return the values that are anagrams.
chr_anagram("stac", c("cats are great", "tacs", "frogs", "cats", "ts"), value = TRUE)
#> [1] "tacs" "cats"
# Use arg "any_len" to test for anagrams that are any length (either same length or sub-string).
chr_anagram("stac", c("cats are great", "tacs", "frogs", "cats", "ts"), any_len = TRUE)
#> [1] TRUE TRUE FALSE TRUE FALSE
# Use arg "ignore_space" to make anagram searching insensitive to spaces.
chr_anagram("s t a c", c("cats are great", "t acs", "frogs", "ca ts", "ts"), ignore_space = TRUE)
#> [1] FALSE TRUE FALSE TRUE FALSE
# Use arg "ignore_case" to make anagram searching insensitive to lower/upper case.
chr_anagram("STAc", c("catS are great", "tacs", "frogs", "CaTS", "ts"), ignore_case = TRUE)
#> [1] FALSE TRUE FALSE TRUE FALSE
Any interest in a function that generates char ngram tokens? I created something similar (using base R) for a pkg of mine, but just rewrote it in c++ so am not using the R function anymore.
If there's any interest I'd be happy to submit a PR.
Here's the output:
x <- c("Acme Pizza, Inc.", "Tom's Sports Equipment, LLC")
ngram_func(x, ngram_len = 2L)
#> [[1]]
#> [1] "Ac" "cm" "me" "e " " P" "Pi" "iz" "zz" "za" "a," ", " " I" "In" "nc" "c."
#> [[2]]
#> [1] "To" "om" "m'" "'s" "s " " S" "Sp" "po" "or" "rt" "ts" "s " " E" "Eq" "qu" "ui" "ip" "pm" "me" "en" "nt" "t," ", " " L" "LL" "LC"
ngram_func(x, ngram_len = 3L, lower = TRUE, strip_punc = TRUE, strip_ws = TRUE)
#> [[1]]
#> [1] "acm" "cme" "mep" "epi" "piz" "izz" "zza" "zai" "ain" "inc"
#> [[2]]
#> [1] "tom" "oms" "mss" "ssp" "spo" "por" "ort" "rts" "tse" "seq" "equ" "qui" "uip" "ipm" "pme" "men" "ent" "ntl" "tll" "llc"
And here's the function:
ngram_func <- function(x, ngram_len = 2L, lower = FALSE, strip_punc = FALSE,
strip_ws = FALSE) {
# Input validation
stopifnot(is.character(x))
stopifnot(is.integer(ngram_len))
stopifnot(is.logical(lower))
stopifnot(is.logical(strip_punc))
stopifnot(is.logical(strip_ws))
# If arg "lower" is TRUE, make all chars in x lowercase.
if (isTRUE(lower)) x <- tolower(x)
# If arg "strip_punc" is TRUE, remove all punctuation from x.
if (isTRUE(strip_punc)) x <- gsub("[[:punct:]]", "", x)
# If arg "strip_ws" is TRUE, remove all white space from x.
if (isTRUE(strip_ws)) x <- gsub("\\s", "", x)
# Split each element of x into individual chars.
x <- strsplit(x, "", fixed = TRUE)
# If ngram_len is 1L, return x, as strsplit handles tokenization into single
# chars.
if (identical(ngram_len, 1L)) return(x)
# Generate ngram tokens.
ngram_len <- ngram_len - 1
lapply(x, function(strings) {
vapply(seq_len(length(strings) - ngram_len), function(char) {
paste(strings[char:(char + ngram_len)], collapse = "")
}, character(1))
})
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.