示例#1
0
###############################
# Testing the command line app
###############################
import unittest, random

from util import (config_doc_kw_model, get_session)

_, fmim = config_doc_kw_model()

from scinet3.cmdapp import CmdApp
from scinet3.model import (Document, Keyword)

from scinet3.fb_propagator import OnePassPropagator
from scinet3.fb_updater import OverrideUpdater

from scinet3.rec_engine.query import QueryBasedRecommender
from scinet3.rec_engine.linrel import LinRelRecommender


class CmdAppTest(unittest.TestCase):
    def setUp(self):
        init_recommender = QueryBasedRecommender(3, 2, 3, 2, **fmim.__dict__)
        main_recommender = LinRelRecommender(3, 3, 1., .5, 1., .5, None, None,
                                             None, None, **fmim.__dict__)

        self.app = CmdApp(OnePassPropagator, OverrideUpdater, init_recommender,
                          main_recommender)

        self.session = get_session()

        #add recommended list
示例#2
0
##############################
# Test the associations between Document and Keywords
##############################
import unittest
from types import DictType

from scinet3.model import Document, Keyword

from util import config_doc_kw_model, get_session

config_doc_kw_model()

class ModelTest(unittest.TestCase):
    def setUp(self):
        #load all first
        self.docs = Document.load_all_from_db()
        
    def test_document(self):
        """
        whether id, article, keywords are correct
        """
        kw_strs = ["redis", "database", "a"]
        doc = Document.get(1)
        
        self.assertEqual(doc.id, 1)
        
        self.assertEqual(doc.title, "redis: key-value-storage database (ONE)")
        
        self.assertEqual(set(doc.keywords), set(Keyword.get_many(kw_strs)))

        #that is as far as we can test
###############################
# Testing the onepass feedback propagator
###############################
import unittest
from util import (config_doc_kw_model, get_session)

#config model, 
#only done once
config_doc_kw_model()

from scinet3.model import (Document, Keyword)
from scinet3.fb_propagator import OnePassPropagator as ppgt
from scinet3.fb_updater import OverrideUpdater as upd

class OnePassPropagatorTest(unittest.TestCase):
    def setUp(self):
        self.session = get_session()
        
        #add recommendation list
        self.session.add_doc_recom_list(Document.get_many([1,2]))
        
    def test_fb_from_doc(self):
        doc = Document.get(1)
        ppgt.fb_from_doc(doc, 0.5, self.session)        
        
        upd.update(self.session)
        
        # assertions
        self.assertAlmostEqual(.5 * .7, doc.fb(self.session))

        self.assertAlmostEqual(1/2., Keyword.get("a").fb(self.session))
###############################
# Testing the LinRel recommender
###############################
from util import (config_doc_kw_model, get_session, NumericTestCase)

from scinet3.model import (Document, Keyword)
from scinet3.rec_engine.linrel import LinRelRecommender
from scinet3.data import FeatureMatrixAndIndexMapping

_, fmim = config_doc_kw_model()

class LinRelUtilityTest(NumericTestCase):
    def setUp(self):
        self.r = LinRelRecommender(4, 4, 
                                   1., .5, 1., .5,
                                   None, None,
                                   **fmim.__dict__)

    def test_associated_keywords_from_documents(self):
        kws = self.r.associated_keywords_from_docs(Document.get_many([1,2]))
        self.assertEqual(set(Keyword.get_many(["a", "database", "redis", "the"])),
                         set(kws))
        
        exclude_kws = [Keyword.get("redis")]
        kws = self.r.associated_keywords_from_docs(Document.get_many([1,2]), exclude_kws)
        
        self.assertEqual(set(Keyword.get_many(["a", "database", "the"])), 
                         set(kws))

    def test_filter_objs(self):
        def has_redis_filter(objs = None):