Guest User

Untitled

a guest
May 28th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "bytes"
  6. "compress/gzip"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "strconv"
  12. "strings"
  13.  
  14. cbor "github.com/2tvenom/cbor"
  15. )
  16.  
  17. type Vectors map[string][]float64
  18. type MapOfSimilarity map[float64]string
  19.  
  20. func LoadVectors(inputFile string) Vectors {
  21. vectors := Vectors{}
  22.  
  23. // load the Glove TXT file
  24. file, err := os.Open(inputFile)
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. defer file.Close()
  29.  
  30. scanner := bufio.NewScanner(file)
  31. if err := scanner.Err(); err != nil {
  32. log.Fatal(err)
  33. }
  34.  
  35. for scanner.Scan() {
  36. stringSlice := strings.Split(scanner.Text(), " ")
  37.  
  38. // length is complete slice minus 1
  39. for i := 1; i < (len(stringSlice) - 1); i++ {
  40. // parse the string to a float
  41. float, _ := strconv.ParseFloat(stringSlice[i], 64)
  42. // add the float to the vector
  43. vectors[stringSlice[0]] = append(vectors[stringSlice[0]], float)
  44. //vectors[stringSlice[0]][i] = float
  45. }
  46.  
  47. }
  48.  
  49. return vectors
  50.  
  51. }
  52.  
  53. func main() {
  54.  
  55. // Load vectors
  56. vectors := Vectors{}
  57. vectors = LoadVectors("glove.6B.300d.txt")
  58.  
  59. // Create encoder and marshal
  60. var buffTest bytes.Buffer
  61. encoder := cbor.NewEncoder(&buffTest)
  62. ok, error := encoder.Marshal(vectors)
  63. //check binary string
  64. if !ok {
  65. fmt.Errorf("Error decoding %s", error)
  66. }
  67. fmt.Printf("Encoding to CBOR = done")
  68.  
  69. // Create GZIPed version
  70. var b bytes.Buffer
  71. w := gzip.NewWriter(&b)
  72. w.Write(buffTest.Bytes())
  73. w.Close()
  74.  
  75. // Create output file
  76. fmt.Println("Creating output file", "vectors.cbor.gz")
  77. err := ioutil.WriteFile("vectors.cbor.gz", b.Bytes(), 0644)
  78. if err != nil {
  79. panic(err)
  80. }
  81.  
  82. fmt.Println("DONE")
  83.  
  84. }
Add Comment
Please, Sign In to add comment