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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package util
  2. import (
  3. "bufio"
  4. "os"
  5. "strings"
  6. )
  7. func parseJsonToFloats(s string) []float64 {
  8. s = strings.TrimLeft(s, "[")
  9. s = strings.TrimRight(s, "]")
  10. s = strings.ReplaceAll(s, "\n", "")
  11. tokens := strings.Split(s, " ")
  12. res := []float64{}
  13. for _, token := range tokens {
  14. if token == "" {
  15. continue
  16. }
  17. f := ParseFloat(token)
  18. res = append(res, f)
  19. }
  20. return res
  21. }
  22. func LoadVectorFileByCsv(path string) ([]string, [][]float64) {
  23. nameArray := []string{}
  24. dataArray := [][]float64{}
  25. file, err := os.Open(path)
  26. if err != nil {
  27. panic(err)
  28. }
  29. defer file.Close()
  30. rows := [][]string{}
  31. LoadCsvFile(path, &rows)
  32. for _, row := range rows {
  33. if row[0] == "" {
  34. continue
  35. }
  36. nameArray = append(nameArray, row[1])
  37. dataArray = append(dataArray, parseJsonToFloats(row[2]))
  38. }
  39. return nameArray, dataArray
  40. }
  41. func LoadVectorFileByCsv2(path string) ([]string, [][]float64) {
  42. nameArray := []string{}
  43. dataArray := [][]float64{}
  44. file, err := os.Open(path)
  45. if err != nil {
  46. panic(err)
  47. }
  48. defer file.Close()
  49. rows := [][]string{}
  50. LoadCsvFile(path, &rows)
  51. for _, row := range rows {
  52. nameArray = append(nameArray, row[0])
  53. dataArray = append(dataArray, StringsToFloats(row[1:]))
  54. }
  55. return nameArray, dataArray
  56. }
  57. func LoadVectorFileBySpace(path string) ([]string, [][]float64) {
  58. nameArray := []string{}
  59. dataArray := [][]float64{}
  60. file, err := os.Open(path)
  61. if err != nil {
  62. panic(err)
  63. }
  64. defer file.Close()
  65. scanner := bufio.NewScanner(file)
  66. const maxCapacity = 1024 * 1024 * 8
  67. buf := make([]byte, maxCapacity)
  68. scanner.Buffer(buf, maxCapacity)
  69. i := 0
  70. for scanner.Scan() {
  71. if i == 0 {
  72. i += 1
  73. continue
  74. }
  75. line := scanner.Text()
  76. line = strings.Trim(line, " ")
  77. tokens := strings.Split(line, " ")
  78. nameArray = append(nameArray, tokens[0])
  79. data := []float64{}
  80. for j := 1; j < len(tokens); j++ {
  81. data = append(data, ParseFloat(tokens[j]))
  82. }
  83. dataArray = append(dataArray, data)
  84. i += 1
  85. }
  86. if err = scanner.Err(); err != nil {
  87. panic(err)
  88. }
  89. return nameArray, dataArray
  90. }

基于Casbin的开源AI领域知识库平台

Contributors (1)