-1

I am trying to convert a random telephone number from a number into words using dictionaries using the following code:

dict_1 = {07: 'zero seven'}

dict_2 = {2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight'}

dict_3 = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}

def main():

    import re
    no_tel = raw_input("Enter a phrase: ")
    mat = re.search('(\d{10})', no_tel)
    a_no = mat.group(0)
    first_part = a_no[0:2]
    second_part = a_no[2:3]
    third_part = a_no[3:]

    it_works = False
    if dict_1.get(int(first_part)) != None:
        a_print = dict_1.get(int(first_part))
        it_works = True
    else:
        print "Error!"

    if dict_2.get(int(second_part)) != None:
        b_print = dict_2.get(int(second_part))
        it_works = True
    else:
        print "Error!"
    fourth_part = third_part[0]
    fifth_part = third_part[1]
    sixth_part = third_part[2]
    seventh_part = third_part[3]
    eighth_part = third_part[4]
    ninth_part = third_part[5]
    tenth_part = third_part[6]

    if dict_3.get(int(fourth_part)) != None:
        d_print = dict_3.get(int(fourth_part))
        it_works = True

    if dict_3.get(int(fifth_part)) != None:
        e_print = dict_3.get(int(fifth_part))
        it_works = True

    if dict_3.get(int(sixth_part)) != None:
        f_print = dict_3.get(int(sixth_part))
        it_works = True

    if dict_3.get(int(seventh_part)) != None:
        g_print = dict_3.get(int(seventh_part))
        it_works = True

    if dict_3.get(int(eighth_part)) != None:
        h_print = dict_3.get(int(eighth_part))
        it_works = True

    if dict_3.get(int(ninth_part)) != None:
        i_print = dict_3.get(int(ninth_part))
        it_works = True


    if dict_3.get(int(tenth_part)) != None:
        j_print = dict_3.get(int(tenth_part))
        it_works = True

    if it_works == True:
        telephone = " ". join([a_print, b_print, d_print, e_print, f_print, g_print, h_print, i_print, j_print])
        correct_no = re.sub ('(first_part)(second_part)(third_part)', telephone, no_tel)
        print correct_no
    else:
        print "Error!"


if __name__ == '__main__':
    main()

After running this program the output should be:

Enter a phrase: My phone no is 0734123456

which the above code should convert to ->

My phone no is zero seven two three one two three four five six

or any other phone no in this format.

Also how can the code be applied to a document containing telephone numbers? I do not exactly how to do it, read from text and then display the content with the telephone numbers substituted, I do not know.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
nickyd
  • 3
  • 1
  • 3
  • You haven't told us what isn't working. You need to post your traceback and tell us what's wrong. – Morgan Thrapp Aug 12 '15 at 17:54
  • Are you sure it shouldn't produce `'zero seven three four...'` from that number? – TigerhawkT3 Aug 12 '15 at 17:55
  • My apologies. First of all sorry for the mistype, it is "zero seven three four..." and second I put a print after the first split: print first_part print second_part print third_part and it works, the result being: 07 34 123456 . Then the conversion isn't made into words. Also I haven't found a method to read from text and exchange the numbers into words. BTW I am using Pycharm in Ubuntu. – nickyd Aug 13 '15 at 17:59

1 Answers1

4

Why not just:

def phone(number):
    numbers = ['zero', 'one', 'two', 'three', 'four',
               'five', 'six', 'seven', 'eight', 'nine']
    return ' '.join(numbers[c] for c in map(int, number))

Result:

>>> phone('0734123456')
'zero seven three four one two three four five six'
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97