def test_get_products_for_empty_graph(self):
     """ Test get products for an empty DependencyGraph """
     str_answer = "[]"
     graph_dep = DependencyGraph.DependencyGraph()
     str_result = str(
         sorted([
             vtx_cur.func_detail()
             for vtx_cur in graph_dep.func_get_products() if vtx_cur
         ]))
     self.func_test_equals(str_answer, str_result)
 def test_get_command_for_empty_graph(self):
     """ Test get commands for empty Dependency graph. """
     str_answer = "[]"
     graph_dep = DependencyGraph.DependencyGraph()
     str_result = str(
         sorted([
             vtx_cur.str_id for vtx_cur in graph_dep.func_get_commands()
             if vtx_cur
         ]))
     self.func_test_equals(str_answer, str_result)
 def func_make_simple_graph(self):
     """
      -----R1------
     |      |      |
     2      3      4
     |     | |     |
     R5    R6  R7 R8
     """
     vtx_one = Resource.Resource("/R1", False)
     vtx_two = Graph.Vertex(2)
     vtx_three = Graph.Vertex(3)
     vtx_four = Graph.Vertex(4)
     vtx_two.str_type = Command.STR_TYPE
     vtx_three.str_type = Command.STR_TYPE
     vtx_four.str_type = Command.STR_TYPE
     vtx_five = Resource.Resource("/R5", True)
     vtx_six = Resource.Resource("/R6", True)
     vtx_seven = Resource.Resource("/R7", True)
     vtx_eight = Resource.Resource("/R8", True)
     graph_dep = DependencyGraph.DependencyGraph()
     graph_dep.func_add_vertex(vtx_one)
     graph_dep.func_add_vertex(vtx_two)
     graph_dep.func_add_vertex(vtx_three)
     graph_dep.func_add_vertex(vtx_four)
     graph_dep.func_add_vertex(vtx_five)
     graph_dep.func_add_vertex(vtx_six)
     graph_dep.func_add_vertex(vtx_seven)
     graph_dep.func_add_vertex(vtx_eight)
     graph_dep.func_add_edge(vtx_one, vtx_two)
     graph_dep.func_add_edge(vtx_one, vtx_three)
     graph_dep.func_add_edge(vtx_one, vtx_four)
     graph_dep.func_add_edge(vtx_two, vtx_five)
     graph_dep.func_add_edge(vtx_three, vtx_six)
     graph_dep.func_add_edge(vtx_three, vtx_seven)
     graph_dep.func_add_edge(vtx_four, vtx_eight)
     return graph_dep
    def func_make_complex_graph(self):
        """
         -----R1------
        |      |      |
        2      3      4
        |     | |     |
        R5    R6  R7 R8

        R9
        |
        10
        |
        R11

        R12
        |    |
        13   14
        |    |
        R15  R16
          | |
           17
           |
          R18
        
        """
        vtx_one = Resource.Resource("/R1", False)
        vtx_two = Graph.Vertex(2)
        vtx_three = Graph.Vertex(3)
        vtx_four = Graph.Vertex(4)
        vtx_five = Resource.Resource("/R5", True)
        vtx_six = Resource.Resource("/R6", True)
        vtx_seven = Resource.Resource("/R7", True)
        vtx_eight = Resource.Resource("/R8", True)
        vtx_nine = Resource.Resource("/R9", True)
        vtx_ten = Graph.Vertex(10)
        vtx_eleven = Resource.Resource("/R11", True)
        vtx_twelve = Resource.Resource("/R12", True)
        vtx_thirteen = Graph.Vertex(13)
        vtx_fourteen = Graph.Vertex(14)
        vtx_fifteen = Resource.Resource("/R15", True)
        vtx_sixteen = Resource.Resource("/R16", True)
        vtx_seventeen = Graph.Vertex(17)
        vtx_eighteen = Resource.Resource("/R18", True)
        graph_dep = DependencyGraph.DependencyGraph()
        vtx_two.str_type = Command.STR_TYPE
        vtx_three.str_type = Command.STR_TYPE
        vtx_four.str_type = Command.STR_TYPE
        vtx_ten.str_type = Command.STR_TYPE
        vtx_thirteen.str_type = Command.STR_TYPE
        vtx_fourteen.str_type = Command.STR_TYPE
        vtx_seventeen.str_type = Command.STR_TYPE

        for vtx_add in [
                vtx_one, vtx_two, vtx_three, vtx_four, vtx_five, vtx_six,
                vtx_seven, vtx_eight, vtx_nine, vtx_ten, vtx_eleven,
                vtx_twelve, vtx_thirteen, vtx_fourteen, vtx_fifteen,
                vtx_sixteen, vtx_seventeen, vtx_eighteen
        ]:
            graph_dep.func_add_vertex(vtx_add)

        graph_dep.func_add_edge(vtx_one, vtx_two)
        graph_dep.func_add_edge(vtx_one, vtx_three)
        graph_dep.func_add_edge(vtx_one, vtx_four)
        graph_dep.func_add_edge(vtx_two, vtx_five)
        graph_dep.func_add_edge(vtx_three, vtx_six)
        graph_dep.func_add_edge(vtx_three, vtx_seven)
        graph_dep.func_add_edge(vtx_four, vtx_eight)
        graph_dep.func_add_edge(vtx_nine, vtx_ten)
        graph_dep.func_add_edge(vtx_ten, vtx_eleven)
        graph_dep.func_add_edge(vtx_twelve, vtx_thirteen)
        graph_dep.func_add_edge(vtx_thirteen, vtx_fifteen)
        graph_dep.func_add_edge(vtx_twelve, vtx_fourteen)
        graph_dep.func_add_edge(vtx_fourteen, vtx_sixteen)
        graph_dep.func_add_edge(vtx_fifteen, vtx_seventeen)
        graph_dep.func_add_edge(vtx_sixteen, vtx_seventeen)
        graph_dep.func_add_edge(vtx_seventeen, vtx_eighteen)
        return graph_dep
 def test_init_for_no_command(self):
     """ Test initialization with no commands """
     str_answer = "Graph:VERTEX{ ID=_i_am_Groot_;Parents=[];Children=[];Type=VERTEX }"
     graph_dep = DependencyGraph.DependencyGraph()
     str_result = graph_dep.func_detail()
     self.func_test_equals(str_answer, str_result)
예제 #6
0
    def __init__(self, dir, maxitems=0):
        self.dir = dir
        self.sentences = []
        self.sentenceIDs = {}

        if not os.path.exists(dir + "/sentences.tok"):
            sys.stderr.write("* FATAL: can't find '%s/sentences.tok'" % (dir))
            sys.exit(1)

        if not os.path.exists(dir + "/ids"):
            sys.stderr.write("* FATAL: can't find '%s/ids'" % (dir))
            sys.exit(1)

        if not os.path.exists(dir + "/corrections"):
            sys.stderr.write("* WARNING: can't find %s/corrections'" % (dir))


#            sys.exit(1)

        for i, (sid, line) in enumerate(
                izip(open(dir + "/ids"), open(dir + "/sentences.tok"))):
            sid = sid.rstrip()
            line = line.rstrip()

            self.sentences.append(Sentence(sid, line, basedir=self.dir))
            self.sentenceIDs[sid] = self.sentences[-1]
            if i == maxitems - 1:
                break

        if os.path.exists(dir + "/corrections"):
            for i, line in enumerate(open(dir + "/corrections")):

                # 829.3.1 52-53   Wcip    inhabitable     otherwise uninhabitable
                tokens = line.strip().split('\t')
                if len(tokens) == 6:
                    id, spanstr, code, bad, good, comments = tokens
                    i, j = spanstr.split('-')
                elif len(tokens) == 5:
                    id, spanstr, code, bad, good = tokens
                    i, j = spanstr.split('-')
                elif len(tokens) == 4:
                    id, spanstr, code, good = tokens
                    i, j = spanstr.split('-')
                    if not id in self.sentenceIDs:
                        continue
                    bad = " ".join(self.sentenceIDs[id].tokens[int(i):int(j)])
                else:
                    continue  # how to handle this?
                c = Correction(id, code, (i, j), bad, good.rstrip())

                if not id in self.sentenceIDs:
                    continue

                self.sentenceIDs[id].addCorrection(c)

        if os.path.exists(dir + "/sentences.parses"):
            for i, line in enumerate(open(dir + "/sentences.parses")):
                self.sentences[i].setParse(Tree.fromString(line.rstrip()))
                if i == maxitems - 1:
                    break

        if os.path.exists(dir + "/sentences.pos"):
            for i, line in enumerate(open(dir + "/sentences.pos")):
                self.sentences[i].setPOS(line.rstrip())
                if i == maxitems - 1:
                    break

        if os.path.exists(dir + "/sentences.dep"):
            for i, line in enumerate(open(dir + "/sentences.dep")):
                dg = DependencyGraph(line.replace('\\n', '\n'))
                if dg is not None: self.sentences[i].setDepgraph(dg)
                if i == maxitems - 1:
                    break