From 0b4f0d6abc1b3b59afd781d147af2f60be556603 Mon Sep 17 00:00:00 2001 From: Jeremy Singer-Vine Date: Wed, 2 Mar 2016 19:59:41 -0500 Subject: [PATCH] Move cluster functions to helpers.py Not proper utils, and not pandas-dependent. --- pdfplumber/helpers.py | 28 ++++++++++++++++++++++++++++ pdfplumber/utils.py | 38 +++----------------------------------- 2 files changed, 31 insertions(+), 35 deletions(-) create mode 100644 pdfplumber/helpers.py diff --git a/pdfplumber/helpers.py b/pdfplumber/helpers.py new file mode 100644 index 0000000..1584c06 --- /dev/null +++ b/pdfplumber/helpers.py @@ -0,0 +1,28 @@ +import itertools +def cluster_list(xs, tolerance=0): + if tolerance == 0: return [ [x] for x in sorted(xs) ] + if len(xs) < 2: return [ [x] for x in sorted(xs) ] + groups = [] + xs = list(sorted(xs)) + current_group = [xs[0]] + last = xs[0] + for x in xs[1:]: + if x <= (last + tolerance): + current_group.append(x) + else: + groups.append(current_group) + current_group = [x] + last = x + groups.append(current_group) + return groups + +def make_cluster_dict(values, tolerance): + clusters = cluster_list(set(values), tolerance) + + nested_tuples = [ [ (val, i) for val in value_cluster ] + for i, value_cluster in enumerate(clusters) ] + + cluster_dict = dict(itertools.chain(*nested_tuples)) + return cluster_dict + + diff --git a/pdfplumber/utils.py b/pdfplumber/utils.py index df34b0d..6cd313b 100644 --- a/pdfplumber/utils.py +++ b/pdfplumber/utils.py @@ -1,37 +1,5 @@ import pandas as pd -import itertools - -def compatible_iter(thing): - if hasattr(thing, "iterrows"): - return thing.iterrows() - else: - return enumerate(thing) - -def cluster_list(xs, tolerance=0): - if tolerance == 0: return [ [x] for x in sorted(xs) ] - if len(xs) < 2: return [ [x] for x in sorted(xs) ] - groups = [] - xs = list(sorted(xs)) - current_group = [xs[0]] - last = xs[0] - for x in xs[1:]: - if x <= (last + tolerance): - current_group.append(x) - else: - groups.append(current_group) - current_group = [x] - last = x - groups.append(current_group) - return groups - -def make_cluster_dict(values, tolerance): - clusters = cluster_list(set(values), tolerance) - - nested_tuples = [ [ (val, i) for val in value_cluster ] - for i, value_cluster in enumerate(clusters) ] - - cluster_dict = dict(itertools.chain(*nested_tuples)) - return cluster_dict +from pdfplumber import helpers def collate_chars(chars, x_tolerance=0, y_tolerance=0): using_pandas = isinstance(chars, pd.DataFrame) @@ -50,7 +18,7 @@ def collate_chars(chars, x_tolerance=0, y_tolerance=0): coll += char["text"] return coll - doctop_clusters = make_cluster_dict(_chars["doctop"], y_tolerance) + doctop_clusters = helpers.make_cluster_dict(_chars["doctop"], y_tolerance) _chars["doctop_cluster"] = _chars["doctop"].apply(doctop_clusters.get) dc_grp = _chars.sort_values("doctop_cluster").groupby("doctop_cluster") coll = "\n".join(dc_grp.apply(collate_line)) @@ -114,7 +82,7 @@ def extract_columns(chars, collator = lambda x: collate_chars(x, x_tolerance=x_tolerance, y_tolerance=y_tolerance) - doctop_clusters = make_cluster_dict(_chars["doctop"], y_tolerance) + doctop_clusters = helpers.make_cluster_dict(_chars["doctop"], y_tolerance) _chars["doctop_cluster"] = _chars["doctop"].apply(doctop_clusters.get) collated = _chars.groupby([ "doctop_cluster", "column" ])\