Skip to content

Instantly share code, notes, and snippets.

@naimurhasan
Created October 21, 2022 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naimurhasan/0369725bd4ff120f1046b3abf5cc4b27 to your computer and use it in GitHub Desktop.
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.
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))
@naimurhasan
Copy link
Author

Hira bhai originally wrote the method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment