summaryrefslogtreecommitdiff
path: root/internal/vocab/reader.go
blob: f1506ba9f8241935273ed5c7f0d143d1103d54ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Package vocab reads language-agnostic vocabulary files.
package vocab

import (
	"fmt"
	"os"
	"strings"
)

// CardType identifies the semantic shape of a vocabulary entry.
type CardType string

const (
	// CardTypeTranslation marks a standard word-to-translation entry.
	CardTypeTranslation CardType = "translation"
	// CardTypeDefinition marks a same-language word-to-definition entry.
	CardTypeDefinition CardType = "definition"
)

// WordEntry represents a vocabulary entry with an optional translation.
type WordEntry struct {
	Word             string
	Translation      string
	NeedsTranslation bool
	CardType         CardType
}

// ReadVocabularyFile reads vocabulary entries from a file and returns the parsed slice.
func ReadVocabularyFile(filename string) ([]WordEntry, error) {
	content, err := os.ReadFile(filename)
	if err != nil {
		return nil, fmt.Errorf("read vocabulary file: %w", err)
	}

	normalized := strings.ReplaceAll(string(content), "\r\n", "\n")
	lines := strings.Split(normalized, "\n")
	entries := make([]WordEntry, 0, len(lines))
	for _, line := range lines {
		if entry := parseLine(line); entry != nil {
			entries = append(entries, *entry)
		}
	}

	if len(entries) == 0 {
		return nil, nil
	}

	return entries, nil
}

func parseLine(line string) *WordEntry {
	trimmed := strings.TrimSpace(line)
	if trimmed == "" {
		return nil
	}

	if strings.Contains(trimmed, "==") {
		parts := strings.SplitN(trimmed, "==", 2)
		if len(parts) != 2 {
			return nil
		}

		word := strings.TrimSpace(parts[0])
		translation := strings.TrimSpace(parts[1])
		if word == "" || translation == "" {
			return nil
		}

		return &WordEntry{
			Word:             word,
			Translation:      translation,
			NeedsTranslation: false,
			CardType:         CardTypeDefinition,
		}
	}

	if strings.Contains(trimmed, "=") {
		parts := strings.SplitN(trimmed, "=", 2)
		if len(parts) != 2 {
			return nil
		}

		word := strings.TrimSpace(parts[0])
		translation := strings.TrimSpace(parts[1])
		if word == "" && translation != "" {
			return &WordEntry{
				Word:             "",
				Translation:      translation,
				NeedsTranslation: true,
				CardType:         CardTypeTranslation,
			}
		}
		if word != "" && translation != "" {
			return &WordEntry{
				Word:             word,
				Translation:      translation,
				NeedsTranslation: false,
				CardType:         CardTypeTranslation,
			}
		}

		return nil
	}

	return &WordEntry{
		Word:             trimmed,
		Translation:      "",
		NeedsTranslation: false,
		CardType:         CardTypeTranslation,
	}
}