Skip to content

Instantly share code, notes, and snippets.

@mwerner
Last active June 14, 2022 19:44
Show Gist options
  • Save mwerner/f2443854178b4c0507b03ab9e72c5300 to your computer and use it in GitHub Desktop.
Save mwerner/f2443854178b4c0507b03ab9e72c5300 to your computer and use it in GitHub Desktop.
Kangaroo Word Finder

Kangaroo Word Finder

This application looks through a word list and compares all other words in the list to find which words are found in other words

Kangaroo Word: A word that contains its synonym within it (ex: rambunctious & raucous: RAmbUnCtiOUS)

Joey Word: A word that is a synonym with another word it’s found within

Requirements to run:

  • Your wordlist kept at ./words.txt
  • Joey words (the words found inside the other word) must be 5 characters or longer (just so it's interesting)
  • Joey words can't be contiguously within a kangaroo word ex: "underground: under - ground"
  • Doesn't currently find synonyms :(

Usage

> irb
$ load './app.rb'
$ app = App.new
$ app.run
class App
attr_reader :words, :joeys, :matches
DICT = File.readlines("/usr/share/dict/words").map { |w| w.chomp.downcase }.uniq
WORDS = File.readlines("./words.txt").map { |w| w.chomp.downcase }.uniq
def initialize(source: WORDS)
@matches = {}
@words = source.select{|word| word.length >= 5 }
@joeys = WORDS.select{|word| word.length >= 4 }
end
def run
words.each do |kanga|
printf("#{kanga}, ")
joeys.select{|word| word.length >= 5 }.each do |roo|
next unless qualify?(kanga, roo) && kangaroo?(kanga, roo)
@matches[kanga] ||= []
@matches[kanga] << roo
end
end
puts
end
def present(results = matches)
results.each_pair do |match, joeys|
puts "#{match}:"
puts " #{joeys.join(', ')}"
end
nil
end
def kangaroo?(parent, child)
parent_letters = parent.split('')
child_letters = child.split('')
(parent_letters & child_letters).join('') == child &&
!contained_contiguously?(parent_letters, child_letters) # Can't contain the word in it's entirety
end
private
def qualify?(parent, child)
child.length >= 4 &&
parent.length >= 5 &&
(parent.length - 2) > child.length
end
def contained_contiguously?(parent_letters, child_letters)
ends_at = parent_letters.index(child_letters.last)
starts_at = parent_letters.index(child_letters.first)
return false if ends_at.nil? || starts_at.nil?
(ends_at - starts_at) < child_letters.length
end
end
community:
count, county
president:
print
research:
reach
relationship:
latin, ratio
computer:
outer
administration:
admit
population:
latin, plain
performance:
roman
whatever:
water
particularly:
party, partly
character:
chart
environmental:
vital
financial:
final
traditional:
radio, train, trail
scientist:
scent
agreement:
agent
responsibility:
reply
professional:
final
southern:
other, outer
relation:
latin, ratio
directly:
dirty
immediately:
medal
absolutely:
about
conversation:
cover, convert
completely:
comply
researcher:
reach
argument:
argue, agent
university:
unity
distance:
dance
tradition:
radio, train
communication:
count
contribute:
tribe
protection:
protein
principle:
price
chairman:
chain, charm
background:
around
slightly:
sight
reaction:
ratio
location:
latin
participate:
price
otherwise:
others
software:
stare
scientific:
scent
impossible:
impose
presidential:
print, rental
teaching:
thing
regulation:
region, latin, ratio, regain
thinking:
thing
comfortable:
cable
significantly:
signal
combination:
combat
entirely:
entry
judgment:
judge
necessarily:
early, nearly
specifically:
special, specify
experiment:
print
unfortunately:
forty
characteristic:
chart
virtually:
vital
explanation:
plant
personality:
penalty
assistance:
since
observation:
brain
creation:
ratio
graduate:
grade
purchase:
phase, purse
regarding:
regain
investigator:
investor
substance:
stance
volunteer:
voter, outer
complaint:
plant, paint
championship:
chain, chaos
approximately:
prime
breakfast:
breast, beast
reputation:
retain
supporter:
sport
administrator:
admit
grandmother:
grade, grant
constantly:
costly
preparation:
ratio
elementary:
entry
diversity:
dirty
valuable:
value
extraordinary:
train
clothing:
cling
furniture:
unite
cooperation:
opera
communicate:
count, unite
stranger:
stage
strategic:
tragic
complicated:
plate
champion:
chain
tremendous:
trend
politically:
policy
considerable:
ideal
external:
extra
headquarters:
heart
controversy:
convey
characterize:
chart
prominent:
point, print
significance:
since
grandfather:
grade, grant
celebration:
brain, latin
appointment:
point
friendship:
fresh
scholarship:
sharp, solar
reservation:
ratio
publisher:
pulse
sophisticated:
spite
controversial:
trial
sufficient:
scent
narrative:
native
provider:
prove, pride
cholesterol:
chest
differently:
dirty
medication:
media
absolute:
about
operating:
eating
province:
price, prove
permission:
person, prison
repeatedly:
ready, reply
discourse:
score
attribute:
tribe
dramatically:
daily
laughter:
later
secondary:
scary
experimental:
print
midnight:
might
correlation:
latin
humanity:
unity
administrative:
admit, admire
inspector:
sector, insect
inspection:
insect
consistently:
costly
broadcast:
boast
uncomfortable:
cable, unable, uncle
radiation:
radio
replacement:
plant, react
administer:
admit
particle:
price
traditionally:
radio, train, trail
respectively:
reply
profound:
pound, proud, round
astronomer:
stone
complexity:
comply
sidewalk:
ideal
pleasant:
least, plant
theoretical:
trial
altogether:
alter
counterpart:
outer
historically:
story, history
identical:
ideal
residential:
rental
provided:
prove, pride
guidance:
guide
exploration:
explain, latin, export, plain
sunlight:
night, sight
accounting:
count
productive:
produce, prove, route
popularity:
party
cooperate:
opera
shortage:
stage, shore
credibility:
credit
thoroughly:
truly
ancestor:
actor
collaboration:
latin, cabin
tropical:
trial, topic
revelation:
latin, ratio
alongside:
alone, aside
fascinating:
faint
discourage:
score
documentary:
entry
conspiracy:
spray
functional:
until
cooperative:
opera
practitioner:
action, ratio
dissolve:
solve
accomplishment:
client, alien
fortunately:
fortune, forty
scramble:
scale, cable
equivalent:
equal
supplier:
super
consequently:
costly
franchise:
raise
mentally:
metal
partially:
party, partly
placement:
plant
grandchild:
ranch
reportedly:
reply
columnist:
count
speculation:
section, latin, plain
swimming:
swing
outsider:
outer
desperately:
spray
breathing:
bring, brain, being, rating, eating
supportive:
sport
compliance:
plane
deliberately:
dirty, delay
shareholder:
shared
comparable:
compare, cable, marble
considerably:
ideal
similarity:
smart
sprinkle:
spine
mortality:
moral
conversion:
cover
blessing:
being
grandparent:
grade, grant, grape
supermarket:
speak
privately:
rival
fraction:
ratio
routinely:
route
chemistry:
chest
overnight:
right, eight
fragment:
agent, frame
policeman:
ocean
suitable:
suite
graduation:
radio, ratio, grain
rehabilitation:
habit
steadily:
steal
youngster:
outer
accountability:
until, count, county
ironically:
irony
magnetic:
magic
devastating:
eating
articulate:
article
importantly:
moral, imply
accusation:
action
straighten:
stage, train, strain
compelling:
cling, coming
honestly:
honey
sustainable:
table, stable
plaintiff:
plant, paint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment