class StompListener(object): def __init__(self, config): self.config_ = config #Connect to mongo database try: connect(config['database']['db-name'], username= config['database']['user'], password= config['database']['passwd'], port = config['database']['port']) except ConnectionError as e: logger.error("Could not connect to mongodb: %s" % e) sys.exit(1) logger.info("Load feature extractor.") try: self.feature_extractor_ = EsaFeatureExtractor(prefix = self.config_["prefix"]) except Exception as inst: logger.error("Could not load feature extractor." "Unknown error %s: %s" % (type(inst), inst)) sys.exit(1) self.ranker = ArticleRanker(extractor = self.feature_extractor_) def rank_article(self, article_as_dict): self.ranker.rank_article(article_as_dict) def on_error(self, hears, message): logger.error('received an error %s' % message) def on_message(self, headers, message): received_message = json.loads(message) #save and rank article self.rank_article(received_message)
def setUp(self): fill_database() config_ = load_config(file_path = "/media/sdc1/Aptana Studio 3 Workspace/configs/config.yaml", logger = logger) self.feature_extractor = EsaFeatureExtractor(prefix = config_['prefix']) self.ranker = ArticleRanker(extractor = self.feature_extractor) self.article_as_dict = {'news_vendor': 'TechCrunch', 'author': "MG Siegler", 'link': "http://www.techcrunch.com", 'headline': "Again Apple", 'clean_content': "Fooobaaar!", 'content': "<p>Fooobaaar!</p>", 'features': {'version': '1.0', 'data': [(1, 0.5), (3, 0.6) ] } }
def __init__(self, config): self.config_ = config #Connect to mongo database try: connect(config['database']['db-name'], username= config['database']['user'], password= config['database']['passwd'], port = config['database']['port']) except ConnectionError as e: logger.error("Could not connect to mongodb: %s" % e) sys.exit(1) logger.info("Load feature extractor.") try: self.feature_extractor_ = EsaFeatureExtractor(prefix = self.config_["prefix"]) except Exception as inst: logger.error("Could not load feature extractor." "Unknown error %s: %s" % (type(inst), inst)) sys.exit(1) self.ranker = ArticleRanker(extractor = self.feature_extractor_)
class ArticleRankerTest(unittest.TestCase): def setUp(self): fill_database() config_ = load_config(file_path = "/media/sdc1/Aptana Studio 3 Workspace/configs/config.yaml", logger = logger) self.feature_extractor = EsaFeatureExtractor(prefix = config_['prefix']) self.ranker = ArticleRanker(extractor = self.feature_extractor) self.article_as_dict = {'news_vendor': 'TechCrunch', 'author': "MG Siegler", 'link': "http://www.techcrunch.com", 'headline': "Again Apple", 'clean_content': "Fooobaaar!", 'content': "<p>Fooobaaar!</p>", 'features': {'version': '1.0', 'data': [(1, 0.5), (3, 0.6) ] } } def tearDown(self): clear_database() def test_get_vendor_false(self): vendor = self.ranker.get_vendor({'news_vendor': 'not in db'}) self.assertEqual(vendor, None) def test_get_vendor(self): vendor = self.ranker.get_vendor(self.article_as_dict) self.assertEqual(vendor.config, 'vendor config') def test_save_article_false(self): vendor = self.ranker.get_vendor(self.article_as_dict) stored_article = self.ranker.save_article(vendor, {'headline': "Everything else is missing."}) self.assertEqual(stored_article, None) def test_save_article(self): vendor = self.ranker.get_vendor(self.article_as_dict) stored_article = self.ranker.save_article(vendor, self.article_as_dict) self.assertEqual(stored_article.author, 'MG Siegler') def test_save_rating(self): vendor = self.ranker.get_vendor(self.article_as_dict) stored_article = self.ranker.save_article(vendor, self.article_as_dict) user = User.objects(name = "Karsten Jeschkies").first() self.ranker.save_rating(user=user, article = stored_article, rating = 1.0) user.reload() ranked_articles = RankedArticle.objects(user_id = user.id) self.assertEqual(3, ranked_articles.count()) self.assertEqual(1.0, ranked_articles[0].rating) def test_rank_article(self): pass #some error in genism. probably because some features are not quite right self.ranker.rank_article(self.article_as_dict)