Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dictionaries
- import (
- "bufio"
- "log"
- "os"
- "quoteService/internal/system/configReader"
- "strings"
- )
- var GDictionaries = new(Dictionaries)
- type Dictionaries struct {
- VerbsDictionary []string
- CollocationsDictionary []string
- PassivesDictionary []string
- //PronounsDictionary map[int][]string
- VerbsExceptionsDictionary []string
- }
- func (d *Dictionaries) LoadDictionaries() {
- d.VerbsDictionary = loadDictionary(configReader.GConfig.DictionariesPaths.Verbs)
- //d.PronounsDictionary = loadMapDictionary(configReader.GConfig.DictionariesPaths.Pronouns)
- d.CollocationsDictionary = loadDictionary(configReader.GConfig.DictionariesPaths.Collocations)
- d.PassivesDictionary = loadDictionary(configReader.GConfig.DictionariesPaths.Passives)
- d.VerbsExceptionsDictionary = loadDictionary(configReader.GConfig.DictionariesPaths.VerbsExceptions)
- }
- func loadDictionary(path string) (result []string) {
- file, err := os.OpenFile(path, os.O_RDONLY, 0777)
- if err != nil {
- log.Printf("failed to load file loadDictionary %v", err)
- return
- }
- defer func() {
- if err := file.Close(); err != nil {
- log.Fatalln(err)
- }
- }()
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- line := scanner.Text()
- result = append(result, line)
- }
- return
- }
- func (d *Dictionaries) FindVerb(text string) string {
- for _, verb := range d.VerbsDictionary {
- if text == verb {
- return verb
- }
- }
- return ""
- }
- func (d *Dictionaries) FindCollocation(text string) string {
- for _, item := range d.CollocationsDictionary {
- colwords := strings.Split(item, "*")
- if strings.Contains(strings.ToLower(text), colwords[0]) {
- if strings.Contains(strings.ToLower(text), colwords[1]) {
- return colwords[1]
- }
- }
- }
- return ""
- }
- func (d *Dictionaries) FindPassives(text string) string {
- for _, passive := range d.PassivesDictionary {
- if strings.Contains(text, passive) {
- return passive
- }
- }
- return ""
- }
- func (d *Dictionaries) FindVerbExceptions(text string) string {
- for _, passive := range d.VerbsExceptionsDictionary {
- if strings.Contains(text, passive) {
- return passive
- }
- }
- return ""
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement