github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/text/levenshtein/type-edit-script.go (about)

     1  package levenshtein
     2  
     3  import "fmt"
     4  
     5  // EditOperation types the possible ways to manipulate a slice of tokens.
     6  type EditOp int
     7  
     8  const (
     9  	Ins EditOp = iota
    10  	Del
    11  	Sub
    12  	Match
    13  )
    14  
    15  // String implements the stringer interface. Trivial.
    16  func (o EditOp) String() string {
    17  	if o == Match {
    18  		return "match"
    19  	} else if o == Ins {
    20  		return "ins"
    21  	} else if o == Sub {
    22  		return "sub"
    23  	}
    24  	return "del"
    25  }
    26  
    27  // EditOperation extended holds concrete references
    28  // to the elements that are to be inserted/deleted/substituted.
    29  type EditOpExt struct {
    30  	op       EditOp
    31  	src, dst int
    32  }
    33  
    34  func (op EditOpExt) String() string {
    35  	return fmt.Sprintf("%v-%v-%v", op.op, op.src, op.dst)
    36  }
    37  
    38  // TEditScrpt is a slice of extended edit operations,
    39  // transforming one slice into another.
    40  // Spelling is deliberately distinguished from the method.
    41  type TEditScrpt []EditOpExt
    42  
    43  func (es TEditScrpt) Print() {
    44  
    45  	fmt2 := fmt.Sprintf("%s-%vv", "%", cl)
    46  
    47  	fmt.Printf(fmt2, "EditScr:")
    48  	// fmt.Printf("%v", strings.Repeat(" ", cl))
    49  
    50  	for k, _ := range es {
    51  		fmt.Printf(fmt2, k)
    52  	}
    53  	fmt.Printf("\n")
    54  
    55  	fmt.Printf(fmt2, " ")
    56  	for _, v := range es {
    57  		fmt.Printf(fmt2, v)
    58  	}
    59  	fmt.Printf("\n")
    60  
    61  }