Skip to content

Commit

Permalink
Minor code cleanup; updated docs and ci settings
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-alrux committed Feb 17, 2017
1 parent ad4eb37 commit 5f10fee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 53 deletions.
26 changes: 15 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
language: go

sudo: false
go:
- 1.8
- 1.7.5
- 1.7.4
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7
- tip
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6
- tip
- 1.5.4
- 1.5.3
- 1.5.2
Expand All @@ -32,15 +33,22 @@ go:
- 1.1.2
- 1.1.1
- 1.1
- 1.0.3
- 1.0.2
- 1.0.1
- 1

before_install:
- go get github.com/mattn/goveralls
script:
- $HOME/gopath/bin/goveralls -service=travis-ci
notifications:
email:
on_success: never
matrix:
fast_finish: true
allow_failures:
- go: tip
- go: 1.6.4
- go: 1.6.3
- go: 1.6.2
- go: 1.6.1
- go: 1.6
- go: 1.5.4
- go: 1.5.3
- go: 1.5.2
Expand All @@ -60,7 +68,3 @@ matrix:
- go: 1.1.2
- go: 1.1.1
- go: 1.1
- go: 1.0.3
- go: 1.0.2
- go: 1.0.1
- go: 1
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# A Go package for calculating the Levenshtein distance between two strings

