Exemplo n.º 1
0
 def calc_distances(self):
     logger = logging.getLogger(_name_)
     n = len(self._tids)
     for i in range(0, n):
         j = i + 1
         logger.debug("calc topn " + str(i))
         while j < n:
             logger.debug("calc topn dists(" + str(i) + ", " + str(j) + ")")
             to = n - j
             if (to > Cfg.instance().threads_max()):
                 to = Cfg.instance().threads_max()
             if (to > n):
                 to = n
             threads = []
             for k in range(0, to):
                 t = CT(Cfg.instance().dist()
                        , Cfg.instance().targets_dists_tablename()
                        , Cfg.instance().targets_tablename()
                        , self._tids[i]
                        , self._tids[j])
                 t.start()
                 threads.append(t)
                 j += 1
             for t in threads:
                 t.join()
     logger.debug("cos calculation complete")
Exemplo n.º 2
0
 def solve_realisation(self, aid, iid):
     cluster = Cluster(aid, 0.9)
     ids = US.instance().ids()
     dist = DF.instance().create(Cfg.instance().dist())
     active_user = US.instance().get(aid)
     for id in ids:
         if not iid in US.instance().get(id).content:
             continue
         user = US.instance().get(id)
         cluster.add(user, dist(user, active_user))
         if (cluster.size() >= Cfg.instance().cluster_size()):
             return self.pred(cluster, iid)
Exemplo n.º 3
0
 def __init__(self):
     Set.__init__(self)
     reader = RF.instance().create(Config.instance().read_source_name(),
                                   Config.instance().users_tablename())
     self._set = dict()
     uids = reader.ids()
     threads = []
     for i in range(0, len(uids)):
         t = RT(Config.instance().users_tablename(), uids[i], self._set)
         t.start()
         threads.append(t)
     for t in threads:
         t.join()
Exemplo n.º 4
0
 def __init__(self):
     Set.__init__(self)
     reader = RF.instance().create(Config.instance().read_source_name()
                                   , Config.instance().targets_tablename())
     self._set = dict()
     logger = logging.getLogger(__name__)
     tids = reader.ids()
     threads = []
     for i in range(0, len(tids)):
         t = RT(Config.instance().targets_tablename(), tids[i], self._set)
         t.start()
         threads.append(t)
     for t in threads:
         t.join()
 def __init__(self):
     self._pool = None
     self._inited = False
     self._lock = Lock()
     self._con = None
     self._lock.acquire()
     config = Config.instance().config()
     self._pool = psycopg2.pool.ThreadedConnectionPool(
         5,
         Config.instance().max_conn(),
         user=config["user"],
         password=config["password"],
         host=config["host"],
         port=config["port"],
         database=config["db"])
     self._lock.release()
Exemplo n.º 6
0
 def run(self):
     df = DF.instance().create(Config.instance().dist())
     logger = logging.getLogger(_name_)
     if (self._set_name == Config.instance().targets_tablename()):
         t1 = TS.instance().get(self._id1)
         t2 = TS.instance().get(self._id2)
         print("d(" + t1.id() + ", " + t2.id() + ")")
         d = df(t1, t2)
         data = dict()
         data[t2.id()] = d
         if (t1.id() == t2.id()):
             d = 0
         t = Target(t1.id(), data)
         w = WF.instance().create(Config.instance().write_source_name(), self._table_name)
         w.write(t)
         data = dict()
         data[t1.id()] = d
         t = Target(t2.id(), data)
         w.write(t)
         TDM.instance().add(t1.id(), t2.id(), d)
Exemplo n.º 7
0
 def solve_realisation(self, aid):
     logger = logging.getLogger(__name__)
     topn = []
     imagine = IF.instance().create("test")
     user = imagine.do("1")
     logger = logging.getLogger(__name__)
     # TODO: create items subset with cluster class
     for id in TS.instance().ids():
         item = TS.instance().get(id)
         d = Dists.hamming(item, user)
         if (d < 0.2):
             logger.debug("append: " + id)
             topn.append(id)
         if (len(topn) >= Cfg.instance().topN_number()):
             break
     return topn
Exemplo n.º 8
0
 def solve_realisation(self, aid):
     logger = logging.getLogger(_name_)
     if (TDM.instance().is_empty()):
         self.calc_distances()
     v = self.create_user_vector(aid)
     mul_rslt = dict()
     for id in self._tids:
         column = TDM.instance().get_column(id)
         logger.debug(id + " column " + str(column) + " " + str(v))
         mul = np.dot(v, column)
         mul_rslt[id] = mul
     sorted_tids = sorted(mul_rslt.items(), key = lambda x: x[1], reverse = True)
     topn = []
     user = US.instance().get(aid)
     for i in range(0, len(v)):
         id = sorted_tids[i][0]
         if (id in user.content()):
             continue
         topn.append(id)
         if (len(topn) == Cfg.instance().topN_number()):
             break
     return topn
Exemplo n.º 9
0
 def read(self):
     reader = RF.instance().create(Config.instance().read_source_name(),
                                   self._tname)
     if (reader.count() == 0):
         return
     tids = reader.ids()
     threads = []
     contents = dict()
     for i in range(0, len(tids)):
         t = RT(self._tname, tids[i], contents)
         t.start()
         threads.append(t)
     for t in threads:
         t.join()
     for id1 in contents:
         pair = (id1, id1)
         self._data[pair] = 1
         for id2 in contents[id1].content():
             d = contents[id1].content()[id2]
             pair = (id1, id2)
             self._data[pair] = d
             pair = (id2, id1)
             self._data[pair] = d
Exemplo n.º 10
0
 def __init__(self, id):
     Thread.__init__(self)
     self.__name = str(id)
     WF.instance().register("psql_test",
                            PW(Config.instance().config(), "test"))
Exemplo n.º 11
0
from input_output.postgresql_writer import PsqlWriter as PW
from utilities.config import Config as Config
from core.solver import SF as SF
from core.item_based_topn import ItemBasedTopn as IBS
from math_model.distance_factory import DistanceFactory as DF
from math_model.distances import Distances as Distances
from math_model.agent import Agent as Agent
from core.fuzz_topn import FuzzTopn as FT
from math_model.imagine_factory import ImagineFactory as IF
from core.item_based_topn import ItemBasedTopn as IbTopn
from core.test_imagine_factory import TestImagine as TI
from core.ub_pred import UbPred as UP
from core.fuzz_pred import FuzzPred as FP

RF.instance().register(
    Config.instance().read_source_name() + "_" +
    Config.instance().targets_tablename(),
    PR(Config.instance().config(),
       Config.instance().targets_tablename()))
RF.instance().register(
    Config.instance().read_source_name() + "_" +
    Config.instance().users_tablename(),
    PR(Config.instance().config(),
       Config.instance().users_tablename()))
RF.instance().register(
    Config.instance().read_source_name() + "_" +
    Config.instance().targets_dists_tablename(),
    PR(Config.instance().config(),
       Config.instance().targets_dists_tablename()))
WF.instance().register(
    Config.instance().write_source_name() + "_" +
Exemplo n.º 12
0
 def __init__(self):
     DM.__init__(self, Config.instance().targets_dists_tablename())
Exemplo n.º 13
0
 def gt(a, b):
     rslt = a > b
     if (Cfg.instance().cmp_order() == "straight"):
         return rslt
     return not rslt