acclivity

pyDictionaryIntegers2Words

Jan 7th, 2022 (edited)
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. # Process a dictionary of data about books, finding all numeric data, and converting each number to the equivalent in words
  2. # e.g. change 'Fahrenheit 451' to 'Fahrenheit four hundred and fifty one'
  3. # by Mike Kerry (acclivity2@gmail.com)  7-Jan-2022
  4.  
  5. import re
  6.  
  7. cybc = {"BookNumber": (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21),
  8.         "BookNames": ('For Whom the Bell Tolls', 'Brave New World', 'Nineteen Eighty-Four',
  9.                       'The Sound and the Fury', 'Gone with the Wind', "Lady Chatterley's Lover",
  10.                       'The Hound of the Baskervilles', 'I Am Number 4', 'The Great Gatsby',
  11.                       'The War of the Worlds', 'The Lord of the Rings', 'Martin Eden', 'On the Road',
  12.                       "A Room of One's Own", 'Lord Jim', 'Manhattan Transfer', "Sophie's Choice",
  13.                       'The Catcher in the Rye', 'No Orchids For Miss Blandish',
  14.                       'The Origins of Totalitarianism The Burden of Our Time', 'Fahrenheit 451'),
  15.         "Pages": (None, 311, 328, 326, '1037 (first edition), 1024 (Warner Books Paperback)',
  16.                   None, None, 440, None, 287, 'original manuscripts, which total 9,250 pages',
  17.                   None, 320, 172, None, None, 562, 234, None, 704, 256),
  18.         "Dates": (1940, 1932, 1949, 1929, 1936, 1928, '1901–02', 2009, 1925, 1898, '1954–55',
  19.                   1909, 1957, 1929, 1900, 1925, 1979, 1951, 1939, 1951, 1953),
  20.         "Author": ('Ernest Hemingway', 'Aldous Huxley', 'George Orwell', 'William Faulkner',
  21.                    'Margaret Mitchell', 'D.H. Lawrence', 'Arthur Conan Doyle', 'Jobie Hughes, '
  22.                    'James Frey', 'F. Scott Fitzgerald', 'H.G. Wells', 'J.R.R. Tolkien',
  23.                    'Jack London', 'Jack Kerouac', 'Virginia Woolf', 'Joseph Conrad', 'John Dos Passos',
  24.                    'William Styron', 'J.D. Salinger', 'James Hadley Chase', 'Hannah Arendt',
  25.                    'Ray Bradbury')
  26.         }
  27.  
  28. # Replace this dummy function with your own integer-to-words function
  29. def num2words(n):
  30.     return "ANUMBER"
  31.  
  32. # This function processes all the data for one specific key of the dictionary
  33. def process_by_key(akey):
  34.     items = cybc[akey]              # extract the tuple of all the dictionary items for the given key
  35.     newitems = []                   # define an empty list that will hold the modified items
  36.     for item in items:
  37.         if isinstance(item, int):       # process integers
  38.             item = num2words(item)      # replace the integer item with the equivalent words
  39.  
  40.         elif isinstance(item, str):     # process strings
  41.             # Examine string items for any embedded integers
  42.             vlist = re.split('(\d+)', item)           # Split on integers, preserving everything else too
  43.             for x, part in enumerate(vlist):          # Examine every part of the split-up string
  44.                 if part.isdigit():                    # Look for parts that are digits only
  45.                     vlist[x] = num2words(int(part))   # Convert such parts to the word equivalence
  46.  
  47.             # Re-combine all the parts of the string item from vlist
  48.             item = "".join(vlist)
  49.         newitems.append(item)           # Add the converted (or unchanged) item to our new list
  50.     # re-insert the modified items into the dictionary
  51.     cybc[akey] = tuple(newitems)        # Convert our new list to a tuple and re-insert in the dictionary
  52.  
  53. process_by_key("BookNumber")            # Process all book numbers
  54. process_by_key("BookNames")             # Process all the book names
  55. process_by_key("Pages")                 # Process all the pages
  56. process_by_key("Dates")                 # Process all the dates
  57.  
  58. print(str(cybc))            # Dump out the modified dictionary
Add Comment
Please, Sign In to add comment