def __init__(self): super(FixedType, self).__init__() if self.fixedtype is None: raise ValueError("You *must* define the `fixedtype` " "class attribute to use this class.") self.fixedtype = encoding_flexible_conversion(self.fixedtype) self.fixedtyperelation = encoding_flexible_conversion(self.fixedtyperelation) self.add_data(self.fixedtyperelation, self._hide_node(self.fixedtype))
def __init__(self): super(FixedType, self).__init__() if self.fixedtype is None: raise ValueError("You *must* define the `fixedtype` " "class attribute to use this class.") self.fixedtype = encoding_flexible_conversion(self.fixedtype) self.fixedtyperelation = \ encoding_flexible_conversion(self.fixedtyperelation) self.add_data(self.fixedtyperelation, self._hide_node(self.fixedtype))
def __init__(self, data): super(FixedDataRelation, self).__init__() if self.relation is None: raise ValueError("You *must* define the `relation` " "class attribute to use this class.") self.relation = encoding_flexible_conversion(self.relation) if self.language is not None: self.language = encoding_flexible_conversion(self.language) data = u'"{0}"@{1}'.format(data, self.language) self.add_data(self.relation, data)
def __init__(self, data): super(FixedDataRelation, self).__init__() if self.relation is None: raise ValueError("You *must* define the `relation` " "class attribute to use this class.") self.relation = encoding_flexible_conversion(self.relation) if self.language is not None: self.language = encoding_flexible_conversion(self.language) data = u"\"{0}\"@{1}".format(data, self.language) self.add_data(self.relation, data)
def safely_to_unicode(x): """ Given an "edge" (a relation) or "a data" from an `Expression` graph transform it into a unicode string fitted for insertion into a MQL query. """ if isinstance(x, unicode): return x if isinstance(x, str): return encoding_flexible_conversion(x) if isinstance(x, IsRelatedTo): return u"/type/reflect/any_master" return unicode(x) # FIXME: Any object is unicode-able, this is error prone
def _save_settings_values(self): """ Persists the settings values of the app to the settings module so it can be accesible from another part of the software. """ for key in dir(self._settings_module): if key.upper() == key: value = getattr(self._settings_module, key) if isinstance(value, str): value = encoding_flexible_conversion(value) setattr(settings, key, value)
def _predicate_sum_from_string(string, predicate): assert issubclass(predicate, Predicate) string = encoding_flexible_conversion(string) words = string.split() result = None for word in words: if result is None: result = predicate(word) else: result += predicate(word) return result
def get_query(self): """ Given `question` in natural language, it returns three things: - the target of the query in string format - the query - metadata given by the regex programmer (defaults to None) The queries returned corresponds to the regexes that match in weight order. """ question = encoding_flexible_conversion(self.subquestions) for expression, metadata in self._iter_compiled_forms(question): target, query = generation.get_code(expression, settings.LANGUAGE) message = u"Interpretation {1}: {0}" logger.debug(message.format(str(expression), expression.rule_used)) logger.debug(u"Query generated: {0}".format(query)) return target, query, metadata
def get_queries(self, question): """ Given `question` in natural language, it returns three things: - the target of the query in string format - the query - metadata given by the regex programmer (defaults to None) The queries returned corresponds to the regexes that match in weight order. """ question = encoding_flexible_conversion(question) for expression, userdata in self._iter_compiled_forms(question): target, sparql_query = expression_to_sparql(expression) logger.debug(u"Semantics {1}: {0}".format(str(expression), expression.rule_used)) logger.debug(u"Query generated: {0}".format(sparql_query)) yield target, sparql_query, userdata
def get_queries(self, question, query_lang='sparql'): """ Given `question` in natural language, it returns three things: - the target of the query in string format - the query - metadata given by the regex programmer (defaults to None) The queries returned corresponds to the regexes that match in weight order. """ printout_func = None if self._printout_module: try: printout_func = getattr( self._printout_module, "expression_to_%s" % query_lang.lower()) except AttributeError: pass if not printout_func: try: printout_func = getattr( sys.modules["quepy.printout"], "expression_to_%s" % query_lang.lower()) except AttributeError: pass if printout_func: question = encoding_flexible_conversion(question) for expression, userdata in self._iter_compiled_forms(question): target = None query = printout_func(expression) logger.debug(u"Semantics {1}: {0}".format( str(expression), expression.rule_used)) logger.debug(u"Query generated: {0}".format(query)) yield target, query, userdata else: logger.error(u"Can't find an expression serialization for: '%s'", query_lang)
def get_queries(self, question, query_lang='sparql'): """ Given `question` in natural language, it returns three things: - the target of the query in string format - the query - metadata given by the regex programmer (defaults to None) The queries returned corresponds to the regexes that match in weight order. """ printout_func = None if self._printout_module: try: printout_func = getattr(self._printout_module, "expression_to_%s" % query_lang.lower()) except AttributeError: pass if not printout_func: try: printout_func = getattr(sys.modules["quepy.printout"], "expression_to_%s" % query_lang.lower()) except AttributeError: pass if printout_func: question = encoding_flexible_conversion(question) for expression, userdata in self._iter_compiled_forms(question): target = None query = printout_func(expression) logger.debug(u"Semantics {1}: {0}".format(str(expression), expression.rule_used)) logger.debug(u"Query generated: {0}".format(query)) yield target, query, userdata else: logger.error(u"Can't find an expression serialization for: '%s'", query_lang)
def __init__(self, tag): tag = encoding_flexible_conversion(tag) self.tag = tag super(Pos, self).__init__(self._predicate) self.arg = tag
def _get_subquery_by_subquestion_and_rule(self, subquestion, rule): question = encoding_flexible_conversion(' '.join(subquestion)) tagger = get_tagger() words = list(tagger(question)) subquery_expression, meta = rule.get_interpretation(words) return get_core_sparql_expression(subquery_expression)
def get_queries(self, question): """ Given `question` in natural language, it returns :type self: object three things: - the target of the query in string format - the query - metadata given by the regex programmer (defaults to None) The queries returned corresponds to the regexes that match in weight order. """ """ parse the question so that we split it if it hase "or" or "and" in it *( maybe on "," also at a later date) """ question = encoding_flexible_conversion(question) questions = question.split(" and ", (-1)) expr = Expression() first_time = True index = 0 #print questions toBeReturned = ReturnModel(None, None) for question in questions: for expression, userdata in self._iter_compiled_forms(question): if first_time: #print expression.rule_used toBeReturned.rule_used = expression.rule_used #print userdata """ -- it wont work for all the question or it will but we need more parrsing -- base on the type of the expression.rule_used we can take actions to connect the next expressions to the curent one if they are more """ if len(questions) > (index + 1): if expression.rule_used == "WhoAreChildrensOfQuestion": print "**************=Next query=**************************" temp_data = question.split(" ", (-1)) print temp_data questions[index + 1] = _new_query_string( temp_data, questions[index + 1], userdata.i, userdata.j) print(questions[index + 1]) message = u"Interpretation {1}: {0}" print(message.format(str(expression), expression.rule_used)) first_time = False expr = expression expr.rule_used = expression else: """ will have to see if there is more to parse form question if there are mor conditions in order to make the next question base on the first query ! """ expr += expression index += 1 target, query = generation.get_code(expr, self.language) toBeReturned.query = query yield toBeReturned print(u"Query generated: {0}".format(query))