Advertisement
alemadi

Untitled

Oct 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. '''
  2. Please help with integrating the ```update_name``` and ```update_number``` into the ```shape_element``` function.
  3.  
  4. Note: all of these functions work fine independently
  5.  
  6. '''
  7.  
  8.  
  9. def phone(osm_file):
  10.     '''
  11.    Gets phone numbers from the XML file
  12.    '''
  13.     # open file
  14.     osm_file = open(file_path, "r")
  15.     num = []
  16.     # reach phone num
  17.     for event, elem in ET.iterparse(osm_file, events=("start",)):
  18.         if elem.tag == "node":
  19.             for tag in elem.iter("tag"):
  20.                 if tag.attrib['k'] == 'phone':
  21.                     num.append(tag.attrib['v'])
  22.     return num
  23.                
  24. phone(osm_file)
  25.  
  26. def std_phone(num):
  27.     '''
  28.    Standardizes phone number
  29.    '''
  30.     fixed_num = []
  31.     for i in num:
  32.         num = i
  33.    
  34.         # Remove anything that is not a number
  35.         num = re.sub('[^0-9]+', '', num)
  36.         # add dashes to 10 digit nums
  37.         if re.match(r'^\d{10}$', num) is not None:
  38.             num = num[:3] + '-' + num[3:6]+ '-' + num[6:]
  39.             fixed_num.append(num)
  40.         # add dashes to 11 digit nums
  41.         elif re.match(r'^\d{11}$', num) is not None:
  42.             num = num[0] + '-' + num[1:4] + '-' + num[4:7] + '-' +num[7:]
  43.             fixed_num.append(num)
  44.  
  45.  
  46. def update_name(name, mapping):
  47.     '''
  48.    1.Removes APT and house numbers from the street address
  49.    2.Using a regex function to search for problematic names,
  50.    then modifies it according to the given mapping.
  51.    
  52.    Args:
  53.        name(string): potential problematic street name
  54.        mapping(dict): the desired outcome of street suffixes
  55.        
  56.    Returns:
  57.        name(string): All street names (fixed and non-fixed)
  58.        '''
  59.    
  60.  
  61.     if '#' in name:
  62.         name = name.split('#',-1)[0].strip()
  63.        
  64.     if ',' in name:
  65.         name = name.split(',', 1)[0].strip()
  66.    
  67.    
  68.        
  69.     m = street_type_re.search(name)
  70.    
  71.    
  72.     if m:
  73.         street_type = m.group()
  74.         if street_type not in expected: # This is somewhat redundant, but it acts as a fail-safe
  75.             if street_type in mapping:
  76.    
  77.                 name= re.sub(street_type_re, mapping[street_type] , name)
  78.        
  79.  
  80.     return name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement