Add docstrings and update docs
This commit is contained in:
+13
-8
@@ -1,18 +1,23 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
VERSION = (0, 4, 0)
|
||||
PHASE = 'alpha' # alpha, beta or rc
|
||||
PHASE_VERSION = '1'
|
||||
PRERELEASE = None # alpha, beta or rc
|
||||
REVISION = None
|
||||
|
||||
|
||||
def generate_version(version, prerelease=None, revision=None):
|
||||
version_parts = ['.'.join(map(str, version))]
|
||||
if prerelease is not None:
|
||||
version_parts.append('-{}'.format(prerelease))
|
||||
if revision is not None:
|
||||
version_parts.append('.{}'.format(revision))
|
||||
return ''.join(version_parts)
|
||||
|
||||
|
||||
__title__ = 'camelot-py'
|
||||
__description__ = 'PDF Table Extraction for Humans.'
|
||||
__url__ = 'http://camelot-py.readthedocs.io/'
|
||||
if PHASE:
|
||||
__version__ = '{}-{}'.format('.'.join(map(str, VERSION)), PHASE)
|
||||
if PHASE_VERSION:
|
||||
__version__ = '{}.{}'.format(__version__, PHASE_VERSION)
|
||||
else:
|
||||
__version__ = '.'.join(map(str, VERSION))
|
||||
__version__ = generate_version(VERSION, prerelease=PRERELEASE, revision=REVISION)
|
||||
__author__ = 'Vinayak Mehta'
|
||||
__author_email__ = 'vmehta94@gmail.com'
|
||||
__license__ = 'MIT License'
|
||||
|
||||
+51
-1
@@ -20,6 +20,29 @@ TABLE_AREA_PADDING = 10
|
||||
|
||||
|
||||
class TextEdge(object):
|
||||
"""Defines a text edge coordinates relative to a left-bottom
|
||||
origin. (PDF coordinate space)
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : float
|
||||
x-coordinate of the text edge.
|
||||
y0 : float
|
||||
y-coordinate of bottommost point.
|
||||
y1 : float
|
||||
y-coordinate of topmost point.
|
||||
align : string, optional (default: 'left')
|
||||
{'left', 'right', 'middle'}
|
||||
|
||||
Attributes
|
||||
----------
|
||||
intersections: int
|
||||
Number of intersections with horizontal text rows.
|
||||
is_valid: bool
|
||||
A text edge is valid if it intersections with at least
|
||||
TEXTEDGE_REQUIRED_ELEMENTS horizontal text rows.
|
||||
|
||||
"""
|
||||
def __init__(self, x, y0, y1, align='left'):
|
||||
self.x = x
|
||||
self.y0 = y0
|
||||
@@ -33,6 +56,9 @@ class TextEdge(object):
|
||||
round(self.x, 2), round(self.y0, 2), round(self.y1, 2), self.align, self.is_valid)
|
||||
|
||||
def update_coords(self, x, y0):
|
||||
"""Updates the text edge's x and bottom y coordinates and sets
|
||||
the is_valid attribute.
|
||||
"""
|
||||
if np.isclose(self.y0, y0, atol=TEXTEDGE_EXTEND_TOLERANCE):
|
||||
self.x = (self.intersections * self.x + x) / float(self.intersections + 1)
|
||||
self.y0 = y0
|
||||
@@ -44,11 +70,18 @@ class TextEdge(object):
|
||||
|
||||
|
||||
class TextEdges(object):
|
||||
"""Defines a dict of left, right and middle text edges found on
|
||||
the PDF page. The dict has three keys based on the alignments,
|
||||
and each key's value is a list of camelot.core.TextEdge objects.
|
||||
"""
|
||||
def __init__(self):
|
||||
self._textedges = {'left': [], 'middle': [], 'right': []}
|
||||
self._textedges = {'left': [], 'right': [], 'middle': []}
|
||||
|
||||
@staticmethod
|
||||
def get_x_coord(textline, align):
|
||||
"""Returns the x coordinate of a text row based on the
|
||||
specified alignment.
|
||||
"""
|
||||
x_left = textline.x0
|
||||
x_right = textline.x1
|
||||
x_middle = x_left + (x_right - x_left) / 2.0
|
||||
@@ -56,12 +89,17 @@ class TextEdges(object):
|
||||
return x_coord[align]
|
||||
|
||||
def find(self, x_coord, align):
|
||||
"""Returns the index of an existing text edge using
|
||||
the specified x coordinate and alignment.
|
||||
"""
|
||||
for i, te in enumerate(self._textedges[align]):
|
||||
if np.isclose(te.x, x_coord, atol=0.5):
|
||||
return i
|
||||
return None
|
||||
|
||||
def add(self, textline, align):
|
||||
"""Adds a new text edge to the current dict.
|
||||
"""
|
||||
x = self.get_x_coord(textline, align)
|
||||
y0 = textline.y0
|
||||
y1 = textline.y1
|
||||
@@ -69,6 +107,8 @@ class TextEdges(object):
|
||||
self._textedges[align].append(te)
|
||||
|
||||
def update(self, textline):
|
||||
"""Updates an existing text edge in the current dict.
|
||||
"""
|
||||
for align in ['left', 'right', 'middle']:
|
||||
x_coord = self.get_x_coord(textline, align)
|
||||
idx = self.find(x_coord, align)
|
||||
@@ -78,11 +118,18 @@ class TextEdges(object):
|
||||
self._textedges[align][idx].update_coords(x_coord, textline.y0)
|
||||
|
||||
def generate(self, textlines):
|
||||
"""Generates the text edges dict based on horizontal text
|
||||
rows.
|
||||
"""
|
||||
for tl in textlines:
|
||||
if len(tl.get_text().strip()) > 1: # TODO: hacky
|
||||
self.update(tl)
|
||||
|
||||
def get_relevant(self):
|
||||
"""Returns the list of relevant text edges (all share the same
|
||||
alignment) based on which list intersects horizontal text rows
|
||||
the most.
|
||||
"""
|
||||
intersections_sum = {
|
||||
'left': sum(te.intersections for te in self._textedges['left'] if te.is_valid),
|
||||
'right': sum(te.intersections for te in self._textedges['right'] if te.is_valid),
|
||||
@@ -96,6 +143,9 @@ class TextEdges(object):
|
||||
return self._textedges[relevant_align]
|
||||
|
||||
def get_table_areas(self, textlines, relevant_textedges):
|
||||
"""Returns a dict of interesting table areas on the PDF page
|
||||
calculated using relevant text edges.
|
||||
"""
|
||||
def pad(area, average_row_height):
|
||||
x0 = area[0] - TABLE_AREA_PADDING
|
||||
y0 = area[1] - TABLE_AREA_PADDING
|
||||
|
||||
@@ -247,10 +247,13 @@ class Stream(BaseParser):
|
||||
" should be equal")
|
||||
|
||||
def _nurminen_table_detection(self, textlines):
|
||||
# a general heuristic implementation of the table detection
|
||||
# algorithm described by Anssi Nurminen's master's thesis:
|
||||
# https://dspace.cc.tut.fi/dpub/bitstream/handle/123456789/21520/Nurminen.pdf?sequence=3
|
||||
# assumes that tables are situated relatively apart vertically
|
||||
"""A general implementation of the table detection algorithm
|
||||
described by Anssi Nurminen's master's thesis.
|
||||
Link: https://dspace.cc.tut.fi/dpub/bitstream/handle/123456789/21520/Nurminen.pdf?sequence=3
|
||||
|
||||
Assumes that tables are situated relatively far apart
|
||||
vertically.
|
||||
"""
|
||||
|
||||
# TODO: add support for arabic text #141
|
||||
# sort textlines in reading order
|
||||
|
||||
Reference in New Issue
Block a user