I am trying to work out a simple function to capture typos, e.g:
"Westminister15"
"Westminister15London"
"23Westminister15London"
after fixating:
["Westminister", "15"]
["Westminister", "15", "London"]
["23", "Westminister", "15", "London"]
First attempt:
def fixate(query):
digit_pattern = re.compile(r'\D')
alpha_pattern = re.compile(r'\d')
digits = filter(None, digit_pattern.split(query))
alphas = filter(None, alpha_pattern.split(query))
print digits
print alphas
result:
fixate("Westminister15London")
> ['15']
> ['Westminister', 'London']
However, I think this could be done more effectively, and I still get bad results when I try something like:
fixate("Westminister15London England")
> ['15']
> ['Westminister', 'London England']
Obviously it should enlist London
and England
separately, but I feel my function will get overly patched and theres a simpler approach
This question is somewhat equivalent to this php question