Пример #1
0
class UserModelCentroidTest(unittest.TestCase):
    def setUp(self):
        logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)

        config = load_config(file_path="/vagrant/config.yaml", logger=logger, exit_with_error=True)

        #Connect to test database
        connect("nyan_test", port=27017)
        fill_database()
        #connect(config['database']['db-name'], 
        #        username= config['database']['user'], 
        #        password= config['database']['passwd'], 
        #        port = config['database']['port'])

        self.user_id = User.objects(email=u'*****@*****.**').first().id

        #self.feature_extractor = EsaFeatureExtractor(prefix = config['prefix'])
        self.feature_extractor = TfidfFeatureExtractor(prefix=config['prefix'])
        self.trainer = UserModelCentroid(self.user_id, extractor=self.feature_extractor)

    def tearDown(self):
        clear_database()

    @unittest.skip("not for now")
    def test_train(self):
        self.trainer.train()

        self.assertAlmostEqual(self.trainer.learned_user_model.data[0][1],
                               0.1553, 4)

    @unittest.skip("no saving yet")
    def test_save(self):
        self.trainer.train()
        self.trainer.save()

        user_model = UserModel.objects(user_id=self.user_id).first()

        self.assertAlmostEqual(user_model.data[0][1],
                               0.0021, 4)
Пример #2
0
    logging.basicConfig(format="%(asctime)s : %(levelname)s : %(message)s", level=logging.DEBUG, filename=options.log)

    # load config
    logger = logging.getLogger("main")
    logging.info("Load config")

    config_ = load_config(options.config, logger, exit_with_error=True)

    if config_ is None:
        logger.error("No config. Exit.")
        sys.exit(1)

    # Connect to mongo database
    connect(
        config_["database"]["db-name"],
        username=config_["database"]["user"],
        password=config_["database"]["passwd"],
        port=config_["database"]["port"],
    )

    feature_extractor = TfidfFeatureExtractor(prefix=config_["prefix"])

    logger.info("Learn user model...")
    users = User.objects()
    for u in users:
        logger.info("for %s" % u.name)
        trainer = UserModelCentroid(user_id=u.id, extractor=feature_extractor)
        trainer.train()
        trainer.save()
    logger.info("...done.")