|
| 1 | +use crate::discord_bot::april_fools_channel::AprilFoolsChannel; |
| 2 | +use arpabet::phoneme::Phoneme; |
| 3 | +use arpabet::{load_cmudict, Polyphone}; |
1 | 4 | use std::collections::BTreeSet; |
2 | 5 | use std::mem; |
3 | | -use arpabet::{load_cmudict, Polyphone}; |
4 | | -use arpabet::phoneme::Phoneme; |
5 | | -use crate::discord_bot::april_fools_channel::AprilFoolsChannel; |
6 | 6 |
|
7 | 7 | const INVALID_HAIKU: &str = "Not quite a haiku |
8 | 8 | Your rhythm is imperfect |
@@ -46,8 +46,17 @@ impl AprilFoolsChannel for HaikuChannel { |
46 | 46 | let syllable_count_2 = get_line_syllables(lines[1]); |
47 | 47 | let syllable_count_3 = get_line_syllables(lines[2]); |
48 | 48 |
|
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 | + )); |
51 | 60 | } |
52 | 61 |
|
53 | 62 | None |
@@ -79,7 +88,10 @@ fn get_line_syllables(line: &str) -> BTreeSet<u32> { |
79 | 88 | let mut next_result = BTreeSet::new(); |
80 | 89 |
|
81 | 90 | 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(); |
83 | 95 | if word.is_empty() { |
84 | 96 | continue; |
85 | 97 | } |
@@ -117,23 +129,28 @@ fn get_line_syllables(line: &str) -> BTreeSet<u32> { |
117 | 129 | } |
118 | 130 |
|
119 | 131 | 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 | + ) |
121 | 138 | } |
122 | 139 |
|
123 | 140 | fn check_word_simple(word: &str) -> u32 { |
124 | 141 | let word = word.as_bytes(); |
125 | 142 | let mut syllables = 0; |
126 | | - |
| 143 | + |
127 | 144 | for i in 0..word.len() { |
128 | 145 | let char = word[i]; |
129 | 146 | let prev_char = if i == 0 { b'_' } else { word[i - 1] }; |
130 | | - |
| 147 | + |
131 | 148 | if is_vowel(char) && !is_vowel(prev_char) && (char != b'e' || i != word.len() - 1) { |
132 | 149 | syllables += 1; |
133 | 150 | } |
134 | 151 | } |
135 | | - |
136 | | - syllables |
| 152 | + |
| 153 | + 1.max(syllables) |
137 | 154 | } |
138 | 155 |
|
139 | 156 | fn is_vowel(c: u8) -> bool { |
|
0 commit comments