"Fossies" - the Fresh Open Source Software Archive

Member "buildah-1.27.2/tests/tools/vendor/github.com/nbutton23/zxcvbn-go/adjacency/adjcmartix.go" (20 Sep 2022, 2446 Bytes) of package /linux/misc/buildah-1.27.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package adjacency
    2 
    3 import (
    4     "encoding/json"
    5     "log"
    6 
    7     "github.com/nbutton23/zxcvbn-go/data"
    8 )
    9 
   10 // Graph holds information about different graphs
   11 type Graph struct {
   12     Graph         map[string][]string
   13     averageDegree float64
   14     Name          string
   15 }
   16 
   17 // GraphMap is a map of all graphs
   18 var GraphMap = make(map[string]Graph)
   19 
   20 func init() {
   21     GraphMap["qwerty"] = BuildQwerty()
   22     GraphMap["dvorak"] = BuildDvorak()
   23     GraphMap["keypad"] = BuildKeypad()
   24     GraphMap["macKeypad"] = BuildMacKeypad()
   25     GraphMap["l33t"] = BuildLeet()
   26 }
   27 
   28 //BuildQwerty builds the Qwerty Graph
   29 func BuildQwerty() Graph {
   30     data, err := data.Asset("data/Qwerty.json")
   31     if err != nil {
   32         panic("Can't find asset")
   33     }
   34     return getAdjancencyGraphFromFile(data, "qwerty")
   35 }
   36 
   37 //BuildDvorak builds the Dvorak Graph
   38 func BuildDvorak() Graph {
   39     data, err := data.Asset("data/Dvorak.json")
   40     if err != nil {
   41         panic("Can't find asset")
   42     }
   43     return getAdjancencyGraphFromFile(data, "dvorak")
   44 }
   45 
   46 //BuildKeypad builds the Keypad Graph
   47 func BuildKeypad() Graph {
   48     data, err := data.Asset("data/Keypad.json")
   49     if err != nil {
   50         panic("Can't find asset")
   51     }
   52     return getAdjancencyGraphFromFile(data, "keypad")
   53 }
   54 
   55 //BuildMacKeypad builds the Mac Keypad Graph
   56 func BuildMacKeypad() Graph {
   57     data, err := data.Asset("data/MacKeypad.json")
   58     if err != nil {
   59         panic("Can't find asset")
   60     }
   61     return getAdjancencyGraphFromFile(data, "mac_keypad")
   62 }
   63 
   64 //BuildLeet builds the L33T Graph
   65 func BuildLeet() Graph {
   66     data, err := data.Asset("data/L33t.json")
   67     if err != nil {
   68         panic("Can't find asset")
   69     }
   70     return getAdjancencyGraphFromFile(data, "keypad")
   71 }
   72 
   73 func getAdjancencyGraphFromFile(data []byte, name string) Graph {
   74 
   75     var graph Graph
   76     err := json.Unmarshal(data, &graph)
   77     if err != nil {
   78         log.Fatal(err)
   79     }
   80     graph.Name = name
   81     return graph
   82 }
   83 
   84 // CalculateAvgDegree calclates the average degree between nodes in the graph
   85 //on qwerty, 'g' has degree 6, being adjacent to 'ftyhbv'. '\' has degree 1.
   86 //this calculates the average over all keys.
   87 //TODO double check that i ported this correctly scoring.coffee ln 5
   88 func (adjGrp Graph) CalculateAvgDegree() float64 {
   89     if adjGrp.averageDegree != float64(0) {
   90         return adjGrp.averageDegree
   91     }
   92     var avg float64
   93     var count float64
   94     for _, value := range adjGrp.Graph {
   95 
   96         for _, char := range value {
   97             if len(char) != 0 || char != " " {
   98                 avg += float64(len(char))
   99                 count++
  100             }
  101         }
  102 
  103     }
  104 
  105     adjGrp.averageDegree = avg / count
  106 
  107     return adjGrp.averageDegree
  108 }