def translate_query(self, query_text):
     """
     Perform the actual translation.
     :param query_text:
     :param relation_oracle:
     :param entity_oracle:
     :return:
     """
     # Parse query.
     logger.info("Translating query: %s." % query_text)
     start_time = time.time()
     # Parse the query.
     query = self.parse_and_identify_entities(query_text)
     # Set the relation oracle.
     query.relation_oracle = self.scorer.get_parameters().relation_oracle
     # Identify the target type.
     target_identifier = AnswerTypeIdentifier()
     target_identifier.identify_target(query)
     # Get content tokens of the query.
     query.query_content_tokens = get_content_tokens(query.query_tokens)
     # Match the patterns.
     pattern_matcher = QueryPatternMatcher(query, self.query_extender,
                                           self.sparql_backend)
     ert_matches = []
     ermrt_matches = []
     ermrert_matches = []
     ert_matches = pattern_matcher.match_ERT_pattern()
     ermrt_matches = pattern_matcher.match_ERMRT_pattern()
     ermrert_matches = pattern_matcher.match_ERMRERT_pattern()
     duration = (time.time() - start_time) * 1000
     logging.info("Total translation time: %.2f ms." % duration)
     return ert_matches + ermrt_matches + ermrert_matches
Beispiel #2
0
 def translate_query(self, query_text):
     """
     Perform the actual translation.
     :param query_text:
     :param relation_oracle:
     :param entity_oracle:
     :return:
     """
     # Parse query.
     logger.info("Translating query: %s." % query_text)
     start_time = time.time()
     # Parse the query.
     query = self.parse_and_identify_entities(query_text)
     # Set the relation oracle.
     query.relation_oracle = self.scorer.get_parameters().relation_oracle
     # Identify the target type.
     target_identifier = AnswerTypeIdentifier()
     target_identifier.identify_target(query)
     # Get content tokens of the query.
     query.query_content_tokens = get_content_tokens(query.query_tokens)
     # Match the patterns.
     pattern_matcher = QueryPatternMatcher(query,
                                           self.query_extender,
                                           self.sparql_backend)
     ert_matches = []
     ermrt_matches = []
     ermrert_matches = []
     ert_matches = pattern_matcher.match_ERT_pattern()
     ermrt_matches = pattern_matcher.match_ERMRT_pattern()
     ermrert_matches = pattern_matcher.match_ERMRERT_pattern()
     duration = (time.time() - start_time) * 1000
     logging.info("Total translation time: %.2f ms." % duration)
     return ert_matches + ermrt_matches + ermrert_matches
Beispiel #3
0
    def translate_query(self, query_text):
        """
        Perform the actual translation.
        :param query_text:
        :param relation_oracle:
        :param entity_oracle:
        :return:
        """

        partial_result = "Query: " + query_text + '\n'

        # Parse query.
        logger.info("Translating query: %s." % query_text)
        start_time = time.time()
        # Parse the query.
        query = self.parse_and_identify_entities(query_text)
        # Set the relation oracle.
        query.relation_oracle = self.scorer.get_parameters().relation_oracle
        for e in query.identified_entities:
            partial_result += "Entity: " + str((e.name, e.surface_score, e.score, e.perfect_match)) + '\n'
            #logging.error((e.name, e.surface_score, e.score, e.perfect_match))

        # Identify the target type.
        target_identifier = AnswerTypeIdentifier()
        target_identifier.identify_target(query)
        #logging.error(query.target_type.as_string())
        partial_result += "TargetType: " + str(query.target_type.as_string()) + "\n"

        # Get content tokens of the query.
        query.query_content_tokens = get_content_tokens(query.query_tokens)
        # Match the patterns.
        pattern_matcher = QueryPatternMatcher(query,
                                              self.query_extender,
                                              self.sparql_backend)
        ert_matches = []
        ermrt_matches = []
        ermrert_matches = []
        ert_matches = pattern_matcher.match_ERT_pattern()
        ermrt_matches = pattern_matcher.match_ERMRT_pattern()
        ermrert_matches = pattern_matcher.match_ERMRERT_pattern()

        partial_result += "Pattern matches: ERT = %d, ERMRT = %d, ERMRERT = %d\n" % (len(ert_matches), len(ermrt_matches), len(ermrert_matches))
        writeFile(test_file, partial_result, "a")

        duration = (time.time() - start_time) * 1000
        logging.info("Total translation time: %.2f ms." % duration)
        return ert_matches + ermrt_matches + ermrert_matches
Beispiel #4
0
    def generate_candidates(self, query_text):
        """
        Perform the actual translation.
        :param query_text:
        :param relation_oracle:
        :param entity_oracle:
        :return:
        """
        num_sparql_queries = self.sparql_backend.num_queries_executed
        sparql_query_time = self.sparql_backend.total_query_time

        # Parse query.
        logger.info("Translating query: %s." % query_text)
        start_time = time.time()
        # Parse the query.
        query = self.parse_and_identify_entities(query_text)
        # Set the relation oracle.
        query.relation_oracle = self.get_scorer().get_parameters().relation_oracle
        # Identify the target type.
        target_identifier = AnswerTypeIdentifier()
        target_identifier.identify_target(query)
        # Get content tokens of the query.
        query.query_content_tokens = get_content_tokens(query.query_tokens)
        # Match the patterns.
        pattern_matcher = QueryPatternMatcher(query,
                                              self.query_extender,
                                              self.sparql_backend)
        ert_matches = pattern_matcher.match_ERT_pattern()
        ermrt_matches = pattern_matcher.match_ERMRT_pattern()
        ermrert_matches = pattern_matcher.match_ERMRERT_pattern()
        duration = (time.time() - start_time) * 1000
        logging.info("Total translation time: %.2f ms." % duration)
        candidates = ert_matches + ermrt_matches + ermrert_matches
        # Extend existing candidates, e.g. by adding answer entity type filters.
        candidates = self.extend_candidates(candidates)

        translation_time = (self.sparql_backend.total_query_time - sparql_query_time) * 1000
        num_sparql_queries = self.sparql_backend.num_queries_executed - num_sparql_queries
        avg_query_time = translation_time / (num_sparql_queries + 0.001)
        logger.info("Translation executed %s queries in %.2f ms."
                    " Average: %.2f ms." % (num_sparql_queries,
                                            translation_time, avg_query_time))
        return candidates