Skip to content

Commit

Permalink
Merge pull request #14 from sameetandpotatoes/sameet/unify-rec-format
Browse files Browse the repository at this point in the history
Fix rec issues so that None loggees are allowed
  • Loading branch information
sameetandpotatoes committed Apr 23, 2019
2 parents 96b6a64 + 43a5853 commit cab3eb6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion backend/Procfile
@@ -1 +1 @@
web: sh -c 'python manage.py runserver 0.0.0.0:$PORT'
web: sh -c 'python manage.py migrate && python manage.py shell -c "exec(open(\"seed.py\").read(), globals())" && python manage.py runserver 0.0.0.0:$PORT'
29 changes: 12 additions & 17 deletions backend/api/recommender.py
Expand Up @@ -347,6 +347,8 @@ def recommendations_from_logs(logs, user_id):
small_talk, count_small_talk = _count_small_talk(logs)

for friend in reactions:
if friend is None:
continue
name = friend.name
total = sum(reactions[friend].values())

Expand Down Expand Up @@ -469,7 +471,7 @@ def rolling_disposition_by_friend(logs_week, recs_week):

counts = _count_all(preceding_logs)
for person, count in counts.items():
rec_obj = next((x for x in rec if x.about_person_id == person.id), None)
rec_obj = next((x for x in rec if person != None and x.about_person_id == person.id), None)
if not rec_obj:
continue

Expand All @@ -496,6 +498,8 @@ def rolling_disposition_by_friend(logs_week, recs_week):
counts = _count_all(all_logs)

for person, count in counts.items():
if person == None:
continue
row = []
# Row and then date
row.extend(_aggregate_and_normalize(count, reacc_keys))
Expand Down Expand Up @@ -542,16 +546,7 @@ def recommendations_from_ml(logs, user_id, from_dt=None, to_dt=None):
has_recs = check_recs_now(recs_week)
rec_list = []
if has_recs:
for rec in has_recs:
rec_dict = dict(
recommend_person=user_id,
rec_type=rec.rec_typ,
recommendation=rec.recommendation,
rec_description=rec.rec_description,
)
rec_list.append(rec_dict)
return rec_list

return has_recs
ml_recs = generate_ml_recommendations(logs_by, recs_week)
for rec_enum, english_label, person_id in ml_recs:
person = models.Friend.objects.filter(id=person_id).first()
Expand All @@ -564,8 +559,8 @@ def recommendations_from_ml(logs, user_id, from_dt=None, to_dt=None):
recommendation="Try acting more positive with {}".format(person.name),
rec_description=ML_POS_REC_DESC.format(english_label),
)
_create_and_save_recommendation(positive_ml_rec, friend_id=person_id)
rec_list.append(positive_ml_rec)
rec = _create_and_save_recommendation(positive_ml_rec, friend_id=person_id)
rec_list.append(rec)

elif rec_enum == 'NE':
neg_ml_rec = dict(
Expand All @@ -574,17 +569,17 @@ def recommendations_from_ml(logs, user_id, from_dt=None, to_dt=None):
recommendation="Try a surface-level relationship with {}".format(person.name),
rec_description=ML_NEG_REC_DESC.format(english_label),
)
_create_and_save_recommendation(neg_ml_rec, friend_id=person_id)
rec_list.append(neg_ml_rec)
rec = _create_and_save_recommendation(neg_ml_rec, friend_id=person_id)
rec_list.append(rec)
else:
avoid_ml_rec = dict(
recommend_person=user_id,
rec_type=rec_enum,
recommendation="Try avoiding {}".format(person.name),
rec_description=ML_AVOID_REC_DESC.format(english_label),
)
_create_and_save_recommendation(avoid_ml_rec, friend_id=person_id)
rec_list.append(avoid_ml_rec)
rec = _create_and_save_recommendation(avoid_ml_rec, friend_id=person_id)
rec_list.append(rec)

return rec_list

Expand Down
5 changes: 2 additions & 3 deletions backend/api/views.py
Expand Up @@ -421,8 +421,8 @@ def recommendation(request):
if feedback_count < ML_SPLIT_THRESHOLD:
recs = recommender.recommendations_from_logs(logs, user_id)
else:
recs = recommender.recommendations_from_ml(logs, user_id, from_dt=json_body['from'], to_dt=json_body['to'])

recs = recommender.recommendations_from_ml(logs, user_id, from_dt=json_body.get('from'), to_dt=json_body.get('to'))
safe_recs = dict(
data=[
{
Expand All @@ -435,7 +435,6 @@ def recommendation(request):
for rec in recs
]
)

return JsonResponse(safe_recs, status=200)

elif request.method == 'POST':
Expand Down

0 comments on commit cab3eb6

Please sign in to comment.