[WIP] Either refactoring
This commit is contained in:
parent
f53f0fea29
commit
066cf17add
@ -8,7 +8,7 @@ from typing import List, Union
|
|||||||
import fitz
|
import fitz
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from funcy import merge, compose, rcompose, keep, lfilter, none
|
from funcy import merge, compose, rcompose, keep
|
||||||
from pymonad.either import Right, Left, Either
|
from pymonad.either import Right, Left, Either
|
||||||
from pymonad.tools import curry, identity
|
from pymonad.tools import curry, identity
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ from image_prediction.info import Info
|
|||||||
from image_prediction.stitching.stitching import stitch_pairs
|
from image_prediction.stitching.stitching import stitch_pairs
|
||||||
from image_prediction.stitching.utils import validate_box
|
from image_prediction.stitching.utils import validate_box
|
||||||
from image_prediction.utils import get_logger
|
from image_prediction.utils import get_logger
|
||||||
from image_prediction.utils.generic import bottom
|
from image_prediction.utils.generic import bottom, left, right
|
||||||
|
|
||||||
logger = get_logger()
|
logger = get_logger()
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ class ParsablePDFImageExtractor(ImageExtractor):
|
|||||||
metadata = extract_valid_metadata(self.doc, page)
|
metadata = extract_valid_metadata(self.doc, page)
|
||||||
|
|
||||||
maybe_image_metadata_pairs = map(partial(metadatum_to_image_metadata_pair, self.doc), metadata)
|
maybe_image_metadata_pairs = map(partial(metadatum_to_image_metadata_pair, self.doc), metadata)
|
||||||
image_metadata_pairs = keep(right, maybe_image_metadata_pairs)
|
image_metadata_pairs = keep(take_right, maybe_image_metadata_pairs)
|
||||||
clear_caches()
|
clear_caches()
|
||||||
|
|
||||||
image_metadata_pairs = stitch_pairs(list(image_metadata_pairs), tolerance=self.tolerance)
|
image_metadata_pairs = stitch_pairs(list(image_metadata_pairs), tolerance=self.tolerance)
|
||||||
@ -58,10 +58,14 @@ class ParsablePDFImageExtractor(ImageExtractor):
|
|||||||
yield from image_metadata_pairs
|
yield from image_metadata_pairs
|
||||||
|
|
||||||
|
|
||||||
def right(pair: Either):
|
def take_right(pair: Either):
|
||||||
if pair.is_right():
|
if pair.is_right():
|
||||||
return pair.either(bottom, identity)
|
return pair.either(bottom, identity)
|
||||||
logger.warning(f"Skipping bad image. {pair.either(identity, bottom)}")
|
logger.warning(f"Skipping bad image. {pair.either(format_context, bottom)}")
|
||||||
|
|
||||||
|
|
||||||
|
def format_context(context):
|
||||||
|
return f"Reason: {context['reason']}. Metadata: {EnumFormatter()(context['metadata'])}"
|
||||||
|
|
||||||
|
|
||||||
def extract_pages(doc, page_range):
|
def extract_pages(doc, page_range):
|
||||||
@ -127,14 +131,16 @@ def xref_to_maybe_image(doc, xref) -> Either:
|
|||||||
|
|
||||||
|
|
||||||
def make_maybe_image_metadata_pair(image: Either, metadata: Either):
|
def make_maybe_image_metadata_pair(image: Either, metadata: Either):
|
||||||
# haskell.org/tutorial/monads.html
|
"""
|
||||||
# (>>) :: m a -> m b -> m b
|
Reference:
|
||||||
either = Right(make_image_metadata_pair).amap(image).amap(metadata)
|
haskell.org/tutorial/monads.html
|
||||||
return (
|
(>>) :: m a -> m b -> m b
|
||||||
Left({"reason": either.either(identity, bottom), "metadata": EnumFormatter()(metadata.value)})
|
"""
|
||||||
if either.is_left()
|
|
||||||
else either
|
def context(value):
|
||||||
)
|
return {"reason": value, "metadata": metadata.either(bottom, identity)}
|
||||||
|
|
||||||
|
return Right(make_image_metadata_pair).amap(image).amap(metadata).either(left(context), right(identity))
|
||||||
|
|
||||||
|
|
||||||
@curry(2)
|
@curry(2)
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
from itertools import starmap
|
from itertools import starmap
|
||||||
|
|
||||||
from funcy import iterate, first, curry, map
|
from funcy import iterate, first, curry, map
|
||||||
|
from pymonad.either import Left, Right
|
||||||
|
|
||||||
|
|
||||||
def until(cond, func, *args, **kwargs):
|
def until(cond, func, *args, **kwargs):
|
||||||
@ -17,3 +18,11 @@ def starlift(fn):
|
|||||||
|
|
||||||
def bottom(*args, **kwargs):
|
def bottom(*args, **kwargs):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def left(fn):
|
||||||
|
return lambda x: Left(fn(x))
|
||||||
|
|
||||||
|
|
||||||
|
def right(fn):
|
||||||
|
return lambda x: Right(fn(x))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user