Code Style Review:
- Use of Redundant variables: I cannot find any use for
max
in your
script. Remove it
- Better naming:
jw
could be better described as jw_score
. Max is
built_in name, don't use it.
- Unnecessary
enumerate
. You are not using index in inner loop. Remove
enumerate.
- I am assuming
confidence
is global scope. Bad practice. Pass
confidence value as argument. If you must use confidence as global
constant, declare it as CONFIDENCE (caps indicate constant)
- Since you are inserting zero as initial value for
temp_list
there are better ways to achieve that instead of appending zero in loop.
- Use
item_data
while iterating data, and vice-versa. Your naming
convention right now is confusing
Code Design Review
I am having hard time understanding what you wanted to return. Right now, if any of strings for data matches target, your temp_list will have a value for it. But it will have value for "last_matched" only.
If you only care about any good match, then as soon as any value match, break out from inner loop, and your average running time should reduce drastically:
def get_jw_match(data, target, confidence):
"""Uses the jaro-winkler algorithm to match strings(names) from the target(the set from which you want to find matches) to the data(the set for which you are trying to find matches."""
score_list = [0] * len(target)
for i, item_target in enumerate(target):
for item_data in data:
jw_score = jellyfish.jaro_winkler(item_data, item_target)
if (jw_score > confidence):
score_list[i] = jw_score
break
return score_list
Now you would know for which elements in target
you have good match, but you don't know any element in data for which you have good match. For that, instead of assigning score, you can assign index for data.
Also, you can then ditch list, and use dictionaries:
def get_jw_match(data, target, confidence):
"""Uses the jaro-winkler algorithm to match strings(names) from the target(the set from which you want to find matches) to the data(the set for which you are trying to find matches."""
score_dict = dict()
for i, item_target in enumerate(target):
for j, item_data in enumerate(data):
jw_score = jellyfish.jaro_winkler(item_data, item_target)
if (jw_score > confidence):
score_dict[i] = j
break
return score_dict
Advantage of this is that you can create reverse dict also.
Which also means, that you should be able to swap inner-outer loops reducing time further. (In general, use inner loop for bigger data, when it short circuit, you save much more time)
If I think, there might be further time-reduction optimizations, but for practical purposes, this would shorten your average running time by magnitude.