Skip to content

Commit 118b6ff

Browse files
committed
Ensure words not in the dictionary are at least 1 syllable
1 parent 0f96c88 commit 118b6ff

File tree

1 file changed

+28
-11
lines changed
  • src/discord_bot/april_fools_channel

1 file changed

+28
-11
lines changed

src/discord_bot/april_fools_channel/haiku.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::discord_bot::april_fools_channel::AprilFoolsChannel;
2+
use arpabet::phoneme::Phoneme;
3+
use arpabet::{load_cmudict, Polyphone};
14
use std::collections::BTreeSet;
25
use std::mem;
3-
use arpabet::{load_cmudict, Polyphone};
4-
use arpabet::phoneme::Phoneme;
5-
use crate::discord_bot::april_fools_channel::AprilFoolsChannel;
66

77
const INVALID_HAIKU: &str = "Not quite a haiku
88
Your rhythm is imperfect
@@ -46,8 +46,17 @@ impl AprilFoolsChannel for HaikuChannel {
4646
let syllable_count_2 = get_line_syllables(lines[1]);
4747
let syllable_count_3 = get_line_syllables(lines[2]);
4848

49-
if !syllable_count_1.contains(&5) || !syllable_count_2.contains(&7) || !syllable_count_3.contains(&5) {
50-
return Some(format!("{}\nSyllable counts are: {}, {}, {}", INVALID_HAIKU, display_syllable_count(&syllable_count_1), display_syllable_count(&syllable_count_2), display_syllable_count(&syllable_count_3)));
49+
if !syllable_count_1.contains(&5)
50+
|| !syllable_count_2.contains(&7)
51+
|| !syllable_count_3.contains(&5)
52+
{
53+
return Some(format!(
54+
"{}\nSyllable counts are: {}, {}, {}",
55+
INVALID_HAIKU,
56+
display_syllable_count(&syllable_count_1),
57+
display_syllable_count(&syllable_count_2),
58+
display_syllable_count(&syllable_count_3)
59+
));
5160
}
5261

5362
None
@@ -79,7 +88,10 @@ fn get_line_syllables(line: &str) -> BTreeSet<u32> {
7988
let mut next_result = BTreeSet::new();
8089

8190
for word in line.split(|char: char| !char.is_ascii_alphabetic() && char != '\'') {
82-
let word = word.trim_start_matches('\'').trim_end_matches('\'').to_ascii_lowercase();
91+
let word = word
92+
.trim_start_matches('\'')
93+
.trim_end_matches('\'')
94+
.to_ascii_lowercase();
8395
if word.is_empty() {
8496
continue;
8597
}
@@ -117,23 +129,28 @@ fn get_line_syllables(line: &str) -> BTreeSet<u32> {
117129
}
118130

119131
fn check_word_cmu(polyphone: &Polyphone) -> u32 {
120-
1.max(polyphone.iter().filter(|phoneme| matches!(phoneme, Phoneme::Vowel(_))).count() as u32)
132+
1.max(
133+
polyphone
134+
.iter()
135+
.filter(|phoneme| matches!(phoneme, Phoneme::Vowel(_)))
136+
.count() as u32,
137+
)
121138
}
122139

123140
fn check_word_simple(word: &str) -> u32 {
124141
let word = word.as_bytes();
125142
let mut syllables = 0;
126-
143+
127144
for i in 0..word.len() {
128145
let char = word[i];
129146
let prev_char = if i == 0 { b'_' } else { word[i - 1] };
130-
147+
131148
if is_vowel(char) && !is_vowel(prev_char) && (char != b'e' || i != word.len() - 1) {
132149
syllables += 1;
133150
}
134151
}
135-
136-
syllables
152+
153+
1.max(syllables)
137154
}
138155

139156
fn is_vowel(c: u8) -> bool {

0 commit comments

Comments
 (0)