Advertisement
Guest User

Untitled

a guest
Mar 18th, 2021
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. package dictionaries
  2.  
  3. import (
  4. "bufio"
  5. "log"
  6. "os"
  7. "quoteService/internal/system/configReader"
  8. "strings"
  9. )
  10.  
  11. var GDictionaries = new(Dictionaries)
  12.  
  13. type Dictionaries struct {
  14. VerbsDictionary []string
  15. CollocationsDictionary []string
  16. PassivesDictionary []string
  17. //PronounsDictionary map[int][]string
  18. VerbsExceptionsDictionary []string
  19. }
  20.  
  21. func (d *Dictionaries) LoadDictionaries() {
  22.  
  23. d.VerbsDictionary = loadDictionary(configReader.GConfig.DictionariesPaths.Verbs)
  24. //d.PronounsDictionary = loadMapDictionary(configReader.GConfig.DictionariesPaths.Pronouns)
  25. d.CollocationsDictionary = loadDictionary(configReader.GConfig.DictionariesPaths.Collocations)
  26. d.PassivesDictionary = loadDictionary(configReader.GConfig.DictionariesPaths.Passives)
  27. d.VerbsExceptionsDictionary = loadDictionary(configReader.GConfig.DictionariesPaths.VerbsExceptions)
  28. }
  29.  
  30. func loadDictionary(path string) (result []string) {
  31. file, err := os.OpenFile(path, os.O_RDONLY, 0777)
  32. if err != nil {
  33. log.Printf("failed to load file loadDictionary %v", err)
  34. return
  35. }
  36.  
  37. defer func() {
  38. if err := file.Close(); err != nil {
  39. log.Fatalln(err)
  40. }
  41. }()
  42.  
  43. scanner := bufio.NewScanner(file)
  44. for scanner.Scan() {
  45. line := scanner.Text()
  46. result = append(result, line)
  47. }
  48. return
  49. }
  50.  
  51. func (d *Dictionaries) FindVerb(text string) string {
  52. for _, verb := range d.VerbsDictionary {
  53. if text == verb {
  54. return verb
  55. }
  56. }
  57. return ""
  58. }
  59.  
  60. func (d *Dictionaries) FindCollocation(text string) string {
  61. for _, item := range d.CollocationsDictionary {
  62. colwords := strings.Split(item, "*")
  63. if strings.Contains(strings.ToLower(text), colwords[0]) {
  64. if strings.Contains(strings.ToLower(text), colwords[1]) {
  65. return colwords[1]
  66. }
  67. }
  68. }
  69. return ""
  70. }
  71.  
  72. func (d *Dictionaries) FindPassives(text string) string {
  73. for _, passive := range d.PassivesDictionary {
  74. if strings.Contains(text, passive) {
  75. return passive
  76. }
  77. }
  78. return ""
  79. }
  80.  
  81. func (d *Dictionaries) FindVerbExceptions(text string) string {
  82. for _, passive := range d.VerbsExceptionsDictionary {
  83. if strings.Contains(text, passive) {
  84. return passive
  85. }
  86. }
  87. return ""
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement