示例#1
0
    def load(self):
        try:
            tag_recommendation_data = loadFromJson(
                RECOMMENDATION_DATA_DIR +
                'Current_database_and_class_names.json')
            DATABASE = tag_recommendation_data['database']
            CLASSES = tag_recommendation_data['classes']
            self.cbtr = CommunityBasedTagRecommender(dataset=DATABASE,
                                                     classes=CLASSES)
            self.cbtr.load_recommenders()

        except:
            self.cbtr = None
            logger.info(
                "No computed matrices were found, recommendation system not loading for the moment (but service listening for data to come)."
            )

        try:
            self.index_stats = loadFromJson(RECOMMENDATION_DATA_DIR +
                                            'Current_index_stats.json')
            logger.info("Matrices computed out of information from %i sounds" %
                        self.index_stats['n_sounds_in_matrix'])
        except Exception, e:
            print e
            self.index_stats = {
                'n_sounds_in_matrix': 0,
            }
    def load(self):
        try:
            tag_recommendation_data = loadFromJson(RECOMMENDATION_DATA_DIR + 'Current_database_and_class_names.json')
            DATABASE = tag_recommendation_data['database']
            CLASSES = tag_recommendation_data['classes']
            self.cbtr = CommunityBasedTagRecommender(dataset=DATABASE, classes=CLASSES)
            self.cbtr.load_recommenders()

        except:
            self.cbtr = None
            logger.info("No computed matrices were found, recommendation system not loading for the moment (but service listening for data to come).")

        try:
            self.index_stats = loadFromJson(RECOMMENDATION_DATA_DIR + 'Current_index_stats.json')
            logger.info("Matrices computed out of information from %i sounds" % self.index_stats['n_sounds_in_matrix'])
        except Exception as e:
            print(e)
            self.index_stats = {
                'n_sounds_in_matrix': 0,
            }

        try:
            self.index = loadFromJson(RECOMMENDATION_DATA_DIR + 'Index.json')
            self.index_stats['biggest_id_in_index'] = max([int(key) for key in self.index.keys()])
            self.index_stats['n_sounds_in_index'] = len(self.index.keys())
        except Exception as e:
            logger.info("Index file not present. Listening for indexing data from appservers.")
            self.index_stats['biggest_id_in_index'] = 0
            self.index_stats['n_sounds_in_index'] = 0
            self.index = dict()
示例#3
0
    def load(self):
        try:
            tag_recommendation_data = loadFromJson(
                tr_settings.RECOMMENDATION_DATA_DIR + 'Current_database_and_class_names.json')
            DATABASE = tag_recommendation_data['database']
            CLASSES = tag_recommendation_data['classes']
            self.cbtr = CommunityBasedTagRecommender(dataset=DATABASE, classes=CLASSES)
            self.cbtr.load_recommenders()

        except:
            self.cbtr = None
            logger.info("No computed matrices were found, recommendation system not loading for the moment (but service listening for data to come).")

        try:
            self.index_stats = loadFromJson(tr_settings.RECOMMENDATION_DATA_DIR + 'Current_index_stats.json')
            logger.info("Matrices computed out of information from %i sounds" % self.index_stats['n_sounds_in_matrix'])
        except Exception as e:
            print(e)
            self.index_stats = {
                'n_sounds_in_matrix': 0,
            }

        try:
            self.index = loadFromJson(tr_settings.RECOMMENDATION_DATA_DIR + 'Index.json')
            self.index_stats['biggest_id_in_index'] = max([int(key) for key in self.index.keys()])
            self.index_stats['n_sounds_in_index'] = len(self.index.keys())
        except Exception as e:
            logger.info("Index file not present. Listening for indexing data from appservers.")
            self.index_stats['biggest_id_in_index'] = 0
            self.index_stats['n_sounds_in_index'] = 0
            self.index = dict()
    def load(self):
        try:
            tag_recommendation_data = loadFromJson(RECOMMENDATION_DATA_DIR + 'Current_database_and_class_names.json')
            DATABASE = tag_recommendation_data['database']
            CLASSES = tag_recommendation_data['classes']
            self.cbtr = CommunityBasedTagRecommender(dataset=DATABASE, classes=CLASSES)
            self.cbtr.load_recommenders()

        except:
            self.cbtr = None
            logger.info("No computed matrices were found, recommendation system not loading for the moment (but service listening for data to come).")

        try:
            self.index_stats = loadFromJson(RECOMMENDATION_DATA_DIR + 'Current_index_stats.json')
            logger.info("Matrices computed out of information from %i sounds" % self.index_stats['n_sounds_in_matrix'])
        except Exception, e:
            print e
            self.index_stats = {
                'n_sounds_in_matrix': 0,
            }
