I'd like to generate all consonances of a given string.
Consonance is a stylistic literary device identified by the repetition of identical or similar consonants in neighboring words whose vowel sounds are different. (Wikipedia)
A consonant is a letter of an alphabet that denotes a consonant sound. The following table assimilates groups of English consonants, taking the following conveniences to keep things simple(istic):
- It ignores diagraphs ("sh", "ch", "th", etc.).
- It ignores vowels.
- It ignores "h", "y", "w", "x".
- It assumes that a given letter can only be a member of one consonants group. Thus, "c" is (randomly) put together with "s" and "z", and "g" with "j".
Let's also assume that an input that conforms to cases 2 & 3 is allowed and should simply be ignored. However, an input is invalid if it either conforms to case 1, or it breaks case 4 (see examples below).
So:
var consonants = [
['b', 'p'],
['c', 's', 'z'],
['d', 't'],
['f', 'v'],
['g', 'j'],
['k', 'q']
];
As an example, given the string "jedi"
, the output should be:
var consonances = ["gedi", "jeti", "geti"]
Note that "e" and "i" - the vowles (case no. 2) - are allowed as input.
Some other examples:
"btb" --> ["ptb", "pdb", "pdp", "bdb", "bdp", "btp", "ptp"]
"star" --> ["ctar", "ztar", "sdar", "cdar", "zdar"]
Invalid input:
- Diagraphs:
"show", "chair", "high", "the"
- Breaking case 4:
"sure", "cat", "good"
I'm hitting a wall in trying to find the way to approach it. I went through permutations questions as I guess they may be relevant here, but I don't see how to apply such a solution here.
I need an algorithm, but a full code solution would be nice, of course. I'll add here what I'm currently left with (JS code):
const irrelvant = ['a', 'e', 'i', 'o', 'u', 'h', 'y', 'w', 'x'];
function isConsonant(c) {
return !irrelvant.includes(c);
}
function getConsonants(c) {
let curConsonants = [];
consonants.every((group) => {
if (group.includes(c)) {
curConsonants = group;
};
return !curConsonants.length;
});
return curConsonants;
}