def generalize_question(a, b, parser=None): # replace entity mention in question with a generic symbol if parser is None: parser = LC_Qaud_LinkedParser() _, _, uris = parser.parse_sparql(b) uris = [uri for uri in uris if uri.is_entity()] i = 0 for item in find_mentions(a, uris): a = "{} #en{} {}".format(a[:item["start"]], "t" * (i + 1), a[item["end"]:]) b = b.replace(item["uri"].raw_uri, "#en{}".format("t" * (i + 1))) # remove extra info from the relation's uri and remaining entities for item in [ "http://dbpedia.org/resource/", "http://dbpedia.org/ontology/", "http://dbpedia.org/property/", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ]: b = b.replace(item, "") b = b.replace("<", "").replace(">", "") return a, b
def do(self, qapair, force_gold=False, top=5): entities = [] relations = [] for u in qapair.sparql.uris: question = qapair.question.text mentions = find_mentions(question, [u]) surface = "" if len(mentions) > 0: surface = question[mentions[0]["start"]:mentions[0]["end"]] linked_item = LinkedItem(surface, [u]) if u.is_entity(): entities.append(linked_item) if u.is_ontology(): relations.append(linked_item) return entities, relations
def generalize_question(a, b): # replace entity mention in question with a generic symbol parser = WebQSPParser() _, _, uris = parser.parse_sparql(b) uris = [uri for uri in uris if uri.is_entity()] i = 0 for item in find_mentions(a, uris): a = "{} #en{} {}".format(a[:item["start"]], "t" * (i + 1), a[item["end"]:]) b = b.replace(item["uri"].raw_uri, "#en{}".format("t" * (i + 1))) # remove extra info from the relation's uri and remaining entities for item in ["ns:"]: b = b.replace(item, "") b = b.replace("<", "").replace(">", "") return a, b