python - How to improve performance of this recursive function? -
i'm trying write function search str substr, taking account different possibilities write weird letters, such æ, ø, å in danish language. example can search 'Ålborg' , function return true if there is, 'aalborg' in str.
the function below works, performance unbearable. recommend improve performance?
def danish_tolerating_search(substr, str): '''figure out if substr in str, taking account possible deviations in writing letters æ, ø, å. æ <-> ae ea ø <-> oe o å <-> aa o ''' # normalize input substr = substr.lower().replace('aa',u'å') str = str.lower() # normalized recursive search # todo fix perfomance def s(substr, str): if str.find(substr) >= 0: return true if substr.find(u'æ') >= 0: if s(substr.replace(u'æ','ae', 1), str): return true elif s(substr.replace(u'æ', 'a', 1), str): return true elif s(substr.replace(u'æ','ea', 1), str): return true if str.find(u'æ') >= 0: if s(substr, str.replace(u'æ','ae', 1)): return true elif s(substr, str.replace(u'æ', 'a', 1)): return true elif s(substr, str.replace(u'æ','ea', 1)): return true if substr.find(u'ø') >= 0: if s(substr.replace(u'ø','oe', 1), str): return true elif s(substr.replace(u'ø', 'o', 1), str): return true if str.find(u'ø') >= 0: if s(substr, str.replace(u'ø','oe', 1)): return true elif s(substr, str.replace(u'ø', 'o', 1)): return true if substr.find(u'å') >= 0: if s(substr.replace(u'å','aa', 1), str): return true elif s(substr.replace(u'å', 'a', 1), str): return true elif s(substr.replace(u'å', 'o', 1), str): return true if str.find(u'å') >= 0: if s(substr, str.replace(u'å','aa', 1)): return true elif s(substr, str.replace(u'å', 'a', 1)): return true elif s(substr, str.replace(u'å', 'o', 1)): return true return false return s(substr, str)
i think should eliminate recursion altogether. instead of doing find
, replace
, could, example, decide upon "normal form" of input strings, convert them accordingly (i.e. replace "ambiguous" characters) , simple
return substring in string_
note don't need call find
, replace
together, latter sufficies. if search string isn't found, replace won't replace anything.
Comments
Post a Comment