Advertisement
Guest User

Untitled

a guest
Sep 13th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/csv"
  5. "fmt"
  6. "os"
  7. "strings"
  8. )
  9.  
  10. func main() {
  11. file, err := os.Open("words.csv")
  12. if err != nil {
  13. fmt.Println("Ошибка при открытии файла:", err)
  14. return
  15. }
  16. defer file.Close()
  17.  
  18. reader := csv.NewReader(file)
  19. records, err := reader.ReadAll()
  20. if err != nil {
  21. fmt.Println("Ошибка при чтении файла CSV:", err)
  22. return
  23. }
  24.  
  25. columns := records[0]
  26. words := make([]map[string]string, 0)
  27.  
  28. for _, row := range records[1:] {
  29. word := make(map[string]string)
  30. for i, col := range columns {
  31. word[col] = strings.TrimSpace(row[i])
  32. }
  33. words = append(words, word)
  34. }
  35.  
  36. languages := []string{"isv", "en", "ru", "be", "uk", "pl", "cs", "sk", "bg", "mk", "sr", "hr", "sl"}
  37.  
  38. fmt.Printf("Izberite jezyk %v\n", languages)
  39.  
  40. var lang string
  41. fmt.Scanln(&lang)
  42.  
  43. for {
  44. fmt.Println("Napišite slovo")
  45. var word string
  46. fmt.Scanln(&word)
  47.  
  48. result := make([]map[string]string, 0)
  49.  
  50. for _, w := range words {
  51. if wordExistsInSplit(word, w[lang]) {
  52. result = append(result, w)
  53. }
  54. }
  55.  
  56. if len(result) == 0 {
  57. fmt.Println("Nema rezultatov")
  58. continue
  59. }
  60.  
  61. for _, r := range result {
  62. for _, lang := range languages {
  63. fmt.Printf("%s %s\n", lang, r[lang])
  64. }
  65. fmt.Println()
  66. }
  67. }
  68. }
  69.  
  70. func wordExistsInSplit(s string, text string) bool {
  71. splits := strings.Split(text, ", ")
  72. for _, split := range splits {
  73. if split == s {
  74. return true
  75. }
  76. }
  77.  
  78. splits = strings.Split(text, "; ")
  79. for _, split := range splits {
  80. if split == s {
  81. return true
  82. }
  83. }
  84.  
  85. return false
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement