5 ms·
I like it! Here is a recommendation engine in < 20 lines of python to go along your search engine (if you keep session logs of clicked urls). def build_reco
by maaaaattttt 3y ago
I like it!
Here is a recommendation engine in < 20 lines of python to go along your search engine (if you keep session logs of clicked urls).
def build_recommendations(logs: List[List[str]], window_size: int = 10,
max_recommendations_per_url: int = 50) -> dict:
recommendations = {}
for session in logs:
for i in range(0, len(session)-1):
url_id = session[i] # reference element
window = session[i+1 : i+window_size] # sliding window
recommendations[url_id] = recommendations.get(url_id, {})
for pos, next_link in enumerate(window):
weight = window_size - pos # elements in the window get decreasing weight proportional to their distance from the reference element
recommendations[url_id][next_link] = recommendations[url_id].get(next_link, 0)
recommendations[url_id][next_link] += weight
for url_id, link_recommendations in recommendations.items():
# sort and truncate the recommendations
recommendations[url_id] = dict(sorted(link_recommendations.items(), key=lambda item: item[1], reverse=True)[:max_recommendations_per_url])
return recommendations
recommendations = build_recommendations(your_logs)
print(list(recommendations[some_url_id].keys())) # gives an ordered list of the recommended url_ids for the given url_id
With some tweaking (mix typed queries and clicked urls in the logs you feed) you can get a spellcheck suggestion out of it as well :)