Git Product home page Git Product logo

chr's People

Contributors

chrismuir avatar mkearney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

chr's Issues

anagrams

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

Generate char ngram tokens

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))
  })
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.