stringdist R Package

repository·master·Indexed 43 days ago

https://github.com/markvanderloo/stringdist

An R package for approximate string matching, fuzzy text search, and string distance calculations. Optimized for speed using C and openMP for parallel computing, it implements various metrics including Levenshtein, Jaro-Winkler, Hamming, and Cosine distance. It provides core functions like stringdist, stringdistmatrix, and stringsim, as well as fuzzy matching equivalents to native R functions such as amatch and ain.

Tokens
1.6K
Snippets
4
Records
14
Agent score
6%

What's inside stringdist

  1. Install the stringdist package

    master

    You can install the stable version of stringdist from CRAN using the standard R installation command.

    If you need the absolute latest development version from source, you can clone the GitHub repository and build it manually. Warning: The development version is written in C and may be unstable or cause R sessions to crash.

    install.packages('stringdist')
  2. Access the stringdist C/C++ API

    master

    Developers writing C or C++ code in other packages can call stringdist's underlying C functions directly. To access the documentation for the C API, you can either type ?stringdist_api in the R console or open the specific vignette using the command below.

    vignette("stringdist_C-Cpp_api", package="stringdist")
  3. Install stringdist from source (GitHub)

    master

    To build the package from the latest source code, use a bash terminal (or Git Bash on Windows) to clone, build, and install the resulting tarball.

    git clone https://github.com/markvanderloo/stringdist.git
    cd stringdist
    bash ./build.bash
    R CMD INSTALL output/stringdist_*.tar.gz
  4. Note on deprecated arguments in stringdist

    master

    As of version 0.9.5.0, several arguments that were previously used for parallelization or distance limiting have been removed. If you are upgrading from a very old version, ensure you remove the following from your function calls:

    • cluster (for stringdistmatrix)
    • maxDist (for stringdist and stringdistmatrix, but NOT amatch)
    • ncores (for stringdistmatrix)

    Parallelization Note: The package now uses openMP for parallel computing instead of R's parallel package, making it more efficient and system-independent.

  5. Core string distance and matching functions

    master

    The stringdist package provides several primary functions for approximate string matching and distance calculations in R:

    • stringdist: Computes pairwise distances between two input character vectors (the shorter vector is recycled).
    • stringdistmatrix: Computes the distance matrix for one or two vectors.
    • stringsim: Computes a string similarity score between 0 and 1, based on stringdist.
    • amatch: A fuzzy matching equivalent of R's native match function.
    • ain: A fuzzy matching equivalent of R's native %in% operator.
    • afind: Finds the location of fuzzy matches of a short string within a long string.
    • seq_dist, seq_distmatrix, seq_amatch, and seq_ain: Specialized versions for computing distances and matching for integer sequences.
  6. Use fuzzy matching functions in R

    master

    For tasks requiring fuzzy matching (finding approximate matches rather than exact ones), use these functions which act as equivalents to native R functions:

    • amatch: A fuzzy matching equivalent of R's native match function.
    • ain: A fuzzy matching equivalent of R's native %in% operator.
  7. Calculate distances and matches for integer sequences

    master

    If you are working with integer sequences rather than character strings, use the following specialized functions:

    • seq_dist: Distance between integer sequences.
    • seq_distmatrix: Distance matrix for integer sequences.
    • seq_amatch: Fuzzy matching for integer sequences.
    • seq_ain: Fuzzy %in% equivalent for integer sequences.
  8. Access the stringdist C API for R package development

    master

    Since version 0.9.5.0, you can call stringdist functions directly from the C code of your own R package. To access the full API documentation, you can:

    1. Type ?stringdist_api in the R console.
    2. Browse the package help index under User guides, package vignettes and other documentation and select doc/stringdist_api.pdf.
    3. Locate the PDF directly using:
    system.file("doc/stringdist_api.pdf", package="stringdist")
  9. Utility functions for string processing

    master

    Use these utility functions for phonetic encoding and character analysis:

    • qgrams(): Tabulates qgrams in one or more character vectors.
    • seq_qrams(): Tabulates qgrams (ngrams) in one or more integer vectors.
    • phonetic(): Computes phonetic codes (currently only supports Soundex).
    • printable_ascii(): Detects non-printable ASCII or non-ASCII characters.
  10. Use stringdist utility functions

    master

    The package includes several utilities for string processing and analysis:

    • qgrams(): Tabulates qgrams in one or more character vectors.
    • seq_qrams(): Tabulates qgrams (sometimes called ngrams) in one or more integer vectors.
    • phonetic(): Computes phonetic codes of strings (currently supports soundex).
    • printable_ascii(): Detects non-printable ASCII or non-ASCII characters.
  11. Use stringdist functions for string distance and similarity

    master

    The package provides several core functions for calculating distances and similarities between character vectors:

    • stringdist: Computes pairwise distances between two input character vectors (the shorter vector is recycled).
    • stringdistmatrix: Computes a distance matrix for one or two vectors.
    • stringsim: Computes a string similarity score between 0 and 1, derived from stringdist.