From 41c932e227084f2983dc72ab0cb9d7abb0cc16b1 Mon Sep 17 00:00:00 2001 From: Daniel Briskin Date: Sat, 31 Mar 2018 17:12:21 -0400 Subject: [PATCH] implemented matching algorithm priorities --- app.py | 51 +++++++++++++++++++++++----- migrations/versions/19e377378700_.py | 30 ++++++++++++++++ migrations/versions/526868857b1f_.py | 28 +++++++++++++++ user_db.py | 1 + 4 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 migrations/versions/19e377378700_.py create mode 100644 migrations/versions/526868857b1f_.py diff --git a/app.py b/app.py index 0f2e551..e619351 100644 --- a/app.py +++ b/app.py @@ -18,6 +18,12 @@ def xstr(s): return '' if s is None else str(s) +def common_interests(interests1, interests2): + interests_1 = [x.strip() for x in interests1.split(',')] + interests_2 = [x.strip() for x in interests2.split(',')] + common = list(set(interests_1).intersection(interests_2)) + return ','.join(map(str, common)) + @APP.route("/") def hello(): return "

Hello There!

" @@ -278,6 +284,7 @@ def user_checkin(gid): matched_id = -1 matched_name = "" matched_surname = "" + matched_topics = "" # g.user.lastCheckIn = datetime.datetime.utcnow # g.user.checkInLocation = None # DB.session.commit() @@ -287,16 +294,41 @@ def user_checkin(gid): if u.lastCheckIn is not None and time_now - u.lastCheckIn > datetime.timedelta(minutes=15): print (time_now - u.lastCheckIn) u.checkInLocation = None - elif u is not g.user and matched is None: # need to check that user is not matched - u.matchedUser = g.user.id - g.user.matchedUser = u.id + + for u in location.checkedInUsers: + if matched is not None: + break + common = common_interests(g.user.interests, u.interests) + if common is None: + continue + if u is not g.user and u.matchedUser is None: # need to check that user is not matched + matched = u + matchedTopics = common + + + for u in location.checkedInUsers: + if matched is not None: + break + if u is not g.user and u.matchedUser is None and u.occupation == g.user.occupation: matched = u - print ("matched") - print (matched) - print (matched.id) - matched_id = matched.id - matched_name = matched.name - matched_surname = matched.surname + matched_topics = g.user.occupation + + for u in location.checkedInUsers: + if matched is not None: + break + if u is not g.user and u.matchedUser is None: + matched = u + + if matched is not None: + matched.matchedUser = g.user.id + g.user.matchedUser = matched.id + if matched_topics is not "": + g.user.matchedTopics = matched_topics + matched.matchedTopics = matched_topics + matched_id = matched.id + matched_name = matched.name + matched_surname = matched.surname + if g.user not in location.checkedInUsers: location.checkedInUsers.append(g.user) print ("After") @@ -309,6 +341,7 @@ def user_checkin(gid): surname=matched_surname, selfMessage="", otherMessage="", + topics=matched_topics, ) diff --git a/migrations/versions/19e377378700_.py b/migrations/versions/19e377378700_.py new file mode 100644 index 0000000..7b481b2 --- /dev/null +++ b/migrations/versions/19e377378700_.py @@ -0,0 +1,30 @@ +"""empty message + +Revision ID: 19e377378700 +Revises: 526868857b1f +Create Date: 2018-03-31 16:36:24.905609 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '19e377378700' +down_revision = '526868857b1f' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('matchedTopics', sa.String(length=128), nullable=True)) + op.drop_column('users', 'matchedTopic') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('matchedTopic', sa.VARCHAR(length=128), autoincrement=False, nullable=True)) + op.drop_column('users', 'matchedTopics') + # ### end Alembic commands ### diff --git a/migrations/versions/526868857b1f_.py b/migrations/versions/526868857b1f_.py new file mode 100644 index 0000000..4b79014 --- /dev/null +++ b/migrations/versions/526868857b1f_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: 526868857b1f +Revises: a96d2d790f16 +Create Date: 2018-03-31 16:34:54.763736 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '526868857b1f' +down_revision = 'a96d2d790f16' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('matchedTopic', sa.String(length=128), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('users', 'matchedTopic') + # ### end Alembic commands ### diff --git a/user_db.py b/user_db.py index 8b9098d..52279e1 100644 --- a/user_db.py +++ b/user_db.py @@ -28,6 +28,7 @@ class User_DB(DB.Model): # Foreign Key creates a pointer to the matched user matchedUser = DB.Column(DB.Integer, ForeignKey('users.id')) matchedMessage = DB.Column(DB.String(256)) + matchedTopics = DB.Column(DB.String(128)) # result_all = DB.Column(JSON) # result_no_stop_words = DB.Column(JSON)