Esempio n. 1
0
def test_cb_recommend():
    ''' Everytime client ask for recommendation will invoke this method, return a python object '''
    db_path = "./test.sqlite"
    # If db created then connect, otherwise, load test data.
    if os.path.exists(db_path):
        DBManager.init_db(db_name='test', is_echo=False)
    else:
        DBManager.init_db(db_name='test', is_echo=False)
        Loader.load(file_path='./tests/food.json')
        Loader.load(file_path='./tests/user.json')
        Loader.load(file_path='./tests/review.json')

    user_id = 20

    session = DBManager.get_session()
    food = pd.read_sql(session.query(Food).statement,
                       session.bind).rename(columns={'id': 'food_id'})
    review = pd.read_sql(
        session.query(UserRecommendationReview).filter(
            user_id == UserRecommendationReview.user_id).statement,
        session.bind)
    review['is_accept'] = np.where(review['is_accept'] == True, 1, 0)
    review = review.groupby([
        'food_id'
    ])['is_accept'].mean().reset_index().sort_values(by=["is_accept"],
                                                     ascending=False)

    final_recommendation = _recommend_food(review, food)
    print(final_recommendation)
def test_loader():
    DBManager.init_db(db_name='db4')

    Loader.load(file_path='./data/food.json')
    Loader.load(file_path='./data/user.json')

    Loader.traverse_database()
def test_recommend():
    ''' Everytime client ask for recommendation will invoke this method, return a python object '''
    db_path = "./test.sqlite"
    # If db created then connect, otherwise, load test data.
    if os.path.exists(db_path):
        DBManager.init_db(db_name='test', is_echo=False)
    else:
        DBManager.init_db(db_name='test', is_echo=False)
        Loader.load(file_path='./tests/food.json')
        Loader.load(file_path='./tests/user.json')
        Loader.load(file_path='./tests/review.json')

    # Setup session and tables
    session = DBManager.get_session()
    food = pd.read_sql(session.query(Food).statement,session.bind).rename(columns={'id': 'food_id'})
    user = pd.read_sql(session.query(User).statement,session.bind).rename(columns={'id': 'user_id'})
    review = pd.read_sql(session.query(UserRecommendationReview).statement,session.bind)

    # Data processing & prepare
    review['is_accept'] = np.where(review['is_accept']==True, 1, -1)
    review = review.groupby(['user_id','food_id'])['is_accept'].sum().reset_index()

    rating_pivot = review.pivot(index='user_id',columns = 'food_id',values='is_accept').fillna(0)

    rating_df = rating_pivot.as_matrix()
    user_ratings_mean = np.mean(rating_df, axis = 1)
    r_demeaned = rating_df - user_ratings_mean.reshape(-1, 1)

    # Matrix factorization
    U, sigma, Vt = svds(r_demeaned, k = 50)
    sigma = np.diag(sigma)

    prediction_all= np.dot(np.dot(U, sigma), Vt) + user_ratings_mean.reshape(-1, 1)
    prediction_df = pd.DataFrame(prediction_all, columns = rating_pivot.columns)

    # Recommend top 10 food that the user hasn't been recommended to
    recommendation_list = recommend_food(prediction_df, 130, food, review, 10)

    print(recommendation_list)

    result = session.query(Food).filter(Food.id.in_(recommendation_list)).all()
    food_map = {food.id: food for food in result}
    food_list = [food_map[id] for id in recommendation_list]

    for food in food_list:
        food.show_detail()
def test_init_project():
    random_db_name_seed = ''.join(
        [random.choice(string.ascii_lowercase) for _ in range(10)])
    DBManager.init_db(db_name='test_%s' % random_db_name_seed, is_echo=False)
Esempio n. 5
0
parser.add_argument('-t', '--traverse_database', dest='traverse_database', help='true or false, traverse_database and print all data')

# algorithm related args
parser.add_argument('-a', '--algorithm', dest='algorithm', help='selected used algorithm')
parser.add_argument('-p', '--user_proxy', dest='user_proxy', help='select a userProxy to evaluate algorithm, this argument will close the command line UI')

if __name__ == '__main__':
    args = parser.parse_args()
    parser.print_help()

    # init DB
    DB_NAME = args.connection_db or DBManager.DEFAULT_DB_NAME
    DB_IS_ECHO = True if args.echo_database in ('true', 'True') else False
    print('\n Init database : %s.sqlite' % DB_NAME)
    DBManager.init_db( 
        db_name=DB_NAME,
        is_echo=DB_IS_ECHO
    )
        
    # optional args
    if args.data_directory is not None:
        loaad_path = os.path.join(os.getcwd(), args.data_directory)
        for root, dirs, files in os.walk(loaad_path):
            for file_name in files:
                Loader.load(file_path=os.path.join(loaad_path, file_name))
        Loader.traverse_database()

    if args.data_path is not None: 
        Loader.load(file_path=args.data_path)
        Loader.traverse_database()

    if args.traverse_database in ('true', 'True', 't', 'T'): Loader.traverse_database()
Esempio n. 6
0
    def __init__(self, db_name='db', *args, **kwrags):
        if db_name is None or not isinstance(db_name, str): raise Exception('please provide DB name to init service')
        self.db_name = db_name

        DBManager.init_db(db_name=db_name, is_echo=kwrags.get('db_is_echo', True))
        self.session = DBManager.get_session()
Esempio n. 7
0
def test_loader():
    DBManager.init_db(db_name='db4')