esbot/internal/lang/lang.go

82 lines
1.2 KiB
Go

package lang
import (
"encoding/csv"
"fmt"
"io"
"os"
"strings"
)
type (
Mood int
Tense int
)
const (
Indicative Mood = iota
)
const (
Present Tense = iota
Future
Imperfect
Preterite
Conditional
PresentPerfect
FuturePerfect
PastPerfect
)
type Verb struct {
Inf string
InfEn string
Forms []string
}
func (v *Verb) String() string {
return fmt.Sprintf("%s: %s\n%s", v.Inf, v.InfEn, strings.Join(v.Forms, ", "))
}
var (
verbs map[string]*Verb = make(map[string]*Verb)
)
func GetVerb(q string) *Verb {
return verbs[q]
}
func Load(verbsFile string) (int, error) {
var err error
f, err := os.Open(verbsFile)
if err != nil {
return 0, fmt.Errorf("could not open verbs file: %v", err)
}
csv := csv.NewReader(f)
csv.FieldsPerRecord = 17
csv.LazyQuotes = true
var record []string
var count int
for {
record, err = csv.Read()
if err != nil {
if err == io.EOF {
break
}
return 0, fmt.Errorf("could not read verb database: %v", err)
}
// only indicative present tense for now
if record[2] != "Indicativo" || record[4] != "Presente" {
continue
}
inf := record[0]
verbs[inf] = &Verb{Inf: inf, InfEn: record[1], Forms: record[7:13]}
count++
}
return count, nil
}