This package implements distance and similarity metrics for strings, based on the Levenshtein measure, in [Go](http://golang.org).
[![Release](https://img.shields.io/github/release/agext/levenshtein.svg?style=flat)](https://github.com/agext/levenshtein/releases/latest)
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/agext/levenshtein) 
[![Build Status](https://travis-ci.org/agext/levenshtein.svg?branch=master&style=flat)](https://travis-ci.org/agext/levenshtein)
[![Coverage Status](https://coveralls.io/repos/github/agext/levenshtein/badge.svg?style=flat)](https://coveralls.io/github/agext/levenshtein)
[![Go Report Card](https://goreportcard.com/badge/github.com/agext/levenshtein?style=flat)](https://goreportcard.com/report/github.com/agext/levenshtein)


## Maturity
This package implements distance and similarity metrics for strings, based on the Levenshtein measure, in [Go](http://golang.org).

[![Build Status](https://travis-ci.org/agext/levenshtein.svg?branch=master)](https://travis-ci.org/agext/levenshtein)
## Project Status

v1.2 Stable: Guaranteed no breaking changes to the API in future v1.x releases. No known bugs or performance issues. Probably safe to use in production, though provided on "AS IS" basis.
v1.2.1 Stable: Guaranteed no breaking changes to the API in future v1.x releases. Probably safe to use in production, though provided on "AS IS" basis.

This package is being actively maintained. If you encounter any problems or have any suggestions for improvement, please [open an issue](https://github.com/agext/levenshtein/issues). Pull requests are welcome.

## Overview

[![GoDoc](https://godoc.org/github.com/agext/levenshtein?status.png)](https://godoc.org/github.com/agext/levenshtein)

The Levenshtein `Distance` between two strings is the minimum total cost of edits that would convert the first string into the second. The allowed edit operations are insertions, deletions, and substitutions, all at character (one UTF-8 code point) level. Each operation has a default cost of 1, but each can be assigned its own cost equal to or greater than 0.

A `Distance` of 0 means the two strings are identical, and the higher the value the more different the strings. Since in practice we are interested in finding if the two strings are "close enough", it often does not make sense to continue the calculation once the result is mathematically guaranteed to exceed a desired threshold. Providing this value to the `Distance` function allows it to take a shortcut and return a lower bound instead of an exact cost when the threshold is exceeded.
Expand Down
72 changes: 36 additions & 36 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,106 +47,106 @@ func NewParams() *Params {

// Clone returns a pointer to a copy of the receiver parameter set, or of a new
// default parameter set if the receiver is nil.
func (this *Params) Clone() *Params {
if this == nil {
func (p *Params) Clone() *Params {
if p == nil {
return NewParams()
}
return &Params{
insCost: this.insCost,
subCost: this.subCost,
delCost: this.delCost,
maxCost: this.maxCost,
minScore: this.minScore,
bonusPrefix: this.bonusPrefix,
bonusScale: this.bonusScale,
bonusThreshold: this.bonusThreshold,
insCost: p.insCost,
subCost: p.subCost,
delCost: p.delCost,
maxCost: p.maxCost,
minScore: p.minScore,
bonusPrefix: p.bonusPrefix,
bonusScale: p.bonusScale,
bonusThreshold: p.bonusThreshold,
}
}

// InsCost overrides the default value of 1 for the cost of insertion.
// The new value must be zero or positive.
func (this *Params) InsCost(v int) *Params {
func (p *Params) InsCost(v int) *Params {
if v >= 0 {
this.insCost = v
p.insCost = v
}
return this
return p
}

// SubCost overrides the default value of 1 for the cost of substitution.
// The new value must be zero or positive.
func (this *Params) SubCost(v int) *Params {
func (p *Params) SubCost(v int) *Params {
if v >= 0 {
this.subCost = v
p.subCost = v
}
return this
return p
}

// DelCost overrides the default value of 1 for the cost of deletion.
// The new value must be zero or positive.
func (this *Params) DelCost(v int) *Params {
func (p *Params) DelCost(v int) *Params {
if v >= 0 {
this.delCost = v
p.delCost = v
}
return this
return p
}

// MaxCost overrides the default value of 0 (meaning unlimited) for the maximum cost.
// The calculation of Distance() stops when the result is guaranteed to exceed
// this maximum, returning a lower-bound rather than exact value.
// The new value must be zero or positive.
func (this *Params) MaxCost(v int) *Params {
func (p *Params) MaxCost(v int) *Params {
if v >= 0 {
this.maxCost = v
p.maxCost = v
}
return this
return p
}

// MinScore overrides the default value of 0 for the minimum similarity score.
// Scores below this threshold are returned as 0 by Similarity() and Match().
// The new value must be zero or positive. Note that a minimum greater than 1
// can never be satisfied, resulting in a score of 0 for any pair of strings.
func (this *Params) MinScore(v float64) *Params {
func (p *Params) MinScore(v float64) *Params {
if v >= 0 {
this.minScore = v
p.minScore = v
}
return this
return p
}

// BonusPrefix overrides the default value for the maximum length of
// common prefix to be considered for bonus by Match().
// The new value must be zero or positive.
func (this *Params) BonusPrefix(v int) *Params {
func (p *Params) BonusPrefix(v int) *Params {
if v >= 0 {
this.bonusPrefix = v
p.bonusPrefix = v
}
return this
return p
}

// BonusScale overrides the default value for the scaling factor used by Match()
// in calculating the bonus.
// The new value must be zero or positive. To guarantee that the similarity score
// remains in the interval 0..1, this scaling factor is not allowed to exceed
// 1 / BonusPrefix.
func (this *Params) BonusScale(v float64) *Params {
func (p *Params) BonusScale(v float64) *Params {
if v >= 0 {
this.bonusScale = v
p.bonusScale = v
}

// the bonus cannot exceed (1-sim), or the score may become greater than 1.
if float64(this.bonusPrefix)*this.bonusScale > 1 {
this.bonusScale = 1 / float64(this.bonusPrefix)
if float64(p.bonusPrefix)*p.bonusScale > 1 {
p.bonusScale = 1 / float64(p.bonusPrefix)
}

return this
return p
}

// BonusThreshold overrides the default value for the minimum similarity score
// for which Match() can assign a bonus.
// The new value must be zero or positive. Note that a threshold greater than 1
// effectively makes Match() become the equivalent of Similarity().
func (this *Params) BonusThreshold(v float64) *Params {
func (p *Params) BonusThreshold(v float64) *Params {
if v >= 0 {
this.bonusThreshold = v
p.bonusThreshold = v
}
return this
return p
}

0 comments on commit 5f10fee

Please sign in to comment.