def __init__(self, game, row, col): self.position = 0 self.cells = [ Cell(game.grid, row - 1, col + 0, UI['color']['red']), Cell(game.grid, row + 0, col + 0, UI['color']['red']), Cell(game.grid, row + 1, col + 0, UI['color']['red']), Cell(game.grid, row + 1, col + 1, UI['color']['red']) ] Shape.__init__(self, game)
def test_relative_horizontal_position(self): Cell.switchToContentRepr() log.info("Testing relative horizontal position") g = Grid() a = Cell(startRow=1, startCol=1, rowSpan=3, colSpan=3, content="a") b = Cell(startRow=4, startCol=1, rowSpan=1, colSpan=3, content="b") c = Cell(startRow=0, startCol=0, rowSpan=1, colSpan=2, content="c") d = Cell(startRow=4, startCol=4, rowSpan=1, colSpan=1, content="d") e = Cell(startRow=1, startCol=0, rowSpan=2, colSpan=1, content="e") g.addCell(e) g.addCell(a) g.addCell(b) g.addCell(c) g.addCell(d) print(g) self.assertEqual(features.relativeHorizontalPosition(c, a, None), 0) self.assertEqual(features.relativeHorizontalPosition(e, a, None), 1) self.assertEqual(features.relativeHorizontalPosition(a, b, None), 0) self.assertEqual(features.relativeHorizontalPosition(a, d, None), 1)
def test_below_empty_row(self): Cell.switchToContentRepr() log.info("Testing below empty row") g = Grid() a = Cell(startRow=1, startCol=1, rowSpan=3, colSpan=5, content="a") b = Cell(startRow=4, startCol=1, rowSpan=1, colSpan=3, content="b") c = Cell(startRow=0, startCol=0, rowSpan=1, colSpan=2, content="c") d = Cell(startRow=4, startCol=4, rowSpan=1, colSpan=1, content="d") e = Cell(startRow=1, startCol=0, rowSpan=2, colSpan=1, content="e") g.addCell(e) g.addCell(a) g.addCell(b) g.addCell(c) g.addCell(d) Cell.switchToContentRepr() print(g) self.assertEqual(features.belowEmptyRow(e, c, g), -1) self.assertEqual(features.belowEmptyRow(c, e, g), 1) self.assertEqual(features.belowEmptyRow(a, b, g), 0) self.assertEqual(features.belowEmptyRow(c, b, g), 1)
def test_vertically_spanning(self): Cell.switchToContentRepr() log.info("Testing Vertically Spanned") g = Grid() a = Cell(startRow=1, startCol=1, rowSpan=3, colSpan=5, content="a") b = Cell(startRow=4, startCol=1, rowSpan=1, colSpan=3, content="b") c = Cell(startRow=0, startCol=0, rowSpan=1, colSpan=2, content="c") d = Cell(startRow=4, startCol=4, rowSpan=1, colSpan=1, content="d") g.addCell(a) g.addCell(b) g.addCell(c) g.addCell(d) print(g) self.assertEqual(features.verticallySpanned(a, b, None), 1) self.assertEqual(features.verticallySpanned(b, a, None), -1) self.assertEqual(features.verticallySpanned(c, a, None), 0) self.assertEqual(features.verticallySpanned(b, c, None), 0) self.assertEqual(features.verticallySpanned(a, c, None), 0) self.assertEqual(features.verticallySpanned(a, d, None), 1) self.assertEqual(features.verticallySpanned(d, a, None), -1)
def test_spanned_by_same_cell(self): Cell.switchToContentRepr() log.info("Testing spanned by same cell") g = Grid() a = Cell(startRow=1, startCol=1, rowSpan=3, colSpan=5, content="a") b = Cell(startRow=4, startCol=1, rowSpan=1, colSpan=3, content="b") c = Cell(startRow=2, startCol=6, rowSpan=1, colSpan=1, content="c") d = Cell(startRow=4, startCol=4, rowSpan=1, colSpan=1, content="d") e = Cell(startRow=1, startCol=6, rowSpan=2, colSpan=1, content="e") g.addCell(e) g.addCell(a) g.addCell(b) g.addCell(c) g.addCell(d) Cell.switchToContentRepr() print(g) self.assertEqual(features.spannedBySameCell(b, d, g), 1) self.assertEqual(features.spannedBySameCell(c, e, g), 1) self.assertEqual(features.belowEmptyRow(a, b, g), 0) self.assertEqual(features.belowEmptyRow(c, b, g), 0)
def _init_grid(self): ''' documentation needed! ''' self.north_east = Cell(0, 0) cell = self.north_east row = self.north_east last_row = None last_row_cell = None while True: while (cell.column + 1) < self.columns: new_cell = Cell(row.row, cell.column + 1) if last_row_cell: last_row_cell = last_row_cell.east new_cell.north = last_row_cell last_row_cell.south = new_cell new_cell.west = cell cell.east = new_cell cell = new_cell if (row.row + 1) < self.rows: last_row = row row = Cell(row.row + 1, 0) if last_row: row.north = last_row last_row.south = row cell = row last_row_cell = last_row else: break
def explore(tableGrid, c1, direction, x1, x2, y1, y2, parent, mode): # nonlocal exploredWindow # nonlocal stubCell print(c1, direction, x1, x2, y1, y2, parent, mode) exploredWindow[c1] = (x1, x2, y1, y2) # # print(exploredWindow) if direction == "v" and x1 < x2: #cursor j = y1 i = x1 while j < y2: print(i,j) c2 = tableGrid.getCell(i,j) #in case where the next row is all empty if Cell.isEmptyCell(c2) and i < x2: i += 1 continue j += c2.colSpan #if c2 is not explored yet if not c2 in exploredWindow: #neither c1 and c2 should be empty if not Cell.isEmptyCell(c1): l = predict(c1, c2) print(c1, c2, l) #append c2 as a child of c1 if relationship is left superior if l == "L": c1.addChild(c2) #if two cells are unrelated, find ancestor of c1 that is sibling to c2 elif l == "U": ct = c1.parentList[0] #include case where c1 is L of c2 while ct and predict(ct, c2) != "S": # print("CT", ct) ct = ct.parentList[0] # print("CT", ct) if ct and ct.parentList[0]: ct.parentList[0].addChild(c2) #if nothing is found, then consider it a child of the stub cell, given that it exists elif not Cell.isEmptyCell(stubCell) and c2.function == Cell.COL_HEADER_CELL: # print(c2.function, c2) stubCell.addChild(c2) #they have a common parent give that it exists elif l == "S": print(c1.parentList) if c1.parentList[0]: c1.parentList[0].addChild(c2) #mode is whether you are searching within a header row or a header column if mode == "column": explore(tableGrid, c2, "h", c2.startRow, c2.startRow + c2.rowSpan, c2.startCol + c2.colSpan, min(exploredWindow[c2.parentList[0]][3], exploredWindow[stubCell if not Cell.isEmptyCell(stubCell) else None][3]), c1, mode) explore(tableGrid, c2, "v", c2.startRow + c2.rowSpan, min(exploredWindow[c1.parentList[0]][1],exploredWindow[stubCell if not Cell.isEmptyCell(stubCell) else None][1]), c2.startCol, min(c2.startCol + c2.colSpan, exploredWindow[stubCell if not Cell.isEmptyCell(stubCell) else None][3]), c1, mode) elif mode == "row": explore(tableGrid, c2, "h", c2.startRow, c2.startRow + c2.rowSpan, c2.startCol + c2.colSpan, min(exploredWindow[c2.parentList[0]][3], exploredWindow[c1][3]), c1, mode) explore(tableGrid, c2, "v", c2.startRow + c2.rowSpan, exploredWindow[stubCell if not Cell.isEmptyCell(stubCell) else None][1], c2.startCol, c2.startCol + c2.colSpan, c1, mode) # else: # print("visited", c2) elif direction == "h" and y1 < y2: j = y1 i = x1 while i < x2: c2 = tableGrid.getCell(i,j) i += c2.rowSpan if Cell.isEmptyCell(c2): j += 1 continue if not c2 in exploredWindow: if not Cell.isEmptyCell(c1): l = predict(c1, c2) print(c1, c2, l) if l == "L": c1.addChild(c2) elif l == "U": ct = c1.parentList[0] while ct and predict(ct, c2) != "S": # print("CT", ct) ct = ct.parentTableExtractionList[0] # print("CT", ct) if ct and ct.parentList[0]: ct.parentList[0].addChild(c2) elif not Cell.isEmptyCell(stubCell) and c2.function == Cell.COL_HEADER_CELL: # print(c2.function, c2) stubCell.addChild(c2) elif l == "S": print(c1.parentList) if c1.parentList[0]: c1.parentList[0].addChild(c2) if mode == "column": explore(tableGrid, c2, "v", c2.startRow + c2.rowSpan, min(exploredWindow[c1.parentList[0]][1],exploredWindow[stubCell if not Cell.isEmptyCell(stubCell) else None][1]), c2.startCol, min(c2.startCol + c2.colSpan, exploredWindow[stubCell if not Cell.isEmptyCell(stubCell) else None][3]), c1, mode) explore(tableGrid, c2, "h", c2.startRow, c2.startRow + c2.rowSpan, c2.startCol + c2.colSpan, min(exploredWindow[c2.parentList[0]][3], exploredWindow[stubCell if not Cell.isEmptyCell(stubCell) else None][3]), c1, mode) elif mode == "row": explore(tableGrid, c2, "v", c2.startRow + c2.rowSpan, exploredWindow[stubCell if not Cell.isEmptyCell(stubCell) else None][1], c2.startCol, c2.startCol + c2.colSpan, c1, mode) explore(tableGrid, c2, "h", c2.startRow, c2.startRow + c2.rowSpan, c2.startCol + c2.colSpan, exploredWindow[c2.parentList[0]][3], c1, mode)
from sklearn.externals import joblib from sklearn import svm from sklearn import linear_model import pickle import sys, optparse from pprint import pprint parser = optparse.OptionParser() parser.add_option('-f', '--fileID', dest = "fileID", help='file index') parser.add_option('-t', '--tableID', dest = "tableID", help='table index') parser.add_option('-d', '--dataset', dest = "dataset", help='eu or us dataset') parser.add_option('-g', '--persistedModel', dest = "persistedModel", help='persisted model') parser.add_option('-a', '--all', action = "store_true", help='construct all') (options, args) = parser.parse_args() Cell.switchToIDRepr() # clf = pickle.load(open("/Users/runyuzhang/Desktop/TableExtraction/data/storage/tree_us.pkl", "rb")) clf = joblib.load(options.persistedModel) def construct(tableGrid): fvg = FeatureVectorGenerator(tableGrid) stubCell = tableGrid.getStubCell() pprint(vars(stubCell)) explored = set()
# directions RIGHT = 6 LEFT = 4 UP = 8 DOWN = 2 NONE = 5 # key mapping KEY_UP = "'\\x1b[A'" KEY_DOWN = "'\\x1b[B'" KEY_LEFT = "'\\x1b[D'" KEY_RIGHT = "'\\x1b[C'" KEY_QUIT = "'q'" # cell content EMPTY = Cell(" ") FOOD = Cell("X") SNAKE_BODY = Cell("o") SNAKE_TAIL = Cell("*") SNAKE_UP = Cell("ʌ") SNAKE_DOWN = Cell("v") SNAKE_LEFT = Cell("<") SNAKE_RIGHT = Cell(">") DIMENSION = 9 class Snake: key = None