# Path: app/models.py
# class RepoStarring(models.Model):
#     user_id = models.IntegerField()
#     user_username = models.CharField(max_length=39)
#     repo_id = models.IntegerField()
#     repo_full_name = models.CharField(max_length=140)
#     starred_at = models.DateTimeField()
# 
#     class Meta:
#         unique_together = (('user_id', 'repo_id'),)
# 
#     def __str__(self):
#         return '@{0} starred {1}'.format(self.user_username, self.repo_full_name)
# 
#     @staticmethod
#     def create_one(user_dict, repo_dict):
#         rs = RepoStarring()
#         try:
#             rs.user_id = user_dict['id']
#             rs.user_username = user_dict['login']
#             rs.repo_id = repo_dict['id']
#             rs.repo_full_name = repo_dict['full_name']
#             rs.starred_at = repo_dict['starred_at']
#         except (KeyError, TypeError) as e:
#             print(e)
#             print(user_dict)
#             print(repo_dict)
#             return
# 
#         try:
#             rs.save()
#         except IntegrityError:
#             pass
# 
# Path: app/utils_repo.py
# def prepare_user_item_df(min_stargazers_count):
#     repos = RepoStarring.objects \
#         .filter(stargazers_count__gte=min_stargazers_count) \
#         .values_list('repo_full_name', flat=True) \
#         .order_by('repo_full_name') \
#         .distinct()
#     repos_array = np.fromiter(repos.iterator(), np.dtype('U140'))
#     repos_array.shape
#     n_repos = repos_array.shape[0]
# 
#     users = RepoStarring.objects \
#         .filter(stargazers_count__gte=min_stargazers_count) \
#         .values_list('from_username', flat=True) \
#         .order_by('from_username') \
#         .distinct()
#     users_array = np.fromiter(users.iterator(), np.dtype('U39'))
#     users_array.shape
#     n_users = users_array.shape[0]
# 
#     logger.info('Build the utility matrix')
#     logger.info('The number of users: {0}'.format(n_users))
#     logger.info('The number of items: {0}'.format(n_repos))
# 
#     filename = 'caches/df-{0}x{1}.pickle'.format(n_users, n_repos)
#     try:
#         user_item_df = pickle.load(open(filename, 'rb'))
#     except IOError:
#         shape = (n_users, n_repos)
#         matrix = np.zeros(shape, dtype=np.int8)
#         for i, username in enumerate(users_array):
#             user_starred = RepoStarring.objects \
#                 .filter(from_username=username) \
#                 .values_list('repo_full_name', flat=True)
#             user_starred_array = np.fromiter(user_starred.iterator(), np.dtype('U140'))
#             row = np.in1d(repos_array, user_starred_array, assume_unique=True)
#             matrix[i] = row.astype(np.float64)
# 
#         user_item_df = pd.DataFrame(matrix, columns=repos_array, index=users_array)
#         user_item_df.to_pickle(filename)
# 
#     return user_item_df


import re
import pandas as pd
from django.core.management.base import BaseCommand
from django.db import connection
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
from app.models import RepoStarring
from app.utils_repo import prepare_user_item_df
# coding: utf-8






class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('-i', '--repo_full_name', action='store', dest='repo_full_name', required=True)

    def handle(self, *args, **options):
        repo_full_name = options['repo_full_name']

        self.stdout.write(self.style.SUCCESS('Active item: @{0}'.format(repo_full_name)))

        min_stargazers_count = 500
        user_item_df = prepare_user_item_df(min_stargazers_count=min_stargazers_count)


# Path: app/models.py
# class RepoStarring(models.Model):
#     user_id = models.IntegerField()
#     user_username = models.CharField(max_length=39)
#     repo_id = models.IntegerField()
#     repo_full_name = models.CharField(max_length=140)
#     starred_at = models.DateTimeField()
# 
#     class Meta:
#         unique_together = (('user_id', 'repo_id'),)
# 
#     def __str__(self):
#         return '@{0} starred {1}'.format(self.user_username, self.repo_full_name)
# 
#     @staticmethod
#     def create_one(user_dict, repo_dict):
#         rs = RepoStarring()
#         try:
#             rs.user_id = user_dict['id']
#             rs.user_username = user_dict['login']
#             rs.repo_id = repo_dict['id']
#             rs.repo_full_name = repo_dict['full_name']
#             rs.starred_at = repo_dict['starred_at']
#         except (KeyError, TypeError) as e:
#             print(e)
#             print(user_dict)
#             print(repo_dict)
#             return
# 
#         try:
#             rs.save()
#         except IntegrityError:
#             pass
# 
# Path: app/utils_repo.py
# def prepare_user_item_df(min_stargazers_count):
#     repos = RepoStarring.objects \
#         .filter(stargazers_count__gte=min_stargazers_count) \
#         .values_list('repo_full_name', flat=True) \
#         .order_by('repo_full_name') \
#         .distinct()
#     repos_array = np.fromiter(repos.iterator(), np.dtype('U140'))
#     repos_array.shape
#     n_repos = repos_array.shape[0]
# 
#     users = RepoStarring.objects \
#         .filter(stargazers_count__gte=min_stargazers_count) \
#         .values_list('from_username', flat=True) \
#         .order_by('from_username') \
#         .distinct()
#     users_array = np.fromiter(users.iterator(), np.dtype('U39'))
#     users_array.shape
#     n_users = users_array.shape[0]
# 
#     logger.info('Build the utility matrix')
#     logger.info('The number of users: {0}'.format(n_users))
#     logger.info('The number of items: {0}'.format(n_repos))
# 
#     filename = 'caches/df-{0}x{1}.pickle'.format(n_users, n_repos)
#     try:
#         user_item_df = pickle.load(open(filename, 'rb'))
#     except IOError:
#         shape = (n_users, n_repos)
#         matrix = np.zeros(shape, dtype=np.int8)
#         for i, username in enumerate(users_array):
#             user_starred = RepoStarring.objects \
#                 .filter(from_username=username) \
#                 .values_list('repo_full_name', flat=True)
#             user_starred_array = np.fromiter(user_starred.iterator(), np.dtype('U140'))
#             row = np.in1d(repos_array, user_starred_array, assume_unique=True)
#             matrix[i] = row.astype(np.float64)
# 
#         user_item_df = pd.DataFrame(matrix, columns=repos_array, index=users_array)
#         user_item_df.to_pickle(filename)
# 
#     return user_item_df


# Path: app/management/commands/train_content_based.py
import re
import pandas as pd
from django.core.management.base import BaseCommand
from django.db import connection
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
from app.models import RepoStarring
from app.utils_repo import prepare_user_item_df

# coding: utf-8






class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('-i', '--repo_full_name', action='store', dest='repo_full_name', required=True)

    def handle(self, *args, **options):
        repo_full_name = options['repo_full_name']

        self.stdout.write(self.style.SUCCESS('Active item: @{0}'.format(repo_full_name)))

        min_stargazers_count = 500
        user_item_df = prepare_user_item_df(min_stargazers_count=min_stargazers_count)


        rs = RepoStarring.objects \