python - python3 fuzzywuzzy not returning index value of the array -
i trying modify fuzzywuzzy
library. module process returns score
, array element. want return index of element along group of score,item,index.
here tried:
#!/usr/bin/env python # encoding: utf-8 fuzzywuzzy import fuzz fuzzywuzzy import utils import heapq import logging functools import partial default_scorer = fuzz.wratio default_processor = utils.full_process def extractwithoutorder(query, choices, processor=default_processor, scorer=default_scorer, score_cutoff=0): def no_process(x): return x try: if choices none or len(choices) == 0: raise stopiteration except typeerror: pass if processor none: processor = no_process processed_query = processor(query) if len(processed_query) == 0: logging.warning(u"applied processor reduces input query empty string, " "all comparisons have score 0. " "[query: \'{0}\']".format(query)) # don't run full_process twice if scorer in [fuzz.wratio, fuzz.qratio, fuzz.token_set_ratio, fuzz.token_sort_ratio, fuzz.partial_token_set_ratio, fuzz.partial_token_sort_ratio, fuzz.uwratio, fuzz.uqratio] \ , processor == utils.full_process: processor = no_process # process query once instead of every choice if scorer in [fuzz.uwratio, fuzz.uqratio]: pre_processor = partial(utils.full_process, force_ascii=false) scorer = partial(scorer, full_process=false) elif scorer in [fuzz.wratio, fuzz.qratio, fuzz.token_set_ratio, fuzz.token_sort_ratio, fuzz.partial_token_set_ratio, fuzz.partial_token_sort_ratio]: pre_processor = partial(utils.full_process, force_ascii=true) scorer = partial(scorer, full_process=false) else: pre_processor = no_process processed_query = pre_processor(processed_query) count = -1 try: # see if choices dictionary-like object. key, choice in choices.items(): count = count + 1 processed = pre_processor(processor(choice)) score = scorer(processed_query, processed) if score >= score_cutoff: yield (choice, score, key,count) except attributeerror: # it's list; iterate on it. choice in choices: count = count + 1 processed = pre_processor(processor(choice)) score = scorer(processed_query, processed) if score >= score_cutoff: yield (choice, score,count) def extract(query, choices, processor=default_processor, scorer=default_scorer, limit=5): sl = extractwithoutorder(query, choices, processor, scorer) return heapq.nlargest(limit, sl, key=lambda i: i[1]) if limit not none else \ sorted(sl, key=lambda i: i[1], reverse=true)
when tried implement it, result showing fuzzywuzzy.
import process p box=['ness', 'apple','banana','carrot','duck','eagle','fish','gate','hitler'] p.extract('b',box) [('banana', 90), ('apple', 0), ('carrot', 0), ('duck', 0), ('eagle', 0)]
but expecting return is:
[('banana', 90, 2), ('apple', 0, 1), ('carrot', 0, 3), ('duck', 0, 4), ('eagle', 0, 5)]
kindly let me know suggestion.
Comments
Post a Comment