MEDIAL_CLUSTERS = %w(kk ht hty lk ld ll lm lp lkw lt lv lw ly mb mm mn mp my nk nd ng ngw nn nkw nt nty nw ny ps pt kw rk rd rm rn rkw rr rt rty rs rw ry sk skw ss st sty sw ts tt tw ty ks).sort MEDIAL_CONSONANTS = %w(t p k kw n m l r v f s h hy y).sort INITIAL_CONSONANTS = (MEDIAL_CONSONANTS + %w(hl hr hw)).sort VOWELS = %w(a e i o u).sort VOWEL_CLUSTERS = %w(ea ia ie io oa uo).sort LONG_VOWELS = %w(ai au oi ui eu á é í ó ú).sort FINAL_CONSONANTS = %w(n s l r t).sort def print_all(*pattern) pattern.first.product(*pattern.drop(1)) do |segments| word = segments.join puts word unless invalid? word end end INVALID_COMBOS = /w[uúoó]|y[ií]|twi|hyo|hyu|[fv][uú]/ def invalid?(word) word =~ INVALID_COMBOS || count_rares(word) > 1 end RARE_COMBOS = /kk|ht|hty|lk|ll|lm|lp|lkw|lt|lv|lw|ly|mm|mn|my|nk|nn|nty|ny|pt|rk|rd|rm|rn|rkw|rr|rt|rty|rs|sk|skw|ss|sty|sw|tt|tw|ty|uo|eu|hl|hr|hw|hy|t$|s$|r$|[fvh][oó]/ def count_rares(word) word.scan(RARE_COMBOS).length end print_all(VOWELS, FINAL_CONSONANTS) print_all(INITIAL_CONSONANTS, LONG_VOWELS) print_all(VOWELS, MEDIAL_CLUSTERS, VOWELS) print_all(VOWELS, MEDIAL_CLUSTERS, VOWEL_CLUSTERS) print_all(INITIAL_CONSONANTS, LONG_VOWELS, MEDIAL_CONSONANTS, VOWELS) print_all(INITIAL_CONSONANTS, VOWELS, MEDIAL_CLUSTERS, VOWELS) print_all(INITIAL_CONSONANTS, VOWELS, MEDIAL_CLUSTERS, VOWEL_CLUSTERS) print_all(LONG_VOWELS, MEDIAL_CONSONANTS, VOWELS) print_all(LONG_VOWELS, MEDIAL_CONSONANTS, VOWEL_CLUSTERS) print_all(VOWELS, MEDIAL_CLUSTERS, VOWELS, FINAL_CONSONANTS) print_all(VOWELS, MEDIAL_CLUSTERS, VOWEL_CLUSTERS, FINAL_CONSONANTS) print_all(INITIAL_CONSONANTS, VOWELS, MEDIAL_CLUSTERS, VOWELS, FINAL_CONSONANTS) print_all(INITIAL_CONSONANTS, VOWELS, MEDIAL_CLUSTERS, VOWEL_CLUSTERS, FINAL_CONSONANTS) print_all(LONG_VOWELS, MEDIAL_CONSONANTS, VOWELS, FINAL_CONSONANTS)