Levenshtein distance/Alignment: Difference between revisions

Go solution
(→‎{{header|Racket}}: Why was this posted as a potential solution?)
(Go solution)
Line 141:
<pre>r_oset_tacode
raisethysword</pre>
 
=={{header|Go}}==
{{libheader|biogo}}
Alignment computed by the Needleman-Wunch algorithm, which is used in bioinformatics.
<lang go>package main
 
import (
"fmt"
 
"code.google.com/p/biogo/align"
ab "code.google.com/p/biogo/alphabet"
"code.google.com/p/biogo/feat"
"code.google.com/p/biogo/seq/linear"
)
 
func main() {
// Alphabets for things like DNA are predefined in biogo, but we
// define our own here.
lc := ab.Must(ab.NewAlphabet("-abcdefghijklmnopqrstuvwxyz",
feat.Undefined, '-', 0, true))
// Construct scoring matrix for Needleman-Wunch algorithm.
// We leave zeros on the diagonal for the Levenshtein distance of an
// exact match and put -1s everywhere else for the Levenshtein distance
// of an edit.
nw := make(align.NW, lc.Len())
for i := range nw {
r := make([]int, lc.Len())
nw[i] = r
for j := range r {
if j != i {
r[j] = -1
}
}
}
// define input sequences
a := &linear.Seq{Seq: ab.BytesToLetters([]byte("rosettacode"))}
a.Alpha = lc
b := &linear.Seq{Seq: ab.BytesToLetters([]byte("raisethysword"))}
b.Alpha = lc
// perform alignment
aln, err := nw.Align(a, b)
// format and display result
if err != nil {
fmt.Println(err)
return
}
fa := align.Format(a, b, aln, '-')
fmt.Printf("%s\n%s\n", fa[0], fa[1])
}</lang>
{{out}}
<pre>
r-oset-tacode
raisethysword
</pre>
 
=={{header|Perl}}==
1,707

edits