def run(pipeline): '''Run a complete pipeline of graph operations. Parameters ---------- pipeline : dict The pipeline description. Returns ------- The result from running the pipeline with the provided arguments. ''' # INPUT if 'graph' in pipeline: graph = pipeline['graph'] elif 'text' in pipeline: ts = [] try: ts.append(pipeline.get('parser', DEF_PARSER) + '_parse') except KeyError: pass try: ts += pipeline.get('transformers', DEF_TRANSFORMERS) T = transformers.get_pipeline(ts) except KeyError: raise ValueError("Unknown transformer pipeline") T_args = pipeline.get('transformer_args', DEF_T_ARGS) graph = CG(transformer=T, transformer_args=T_args, text=pipeline['text']) else: raise ValueError('Must provide either graph or text') # OPERATIONS for operation in pipeline.get('operations', []): try: name = operation.pop('op') except KeyError: raise ValueError("No name for the operation") try: graph = operate(graph, name, **operation) except TypeError as e: raise ValueError(e) operation['op'] = name # OUTPUT if 'linearizers' in pipeline: try: L = linearizers.get_pipeline( pipeline.get('linearizers', DEF_LINEARIZERS)) except KeyError: raise ValueError("Unknown linearizer pipeline") L_args = pipeline.get('linearizer_args', DEF_L_ARGS) return graph.linearize(linearizer=L, linearizer_args=L_args) else: return graph
def run (pipeline): '''Run a complete pipeline of graph operations. Parameters ---------- pipeline : dict The pipeline description. Returns ------- The result from running the pipeline with the provided arguments. ''' # INPUT if 'graph' in pipeline: graph = pipeline['graph'] elif 'text' in pipeline: ts = [] try: ts.append(pipeline.get('parser', DEF_PARSER)+'_parse') except KeyError: pass try: ts += pipeline.get('transformers', DEF_TRANSFORMERS) T = transformers.get_pipeline(ts) except KeyError: raise ValueError("Unknown transformer pipeline") T_args = pipeline.get('transformer_args', DEF_T_ARGS) graph = CG(transformer=T,transformer_args=T_args,text=pipeline['text']) else: raise ValueError('Must provide either graph or text') # OPERATIONS for operation in pipeline.get('operations', []): try: name = operation.pop('op') except KeyError: raise ValueError("No name for the operation") try: graph = operate(graph, name, **operation) except TypeError as e: raise ValueError(e) operation['op'] = name # OUTPUT if 'linearizers' in pipeline: try: L = linearizers.get_pipeline(pipeline.get('linearizers', DEF_LINEARIZERS)) except KeyError: raise ValueError("Unknown linearizer pipeline") L_args = pipeline.get('linearizer_args', DEF_L_ARGS) return graph.linearize(linearizer=L,linearizer_args=L_args) else: return graph
class Analysis: def __init__(self, text, superficial=False, autocorrect=True): # Corrects posible misspelled words in the queries if autocorrect: self.text = correct_phrase(text) else: self.text = text if superficial: # Parses the query without generating the semantic graph self.parse = nlp(self.text) else: # Deeply parses the query generating the semantic graph self.graph = CG(transformer=semantic_analyzer, text=self.text) self.parse = self.graph.spacy_parse #Returns the similarity between this object and another of its same type. def similarity(self, other): return self.parse.similarity(other.parse) #Returns the most relevant terms from the analyzed text represented by this object. def content_words(self): return [ tok.lemma_ for tok in self.parse if not (tok.is_stop or tok.is_punct) ] # Generates MATCH queries to solve the question introduce by the user def cypher_query(self, ground_id): if self.graph.question_type == closed: return self.graph.linearize(linearizer=cypher_closed_linearizer, linearizer_args={ 'cypher_extra_params': { 'ground_id': ground_id } }) elif self.graph.question_type == open: return self.graph.linearize(linearizer=cypher_open_linearizer, linearizer_args={ 'cypher_extra_params': { 'ground_id': ground_id } }) # Generates CREATE queries to add the information to the NEO4J graph database def cypher_create(self, ground_id): return self.graph.linearize( linearizer=cypher_create_linearizer, linearizer_args={'cypher_extra_params': { 'ground_id': ground_id }})
class Analysis: def __init__(self, text, superficial=False, autocorrect=True): if autocorrect: self.text = correct_phrase(text) else: self.text = text if superficial: self.parse = nlp(self.text) else: self.graph = CG(transformer=semantic_analyzer, text=self.text) self.parse = self.graph.spacy_parse def similarity(self, other): return self.parse.similarity(other.parse) def content_words(self): return [ tok.lemma_ for tok in self.parse if not (tok.is_stop or tok.is_punct) ] def cypher_query(self, ground_id): if self.graph.question_type == closed: return self.graph.linearize(linearizer=cypher_closed_linearizer, linearizer_args={ 'cypher_extra_params': { 'ground_id': ground_id } }) elif self.graph.question_type == open: return self.graph.linearize(linearizer=cypher_open_linearizer, linearizer_args={ 'cypher_extra_params': { 'ground_id': ground_id } }) def cypher_create(self, ground_id): return self.graph.linearize( linearizer=cypher_create_linearizer, linearizer_args={'cypher_extra_params': { 'ground_id': ground_id }})
def __init__(self, text, superficial=False, autocorrect=True): if autocorrect: self.text = correct_phrase(text) else: self.text = text if superficial: self.parse = nlp(self.text) else: self.graph = CG(transformer=semantic_analyzer, text=self.text) self.parse = self.graph.spacy_parse
def __init__(self, text, superficial=False, autocorrect=True): # Corrects posible misspelled words in the queries if autocorrect: self.text = correct_phrase(text) else: self.text = text if superficial: # Parses the query without generating the semantic graph self.parse = nlp(self.text) else: # Deeply parses the query generating the semantic graph self.graph = CG(transformer=semantic_analyzer, text=self.text) self.parse = self.graph.spacy_parse
def operate(graph, **args): subgraph, main_entity = spot_domain(graph) r = CG(graph, subgraph=subgraph) r.gram['main_entity'] = main_entity return r
def compose_answer(question, answer_graph): g = CG(original=question.graph) graft(g, question.graph.questions[0], answer_graph, answer_graph.roots[0]) return (g.linearize(linearizer=nlg))
def operate (graph, **args): subgraph, main_entity = spot_domain(graph) r = CG(graph, subgraph=subgraph) r.gram['main_entity'] = main_entity return r