def history(self, app_id, mapped_entity, entity, projection, entity_id,
                version):
        """ gets an entity history """
        query = Query(self.reference_date,
                      version=None,
                      session=self.session,
                      branch=self.branch)
        query.set_query_context(app_id, mapped_entity, entity)

        return query.history(mapped_entity, entity, projection, entity_id,
                             version)
    def filter(self,
               app_id,
               mapped_entity,
               entity,
               projection,
               page=None,
               page_size=None):
        """ execute a query on domain """
        query = Query(self.reference_date,
                      self.version,
                      self.session,
                      branch=self.branch)
        query.set_query_context(app_id, mapped_entity, entity)

        return query.execute(projection, page=page, page_size=page_size)
Ejemplo n.º 3
0
    def get_result(self, qr, method='Cosine', exp=False):
        q = Query()
        reader = CSV_Reader()
        q_dict = q.split_query(qr)
        all_d_dict = reader.get_dict("iif_weight.csv")
        result = q.retrieve(q_dict, all_d_dict, method, exp)

        if not result:
            response = []
        elif (result[0][1]) == 1 or m.isnan(result[0][1]):
            response = []
        else:
            response = result

        return response
Ejemplo n.º 4
0
 def run(self):
     self.queries = []
     self.queries.append(Query(self.query_str, -1))
     self.get_entity()
     for q in self.queries:
         if len(q.entities):
             self.entities.extend(q.entities)
Ejemplo n.º 5
0
    def run(self):
        mentions = self.extract_mentions(self.comment)
        for m in mentions:
            self.queries.append(Query(m[0], m[1]))
#         for q in self.queries:
#             print (q.text+' %d' % q.index)
        self.get_entity()
Ejemplo n.º 6
0
    def run(self):
        self.queries = []

        if re.search(ur"[\u4e00-\u9fff]+", self.query_str):  #Chinese
            self.lan = "ch"
            if len(self.query_str) > 5:
                self.extract_zh_mentions(self.query_str)
            else:
                self.queries.append(Query(self.query_str, 0))
Ejemplo n.º 7
0
 def split_querystr(self):
     """
     Get sub query strings according to truncate the origin string
     """
     ws = self.query_str.split()
     for i in range(0, len(ws) - 2):
         for j in range(2, len(ws)):
             new_query = " ".join(ws[i:j])
             self.queries.append(Query(new_query, -1))
Ejemplo n.º 8
0
    def extract_mentions(self):

        te = TermExtractor()
        for t in te.get_terms(1, self.text):
            try:
                index = self.text.index(t)
                q = Query(t.lower(), index)
                self.queries.append(q)
            except ValueError, e:
                print "Substring:", t, "is not in string"
Ejemplo n.º 9
0
    def extract_mentions(self):
        """
        Extract terminology from query string
        """
        te = TermExtractor()
        terms = te.get_terms(1, self.query_str)
        if len(terms) > 0:
            for t in terms:
                q = Query(t.lower(), -1)
                self.queries.append(q)

        print "%d term mentions" % len(self.queries)
Ejemplo n.º 10
0
    def run(self):
        self.queries = []
        w_num = len(self.query_str.split())
        if w_num < 3:
            self.queries.append(Query(self.query_str, -1))
        if w_num >= 5:  # extract terminology
            print "Extract terminology"
            self.extract_mentions()
        if self.no_entity(
        ) and w_num >= 3 and w_num <= 5:  # split query string into short substring
            print "Split Query string"
            self.split_querystr()

            if "-" in self.query_str:
                for q in self.query_str.split():
                    if "-" in q:
                        self.queries.append(Query(q, -1))
                        break

        self.get_entity()
        for q in self.queries:
            if len(q.entities):
                self.entities.append(q.entities[0])
Ejemplo n.º 11
0
    def extract_zh_mentions(self, s):
        """
        Use jieba to extract mentions from Chinese string
        """

        types = ['nt', 'nr', 'nz', 'ns']

        seg_list = pseg.cut(s)
        seg_index = []
        last = 0
        for seg in seg_list:
            begin = s.index(seg.word, last)
            last = begin + len(seg.word)
            if seg.flag in types:
                self.queries.append(Query(seg.word.encode('utf-8'), begin))

        print "%d mentions" % len(self.queries)
Ejemplo n.º 12
0
    def extract_en_mentions(self, s):
        """
        Usr Standford Parser and NLTK to extract mentions from english string
        """
        types = ['LOCATION', 'PERSON', 'ORGANIZATION']

        if not self.en_parser:
            self.en_parser = stanford_parser.Parser()
        seg_list = self.en_parser.NER(s)
        seg_index = []
        begin = 0
        last = 0
        i = 0
        if type(
                seg_list[0]
        ) == list:  #不同系统上的返回结果格式不一样,我们想要的是[('word','LOCATION'),('people','PERSON')],但有的是[[('word','LOCATION')]],这种情况要把里面的list提取出来
            seg_list = seg_list[0]
        while i < len(seg_list):
            segs = []
            word, tag = seg_list[i]
            if tag in types:
                j = i
                # 属于指定类型,类型相同且连续的,合到一起
                while j < len(seg_list) and seg_list[j][1] == tag:
                    segs.append(seg_list[j][0])
                    j += 1
                i = j
                query = " ".join(segs)
                print "query:", query
                try:
                    begin = s.index(query, last)
                    print "Can't find %s" % query
                except:
                    continue
                last = begin + len(query)
                self.queries.append(Query(query, begin))
            else:
                #不属于指定类型,直接跳过
                i += 1
                #begin = s.index(word, last)
                #last = begin + len(word)

        print "%d mentions" % len(self.queries)
Ejemplo n.º 13
0
        self.zh_parser = None

    def run(self):
        self.queries = []

        if re.search(ur"[\u4e00-\u9fff]+", self.query_str):  #Chinese
            self.lan = "ch"
            if len(self.query_str) > 5:
                self.extract_zh_mentions(self.query_str)
            else:
                self.queries.append(Query(self.query_str, 0))
        else:  #English or others
            if len(self.query_str.split()) > 3:
                self.extract_en_mentions(self.query_str)
            else:
                self.queries.append(Query(self.query_str, 0))

        self.get_entity()

    def set_candb(self, db):
        """
        Set Candidateset Mysql db
        """
        self.candb = db

    def set_graph(self, x):
        """
        Set XLore db
        """
        self.graph = x
Ejemplo n.º 14
0
from configuration.socket import SocketConfiguration

# TODO: class and def documentations
# TODO: write tests

app = Flask(__name__)

# version
version = 'v1'

config = SocketConfiguration(
    cfg_file=os.path.join(os.path.dirname(__file__), 'lsapi.cfg'))

ls_accessor = Socket(config.connection_string, config.connection_type)
# init LS query class
ls_query = Query(ls_accessor)


@app.route('/%s/columns' % version, methods=['GET'])
def get_columns():
    entity = 'columns'
    columns = get_columns_from_get_parameter_or_use_defaults(
        request.args, entity)
    query_filter = get_filter_from_get_parameter(request.args)
    with QueryTableCtx(ls_query, entity, query_filter, columns) as ls_ctx:
        data, ls_return_code = ls_ctx.query()
        with LivestatusActionCtx(data, ls_return_code) as task_ctx:
            return task_ctx.return_table()


# COMMENTS