Giter VIP home page Giter VIP logo

levenshteintransformations's Introduction

Levenshtein Transformations Icon

Levenshtein Transformations

GitHub · Contributor Docs

Transform an array into any other array of the same type, or a string into any other string, using only the three transformations replace, delete and insert.

Table of Contents

Installation

  1. Open your Swift package in GNOME Builder, Xcode, or any other IDE.
  2. Open the Package.swift file.
  3. Into the Package initializer, under dependencies, paste:
.package(url: "https://github.com/david-swift/LevenshteinTransformations", from: "0.1.0")   

Usage

let source = [1, 2, 5, 6]
let target = [0, 1, 5, 6, 10]

There are three functions available for arrays containing elements conforming to Equatable and strings.

Arrays with Equatable Elements and Strings

Levenshtein Distance

Get the Levenshtein distance with levenshteinDistance(to:):

print(source.levenshteinDistance(to: target))

This example outputs 3.

Transformation Steps

Get the individual transformation steps with getTransformations(to:).

print(source.getTransformations(to: target).map { $0.description(source: source) }.joined(separator: "\n"))

This example prints the descriptions of the transformations:

Replace '1' at position 0 with '0'
Replace '2' at position 1 with '1'
Insert '10' at position 4

Run Functions for the Steps

Directly run functions for the replace, delete and insert transformations using the transform(to:functions:) function.

var modified = source

source.transform(
    to: target,
    functions: .init { index, element in
        modified[index] = element
    } delete: { index in
        modified.remove(at: index)
    } insert: { index, element in
        modified.insert(element, at: index)
    }
)

print(modified == target)

This example outputs true.

Arrays with Identifiable Elements

The same functions are available for arrays with identifiable elements, but they are named differently to avoid conflicts:

  • identifiableLevenshteinDistance(to:)
  • identifiableGetTransformations(to:)
  • identifiableTransform(to:functions:)

Thanks

levenshteintransformations's People

Contributors

david-swift avatar

Stargazers

adamz avatar Marcin Krzyzanowski avatar  avatar

Watchers

Guido Soranzio avatar  avatar  avatar

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.