Vowel-permuted Anagrams: Words like Texas/taxes, where only vowels have switched positions.

posted 2014-Sep-16

Based on a case-insensitive test of 235,886 “words” found in /usr/share/dict/words on OS X.


The Ruby code used to create the list:

require 'set'
VOWELS = Set.new(%w[a e i o u y])
FOUND  = Set.new # Used to prevent duplicates

module Enumerable
  def indices
    map.with_index{ |o,i| yield(o) ? i : nil }.compact
  end
end

words = File.readlines('/usr/share/dict/words').map(&:chomp).map(&:downcase)
all = Set.new(words)
i = 0
words.each.with_index do |word,n|
  next if FOUND.include?(word)
  matching = [word]
  # Each vowel in its own chunk (unique id), but all consonants strung together
  parts = word.chars.chunk{ |c| VOWELS.include?(c) ? (i+=1) : false }.to_a
  idxs  = parts.indices(&:first)         # Indices of vowels amongst the chunks
  parts = parts.map(&:last).map(&:join)  # Get rid of house-keeping parts
  vowls = parts.values_at(*idxs)         # Get all the vowels
  vowls.permutation.each do |permuted|   # Create unique permutations
    idxs.zip(permuted).each{ |i,v| parts[i]=v }   # Shove permuted vowels in
    permuted = parts.join                         # …and make them into a word
    if permuted!=word && all.include?(permuted) && !FOUND.include?(permuted)
      matching << permuted # Add this permutation to the matches
      FOUND    << permuted # Prevent us from checking this word later on.
    end
  end
  puts matching.join('') unless matching.length==1
end
net.mind details other résumé contact
Phrogz.net