示例#5
0
class TagRecommendationServer(resource.Resource):
    def __init__(self):
        resource.Resource.__init__(self)
        self.methods = server_interface(self)
        self.isLeaf = False

        self.load()

    def load(self):
        try:
            tag_recommendation_data = loadFromJson(
                tr_settings.RECOMMENDATION_DATA_DIR +
                'Current_database_and_class_names.json')
            DATABASE = tag_recommendation_data['database']
            CLASSES = tag_recommendation_data['classes']
            self.cbtr = CommunityBasedTagRecommender(dataset=DATABASE,
                                                     classes=CLASSES)
            self.cbtr.load_recommenders()

        except:
            self.cbtr = None
            logger.info(
                "No computed matrices were found, recommendation system not loading for the moment ("
                "but service listening for data to come).")

        try:
            self.index_stats = loadFromJson(
                tr_settings.RECOMMENDATION_DATA_DIR +
                'Current_index_stats.json')
            logger.info("Matrices computed out of information from %i sounds" %
                        self.index_stats['n_sounds_in_matrix'])
        except Exception as e:
            print(e)
            self.index_stats = {
                'n_sounds_in_matrix': 0,
            }

        try:
            self.index = loadFromJson(tr_settings.RECOMMENDATION_DATA_DIR,
                                      'Index.json')
            self.index_stats['biggest_id_in_index'] = max(
                [int(key) for key in self.index.keys()])
            self.index_stats['n_sounds_in_index'] = len(self.index.keys())
        except Exception as e:
            logger.info(
                "Index file not present. Listening for indexing data from appservers."
            )
            self.index_stats['biggest_id_in_index'] = 0
            self.index_stats['n_sounds_in_index'] = 0
            self.index = dict()

    def error(self, message):
        return json.dumps({'Error': message})

    def getChild(self, name, request):
        return self

    def render_GET(self, request):
        return self.methods[request.prepath[1]](**request.args)

    def recommend_tags(self, input_tags, max_number_of_tags=None):

        try:
            logger.debug('Getting recommendation for input tags %s' %
                         input_tags)
            input_tags = input_tags[0].split(",")
            if max_number_of_tags:
                max_number_of_tags = int(max_number_of_tags[0])
            recommended_tags, com_name = self.cbtr.recommend_tags(
                input_tags, max_number_of_tags=max_number_of_tags)
            result = {
                'error': False,
                'result': {
                    'tags': recommended_tags,
                    'community': com_name
                }
            }

        except Exception as e:
            logger.debug('Errors occurred while recommending tags to %s' %
                         input_tags)
            result = {'error': True, 'result': str(e)}

        return json.dumps(result)

    def reload(self):
        logger.info('Reloading tagrecommendation server...')
        self.load()
        result = {'error': False, 'result': "Server reloaded"}
        return json.dumps(result)

    def last_indexed_id(self):
        result = {
            'error': False,
            'result': self.index_stats['biggest_id_in_index']
        }
        logger.info(
            'Getting last indexed id information (%i, %i sounds in index, %i sounds in matrix)'
            % (self.index_stats['biggest_id_in_index'],
               self.index_stats['n_sounds_in_index'],
               self.index_stats['n_sounds_in_matrix']))
        return json.dumps(result)

    def add_to_index(self, sound_ids, sound_tagss):
        sound_ids = sound_ids[0].split(",")
        sound_tags = [
            stags.split(",") for stags in sound_tagss[0].split("-!-!-")
        ]
        logger.info('Adding %i sounds to recommendation index' %
                    len(sound_ids))

        for count, sound_id in enumerate(sound_ids):
            sid = sound_id
            stags = sound_tags[count]
            self.index[sid] = stags

        if len(self.index.keys()) % 1000 == 0:
            # Every 1000 indexed sounds, save the index
            logger.info('Saving tagrecommendation index...')
            saveToJson(tr_settings.RECOMMENDATION_DATA_DIR + 'Index.json',
                       self.index,
                       verbose=False)
            self.index_stats['biggest_id_in_index'] = max(
                [int(key) for key in self.index.keys()])
            self.index_stats['n_sounds_in_index'] = len(self.index.keys())

        result = {'error': False, 'result': True}
        return json.dumps(result)
