in reply to Phone number to word conversion

did this (for fun) a long time ago when was working with a telco. it was a unix/linux one liner in shell...without reproducing it: map each digit to a regex for that position that is the set of corresponding letters, e.g. abc if digit is 2 or whatever. then grep the entire regex against /etc/words (on linux and most unixes) example 96753 can become the word WORLD
grep "^[wxyz][mno][pqrs][jkl][def]$" /etc/words
obviously you could do the same thing in perl.
the hardest line to type correctly is: stty erase ^H

Replies are listed 'Best First'.
Re^2: Phone number to word conversion
by oko1 (Deacon) on Nov 12, 2010 at 03:04 UTC

    That doesn't work unless the entire string matches a word. So, if you had a number (to use your example) of 967-5355, "world" would be a valid substring within it - but your "grep" line, above, wouldn't show it. Which was the whole reason for my initial post. :)


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
      if you also want substring matching..merely remove the start and end anchors in the regex. if you instead want all permutations of the number (digits in any order) mapping onto words, it's a little more complicated.
      the hardest line to type correctly is: stty erase ^H
        in the end you will find that you have to come up with a very fine specification for what is and isn't acceptable during your mapping, as those choices will substantially influence technique. for example you could also want any number of letters in any permutation, as long as there's at least one or more of a particular digit. The digit 1 could be used as a wildcard, etc, etc. The most straightforward case--what does my full phone number spell as an entire word in that sequence--is a very easy regex.
        the hardest line to type correctly is: stty erase ^H

        > if you also want substring matching..merely remove the start and end anchors in the regex.

        I'm sorry, you seem to have conflated the string and the regex. That won't work; it's equivalent to saying that /foobar/ will match "ooba" because there aren't any anchors in it. The problem is quite a bit more complicated than that; please take a look at both the specification in my original post and JavaFan's solution, which addresses it perfectly.


        --
        "Language shapes the way we think, and determines what we can think about."
        -- B. L. Whorf