|
import sublime |
|
import sublime_plugin |
|
import re |
|
|
|
""" |
|
|
|
Add this file to your Sublime Text 2 User folder, [...]/Sublime Text 2/Packages/User. |
|
To use in the command palette, add the following line to the user.sublime-commands file in the same folder: |
|
{ "caption": "Formalize Street Names", "command": "formalize_street_names" } |
|
|
|
""" |
|
|
|
street_patterns = [ |
|
# Full Street Extension |
|
# See below for Street and Drive and Lane |
|
(r'(?i)\baly\b\.?', 'Alley'), |
|
(r'(?i)\bave?\b\.?', 'Avenue'), |
|
(r'(?i)\brd\b\.?', 'Road'), |
|
(r'(?i)\bbl(vd)?\b\.?', 'Boulevard'), |
|
(r'(?i)\bte?rr?\b\.?', 'Terrace'), |
|
(r'(?i)\bplz\b\.?', 'Plaza'), |
|
(r'(?i)\bpl\b\.?', 'Place'), |
|
(r'(?i)\bpk\b\.?', 'Park'), |
|
(r'(?i)\bln\b\.?', 'Lane'), |
|
(r'(?i)\bexp?w?y\b\.?', 'Expressway'), |
|
(r'(?i)\bfr?wy\b\.?', 'Freeway'), |
|
(r'(?i)\bpkw?y\b\.?', 'Parkway'), |
|
(r'(?i)\btr?n?p(ke)?\b\.?', 'Turnpike'), |
|
(r'(?i)\bhwy?\b\.?', 'Highway'), |
|
(r'(?i)\bcir\b\.?', 'Circle'), |
|
(r'(?i)\bsq\b\.?', 'Square'), |
|
(r'(?i)\bbrg?\b\.?', 'Bridge'), |
|
(r'(?i)\bs/?r\b\.?', 'Service Road'), |
|
(r'(?i)\bwy\b\.?', 'Way'), |
|
|
|
# Directional Suffix |
|
# Not to match if the only thing before is "Avenue" |
|
(r'(?i)(?<!^Avenue) N\b[\.,]?', ' North'), |
|
(r'(?i)(?<!^Avenue) S\b[\.,]?', ' South'), |
|
(r'(?i)(?<!^Avenue) E\b[\.,]?', ' East'), |
|
(r'(?i)(?<!^Avenue) W\b[\.,]?', ' West'), |
|
|
|
# Special rules for street and drive, which are also abbrvs for honorifics |
|
# And La, which may occur in names (e.g. La Guardia Place) |
|
# Only replaced if at the end of the input, or followed by a directional suffix |
|
# e.g. "Example Dr N." => "Example Drive North" |
|
(r'(?i)\bst\b\.?($| North| South| West| East| NW| NE| SE| SW)', r'Street\1'), |
|
(r'(?i)\bdr\b\.?($| North| South| West| East| NW| NE| SE| SW)', r'Drive\1'), |
|
(r'(?i)\bla\b\.?($| North| South| West| East| NW| NE| SE| SW)', r'Lane\1'), |
|
|
|
# Directional prefix without a space for numbered streets |
|
# Sadly common in NYC, e.g. W42 St |
|
(r'(?i)^N\.?(?=\d)', 'North '), |
|
(r'(?i)^S\.?(?=\d)', 'South '), |
|
(r'(?i)^E\.?(?=\d)', 'East '), |
|
(r'(?i)^W\.?(?=\d)', 'West '), |
|
|
|
# Directional prefix |
|
# Washington DC exception e.g "N Street" |
|
(r'(?i)^N\b[\.,]?(?! Str)', 'North'), |
|
(r'(?i)^S\b[\.,]?(?! Str)', 'South'), |
|
(r'(?i)^E\b[\.,]?(?! Str)', 'East'), |
|
(r'(?i)^W\b[\.,]?(?! Str)', 'West'), |
|
|
|
# Spelling out avenues, drives, roads and boulevards |
|
(r'(?i)\b1(st)?(?= (Aven|Driv|Road|Boul))', 'First'), |
|
(r'(?i)\b2(nd)?(?= (Aven|Driv|Road|Boul))', 'Second'), |
|
(r'(?i)\b3(rd)?(?= (Aven|Driv|Road|Boul))', 'Third'), |
|
(r'(?i)\b4(th)?(?= (Aven|Driv|Road|Boul))', 'Fourth'), |
|
(r'(?i)\b5(th)?(?= (Aven|Driv|Road|Boul))', 'Fifth'), |
|
(r'(?i)\b6(th)?(?= (Aven|Driv|Road|Boul))', 'Sixth'), |
|
(r'(?i)\b7(th)?(?= (Aven|Driv|Road|Boul))', 'Seventh'), |
|
(r'(?i)\b8(th)?(?= (Aven|Driv|Road|Boul))', 'Eighth'), |
|
(r'(?i)\b9(th)?(?= (Aven|Driv|Road|Boul))', 'Ninth'), |
|
(r'(?i)\b10(th)?(?= (Aven|Driv|Road|Boul))', 'Tenth'), |
|
(r'(?i)\b11(th)?(?= (Aven|Driv|Road|Boul))', 'Eleventh'), |
|
(r'(?i)\b12(th)?(?= (Aven|Driv|Road|Boul))', 'Twelfth'), |
|
|
|
# Adding numeral suffixes to streets and places |
|
(r'(?<=(11|12|13))(?= (Str|Ter|Lan|Pla|Ave|Dri|Roa|Bou))', 'th'), |
|
(r'(?<=1)(?= (Str|Ter|Lan|Pla|Ave|Dri|Roa|Bou))', 'st'), |
|
(r'(?<=2)(?= (Str|Ter|Lan|Pla|Ave|Dri|Roa|Bou))', 'nd'), |
|
(r'(?<=3)(?= (Str|Ter|Lan|Pla|Ave|Dri|Roa|Bou))', 'rd'), |
|
(r'(?<=[0456789])(?= (Str|Ter|Ave|Roa|Dri|Bou|Pla|Ter|Lan))', 'th'), |
|
|
|
(r'Ext\b[.,]?$', 'Extension') |
|
] |
|
|
|
|
|
def formalize(street): |
|
for pattern, replace in street_patterns: |
|
street = re.sub(pattern, replace, street) # flags=re.MULTILINE | re.IGNORECASE) |
|
return street |
|
|
|
|
|
class FormalizeStreetNamesCommand(sublime_plugin.TextCommand): |
|
'formalize street names' |
|
def run(self, edit): |
|
|
|
# select all |
|
if self.view.sel()[0].empty(): |
|
self.view.sel().add(sublime.Region(0, self.view.size())) |
|
|
|
for sel in self.view.sel(): |
|
newlines = [] |
|
|
|
# Split of the text of the selection |
|
lines = self.view.substr(sel).split("\n") |
|
|
|
for line in lines: |
|
newlines.append(formalize(line)) |
|
|
|
text = "\n".join(newlines) |
|
self.view.replace(edit, sel, text) |
|
|
|
print "Street Name Formalizer looked at", len(self.view.sel()), "selections" |