You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

file.go 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package util
  15. import (
  16. "bufio"
  17. "os"
  18. "strings"
  19. )
  20. func parseJsonToFloats(s string) []float64 {
  21. s = strings.TrimLeft(s, "[")
  22. s = strings.TrimRight(s, "]")
  23. s = strings.ReplaceAll(s, "\n", "")
  24. tokens := strings.Split(s, " ")
  25. res := []float64{}
  26. for _, token := range tokens {
  27. if token == "" {
  28. continue
  29. }
  30. f := ParseFloat(token)
  31. res = append(res, f)
  32. }
  33. return res
  34. }
  35. func LoadFactorFileByCsv(path string) ([]string, [][]float64) {
  36. nameArray := []string{}
  37. dataArray := [][]float64{}
  38. file, err := os.Open(path)
  39. if err != nil {
  40. panic(err)
  41. }
  42. defer file.Close()
  43. rows := [][]string{}
  44. LoadCsvFile(path, &rows)
  45. for _, row := range rows {
  46. if row[0] == "" {
  47. continue
  48. }
  49. nameArray = append(nameArray, row[1])
  50. dataArray = append(dataArray, parseJsonToFloats(row[2]))
  51. }
  52. return nameArray, dataArray
  53. }
  54. func LoadFactorFileByCsv2(path string) ([]string, [][]float64) {
  55. nameArray := []string{}
  56. dataArray := [][]float64{}
  57. file, err := os.Open(path)
  58. if err != nil {
  59. panic(err)
  60. }
  61. defer file.Close()
  62. rows := [][]string{}
  63. LoadCsvFile(path, &rows)
  64. for _, row := range rows {
  65. nameArray = append(nameArray, row[0])
  66. dataArray = append(dataArray, StringsToFloats(row[1:]))
  67. }
  68. return nameArray, dataArray
  69. }
  70. func LoadFactorFileBySpace(path string) ([]string, [][]float64) {
  71. nameArray := []string{}
  72. dataArray := [][]float64{}
  73. file, err := os.Open(path)
  74. if err != nil {
  75. panic(err)
  76. }
  77. defer file.Close()
  78. scanner := bufio.NewScanner(file)
  79. const maxCapacity = 1024 * 1024 * 8
  80. buf := make([]byte, maxCapacity)
  81. scanner.Buffer(buf, maxCapacity)
  82. i := 0
  83. for scanner.Scan() {
  84. if i == 0 {
  85. i += 1
  86. continue
  87. }
  88. line := scanner.Text()
  89. line = strings.Trim(line, " ")
  90. tokens := strings.Split(line, " ")
  91. nameArray = append(nameArray, tokens[0])
  92. data := []float64{}
  93. for j := 1; j < len(tokens); j++ {
  94. data = append(data, ParseFloat(tokens[j]))
  95. }
  96. dataArray = append(dataArray, data)
  97. i += 1
  98. }
  99. if err = scanner.Err(); err != nil {
  100. panic(err)
  101. }
  102. return nameArray, dataArray
  103. }