github.com/sajari/fuzzy@v1.0.0/old.go (about)

     1  // Eventually this should be removed. Currently it gives backwards compatability to old
     2  // versions that did not store the query count, which is now used for autocomplete.
     3  package fuzzy
     4  
     5  import (
     6  	"encoding/json"
     7  	"os"
     8  )
     9  
    10  type OldModel struct {
    11  	Data            map[string]int      `json:"data"`
    12  	Maxcount        int                 `json:"maxcount"`
    13  	Suggest         map[string][]string `json:"suggest"`
    14  	Depth           int                 `json:"depth"`
    15  	Threshold       int                 `json:"threshold"`
    16  	UseAutocomplete bool                `json:"autocomplete"`
    17  }
    18  
    19  // Converts the old model format to the new version
    20  func (model *Model) convertOldFormat(filename string) error {
    21  	oldmodel := new(OldModel)
    22  	f, err := os.Open(filename)
    23  	if err != nil {
    24  		return err
    25  	}
    26  	defer f.Close()
    27  	d := json.NewDecoder(f)
    28  	err = d.Decode(oldmodel)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	// Correct for old models pre divergence measure
    34  	if model.SuffDivergenceThreshold == 0 {
    35  		model.SuffDivergenceThreshold = SuffDivergenceThresholdDefault
    36  	}
    37  
    38  	// Convert fields
    39  	model.Maxcount = oldmodel.Maxcount
    40  	model.Suggest = oldmodel.Suggest
    41  	model.Depth = oldmodel.Depth
    42  	model.Threshold = oldmodel.Threshold
    43  	model.UseAutocomplete = oldmodel.UseAutocomplete
    44  
    45  	// Convert the old counts
    46  	if len(oldmodel.Data) > 0 {
    47  		model.Data = make(map[string]*Counts, len(oldmodel.Data))
    48  		for term, cc := range oldmodel.Data {
    49  			model.Data[term] = &Counts{cc, 0}
    50  		}
    51  	}
    52  	return nil
    53  }