示例#6
0
class TagRecommendationServer(resource.Resource):
    def __init__(self):
        resource.Resource.__init__(self)
        self.methods = server_interface(self)
        self.isLeaf = False

        self.load()

    def load(self):
        try:
            tag_recommendation_data = loadFromJson(
                tr_settings.RECOMMENDATION_DATA_DIR + 'Current_database_and_class_names.json')
            DATABASE = tag_recommendation_data['database']
            CLASSES = tag_recommendation_data['classes']
            self.cbtr = CommunityBasedTagRecommender(dataset=DATABASE, classes=CLASSES)
            self.cbtr.load_recommenders()

        except:
            self.cbtr = None
            logger.info("No computed matrices were found, recommendation system not loading for the moment (but service listening for data to come).")

        try:
            self.index_stats = loadFromJson(tr_settings.RECOMMENDATION_DATA_DIR + 'Current_index_stats.json')
            logger.info("Matrices computed out of information from %i sounds" % self.index_stats['n_sounds_in_matrix'])
        except Exception as e:
            print(e)
            self.index_stats = {
                'n_sounds_in_matrix': 0,
            }

        try:
            self.index = loadFromJson(tr_settings.RECOMMENDATION_DATA_DIR + 'Index.json')
            self.index_stats['biggest_id_in_index'] = max([int(key) for key in self.index.keys()])
            self.index_stats['n_sounds_in_index'] = len(self.index.keys())
        except Exception as e:
            logger.info("Index file not present. Listening for indexing data from appservers.")
            self.index_stats['biggest_id_in_index'] = 0
            self.index_stats['n_sounds_in_index'] = 0
            self.index = dict()

    def error(self,message):
        return json.dumps({'Error': message})

    def getChild(self, name, request):
        return self

    def render_GET(self, request):
        return self.methods[request.prepath[1]](**request.args)

    def recommend_tags(self, input_tags, max_number_of_tags=None):

        try:
            logger.debug('Getting recommendation for input tags %s' % input_tags)
            input_tags = input_tags[0].split(",")
            if max_number_of_tags:
                max_number_of_tags = int(max_number_of_tags[0])
            recommended_tags, com_name = self.cbtr.recommend_tags(input_tags,
                                                                  max_number_of_tags=max_number_of_tags)
            result = {'error': False, 'result': {'tags': recommended_tags, 'community': com_name}}

        except Exception as e:
            logger.debug('Errors occurred while recommending tags to %s' % input_tags)
            result = {'error': True, 'result': str(e)}

        return json.dumps(result)

    def reload(self):
        logger.info('Reloading tagrecommendation server...')
        self.load()
        result = {'error': False, 'result': "Server reloaded"}
        return json.dumps(result)

    def last_indexed_id(self):
        result = {'error': False, 'result': self.index_stats['biggest_id_in_index']}
        logger.info('Getting last indexed id information (%i, %i sounds in index, %i sounds in matrix)' % (self.index_stats['biggest_id_in_index'],
                                                                                                           self.index_stats['n_sounds_in_index'],
                                                                                                           self.index_stats['n_sounds_in_matrix']))
        return json.dumps(result)

    def add_to_index(self, sound_ids, sound_tagss):
        sound_ids = sound_ids[0].split(",")
        sound_tags = [stags.split(",") for stags in sound_tagss[0].split("-!-!-")]
        logger.info('Adding %i sounds to recommendation index' % len(sound_ids))

        for count, sound_id in enumerate(sound_ids):
            sid = sound_id
            stags = sound_tags[count]
            self.index[sid] = stags

        if len(self.index.keys()) % 1000 == 0:
            # Every 1000 indexed sounds, save the index
            logger.info('Saving tagrecommendation index...')
            saveToJson(tr_settings.RECOMMENDATION_DATA_DIR + 'Index.json', self.index, verbose=False)
            self.index_stats['biggest_id_in_index'] = max([int(key) for key in self.index.keys()])
            self.index_stats['n_sounds_in_index'] = len(self.index.keys())

        result = {'error': False, 'result': True}
        return json.dumps(result)