Substituting from another file

I have a textfile with a long text, and a wordlist with words both in
letters and phonetic notation. I want to substitute all the words in the
text with their phonetic spelling from the wordlist.

The wordlist entries look like this:
PANELED P AE1 N AH0 L D
PANELING P AE1 N AH0 L IH0 NG
etc.

What I have come up with so far:

f = File.open(“textfile”,“r+”)
d = File.open(“dictionary”)
g = File.open(“outputfile”,“r+”)
s = IO.read(“textfile”)
w = IO.read(“dictionary”)
s.gsub!(/.+? /) {|match| w.match($1)}
g.write(s)
f.close
d.close

This seems to enter an infinite loop.