@@ -39,28 +39,51 @@ public class BuzzwordPhraseCommand: StringCommand {
39
39
private struct Generator {
40
40
var corpus : Corpus
41
41
42
- mutating func phrase( adjectives: Int = 1 , nouns: Int = 2 ) -> String {
43
- ( ( 0 ..< adjectives) . map { _ in adjective ( ) } + ( 0 ..< nouns) . map { _ in noun ( ) } ) . joined ( separator: " " )
42
+ private enum GenerationError : Error {
43
+ case noMoreNouns
44
+ case noMoreCompoundPrefixes
45
+ case noMoreCompoundSuffixes
46
+ case noMoreAdjectives
44
47
}
45
48
46
- private mutating func noun ( ) -> String {
47
- corpus . nouns. removeRandomElementBySwap ( ) ?? " "
49
+ mutating func phrase ( adjectives : Int = 1 , nouns : Int = 2 ) throws -> String {
50
+ try ( ( 0 ..< adjectives ) . map { _ in try adjective ( ) } + ( 0 ..< nouns) . map { _ in try noun ( ) } ) . joined ( separator : " " )
48
51
}
49
52
50
- private mutating func compoundPrefix( ) -> String {
51
- corpus. compoundPrefixes. removeRandomElementBySwap ( ) ?? " "
53
+ private mutating func noun( ) throws -> String {
54
+ guard let noun = corpus. nouns. removeRandomElementBySwap ( ) else {
55
+ throw GenerationError . noMoreNouns
56
+ }
57
+ return noun
52
58
}
53
59
54
- private mutating func compoundSuffix( ) -> String {
55
- corpus. compoundSuffixes. removeRandomElementBySwap ( ) ?? " "
60
+ private mutating func compoundPrefix( ) throws -> String {
61
+ guard let compoundPrefix = corpus. compoundPrefixes. removeRandomElementBySwap ( ) else {
62
+ throw GenerationError . noMoreCompoundPrefixes
63
+ }
64
+ return compoundPrefix
56
65
}
57
66
58
- private mutating func compoundAdjective( ) -> String {
59
- " \( Bool . random ( ) ? noun ( ) : compoundPrefix ( ) ) - \( compoundSuffix ( ) ) "
67
+ private mutating func compoundSuffix( ) throws -> String {
68
+ guard let compoundSuffix = corpus. compoundSuffixes. removeRandomElementBySwap ( ) else {
69
+ throw GenerationError . noMoreCompoundSuffixes
70
+ }
71
+ return compoundSuffix
60
72
}
61
73
62
- private mutating func adjective( ) -> String {
63
- Bool . random ( ) ? compoundAdjective ( ) : corpus. adjectives. removeRandomElementBySwap ( ) !
74
+ private mutating func primitiveAdjective( ) throws -> String {
75
+ guard let adjective = corpus. adjectives. removeRandomElementBySwap ( ) else {
76
+ throw GenerationError . noMoreAdjectives
77
+ }
78
+ return adjective
79
+ }
80
+
81
+ private mutating func compoundAdjective( ) throws -> String {
82
+ try " \( Bool . random ( ) ? noun ( ) : compoundPrefix ( ) ) - \( compoundSuffix ( ) ) "
83
+ }
84
+
85
+ private mutating func adjective( ) throws -> String {
86
+ try Bool . random ( ) ? compoundAdjective ( ) : primitiveAdjective ( )
64
87
}
65
88
}
66
89
@@ -159,8 +182,12 @@ public class BuzzwordPhraseCommand: StringCommand {
159
182
let nouns = parsedArgs. output. nouns. flatMap { Int ( $0) } ?? 2
160
183
161
184
var generator = Generator ( corpus: corpus)
162
- let phrase = generator. phrase ( adjectives: adjectives, nouns: nouns)
163
185
164
- await output. append ( phrase)
186
+ do {
187
+ let phrase = try generator. phrase ( adjectives: adjectives, nouns: nouns)
188
+ await output. append ( phrase)
189
+ } catch {
190
+ await output. append ( error, errorText: " Could not generate phrase: \( error) " )
191
+ }
165
192
}
166
193
}
0 commit comments