Ejemplo n.º 1
0
def test_load():
    s = biocjson.fromJSON(json.loads(json_str), bioc.SENTENCE)
    g = semgraph.load(s)
    assert len(g) == 16
    assert g.size() == 15

    s = bioc.BioCSentence()
    g = semgraph.load(s)
    assert len(g) == 0
    assert g.size() == 0
Ejemplo n.º 2
0
 def detect(self, sentence, locs):
     """
     Args:
         sentence(BioCSentence): a sentence with universal dependencies
         locs(list): a list of (begin, end)
     Yields:
         (str, MatcherObj, (begin, end)): negation or uncertainty, matcher, matched annotation
     """
     try:
         g = semgraph.load(sentence)
         propagator.propagate(g)
     except:
         logging.exception(
             'Cannot parse dependency graph [offset={}]'.format(
                 sentence.offset))
         raise
     else:
         if self.sentence_rule and is_neg_graph1(g):
             for loc in locs:
                 yield NEGATION, None, loc
             return
         for loc in locs:
             if self.sentence_rule and is_neg_graph2(g, loc[0], loc[1]):
                 yield NEGATION, None, loc
             for node in find_nodes(g, loc[0], loc[1]):
                 m = self.match_neg(g, node)
                 if m:
                     yield NEGATION, m, loc
                 m = self.match_uncertainty(g, node)
                 if m:
                     yield UNCERTAINTY, m, loc
Ejemplo n.º 3
0
    def graph_detect(self, sentence, locs):
        """Detect rules in report sentences.

        Args:
            sentence(BioCSentence): a sentence with universal dependencies
            locs(list): a list of (begin, end)

        Return:
            (str, MatcherObj, (begin, end)): negation or uncertainty,
            matcher, matched annotation
        """
        try:
            g = semgraph.load(sentence)
            propagator.propagate(g)
        except Exception:
            logging.exception('Cannot parse dependency graph [offset=%s]',
                              sentence.offset)
            raise
        else:
            for loc in locs:
                for node in find_nodes(g, loc[0], loc[1]):
                    # Match pre-negation uncertainty rules first.
                    preneg_m = self.match_prenegation_uncertainty(g, node)
                    if preneg_m:
                        yield UNCERTAINTY, preneg_m, loc
                    else:
                        # Then match negation rules.
                        neg_m = self.match_neg(g, node)
                        if neg_m:
                            yield NEGATION, neg_m, loc
                        else:
                            # Finally match post-negation uncertainty rules.
                            postneg_m = self.match_uncertainty(g, node)
                            if postneg_m:
                                yield UNCERTAINTY, postneg_m, loc
Ejemplo n.º 4
0
def _read_graph():
    s = biocjson.fromJSON(json.loads(json_str), bioc.SENTENCE)
    g = semgraph.load(s)
    return g