Created
October 21, 2022 12:34
-
-
Save naimurhasan/0369725bd4ff120f1046b3abf5cc4b27 to your computer and use it in GitHub Desktop.
search through each words in db and get ids, then in script sort the id by max matching word.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getMaxMatchedIds(rmc, words, md): | |
matched_ids = [] | |
matched_id_words = {} | |
for word in words: | |
for id in md[word]: | |
match_count = 1 | |
match_words = [word] | |
if id not in matched_ids: | |
for key in set(md) - set([word]): | |
if id in md[key]: | |
match_count += 1 | |
match_words.append(key) | |
if match_count>=rmc: | |
matched_ids.append(id) | |
matched_id_words[id] = match_words | |
matched_ids = sorted(matched_ids, key=lambda x: len(matched_id_words[x]), reverse=True) | |
return matched_ids | |
w = ['a', 'b', 'c', 'd'] | |
mdict = { | |
'a': [2,], | |
'b': [3, 9, 11, 1], | |
'c': [5, 1,3], | |
'd': [13, 14, 8, 2,3], | |
} | |
rmcount = 7 | |
print(getMaxMatchedIds(rmcount, w, mdict)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hira bhai originally wrote the method.