-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
129 lines (109 loc) · 3.08 KB
/
utils.go
File metadata and controls
129 lines (109 loc) · 3.08 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// neng -- Non-Extravagant Name Generator
// Copyright (C) 2024 Wojciech Głąb (github.com/Zedran)
//
// This file is part of neng.
//
// neng is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 only.
//
// neng is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with neng. If not, see <https://www.gnu.org/licenses/>.
package neng
import (
"fmt"
"strings"
)
// cmpWord is a comparison function for slices.IsSortedFunc
// and slices.SortFunc calls.
func cmpWord(a, b Word) int {
return strings.Compare(a.word, b.word)
}
// countSyllables returns a number of syllables in s given the consonant-vowel
// sequence. General accuracy of this function is not very high, especially
// for borrowed words (cafe). It targets specific groups of verbs.
func countSyllables(s, seq string) int {
if len(s) == 0 {
return 0
}
var (
prevVowel bool
count int
)
for _, sc := range seq {
switch sc {
case 'v':
if !prevVowel {
// Diphthongs and long vowels are part of one syllable
prevVowel = true
count++
}
default:
prevVowel = false
}
}
if count > 1 && endsWithAny(s, []string{"eat", "eate", "iate", "uate"}) {
// When apparent diphthongs are in fact individual vowels
// and belong to separate syllables
count++
}
if strings.HasSuffix(s, "e") && strings.HasSuffix(seq, "cv") {
// A final 'e' preceded by a consonant (silent 'e')
// does not constitute the next syllable
count--
}
if count == 0 {
return 1
}
return count
}
// doubleFinal returns the verb with its final consonant doubled
// and tenseEnding appended.
func doubleFinal(verb, tenseEnding string) string {
return verb + string(verb[len(verb)-1]) + tenseEnding
}
// endsWithAny returns true if s ends with any element of the suf slice.
func endsWithAny(s string, suf []string) bool {
for _, suffix := range suf {
if strings.HasSuffix(s, suffix) {
return true
}
}
return false
}
// getSequence returns a vowel-consonant sequence of s ('word' == 'cvcc').
func getSequence(s string) string {
var (
seq strings.Builder
vowels string = "aeiou"
)
for i, c := range s {
if i == len(s)-1 && c == 'y' && !strings.ContainsRune(vowels, rune(s[i-1])) {
// A special case of final 'y' following a consonant
seq.WriteByte('v')
} else if strings.ContainsRune(vowels, c) {
seq.WriteByte('v')
} else {
seq.WriteByte('c')
}
}
return seq.String()
}
// parseLines converts lines into a slice of Word.
// Relays an error from NewWord (line formatting).
func parseLines(lines []string) ([]Word, error) {
words := make([]Word, len(lines))
for i, ln := range lines {
w, err := NewWord(ln)
if err != nil {
return nil, fmt.Errorf("%d '%s' - incorrect format", i, ln)
}
words[i] = w
}
return words, nil
}