All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.formkiq.vision.crafter.TextLineExtractor Maven / Gradle / Ivy

/*
 * Copyright (C) 2018 FormKiQ Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.formkiq.vision.crafter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.Range;

import com.formkiq.vision.comparator.DocumentBlockRectangleComparator;
import com.formkiq.vision.document.DocumentBlockRectangle;
import com.formkiq.vision.document.DocumentField;

/**
 * Holder class for Extractor Lines.
 *
 */
public class TextLineExtractor {

    /** {@link Range} {@link Float}. */
    private Range x;
    /** {@link Range} {@link Float}. */
    private Range y;
    /** {@link Collection} {@link DocumentBlockRectangle}. */
    private List rectangles;

    /**
     * constructor.
     * @param xrange {@link Range} {@link Float}
     * @param yrange {@link Range} {@link Float}
     */
    public TextLineExtractor(final Range xrange,
            final Range yrange) {
        this.x = xrange;
        this.y = yrange;
        this.rectangles = new ArrayList<>();
    }

    /**
     * constructor.
     * @param xrange {@link Range} {@link Float}
     * @param yrange {@link Range} {@link Float}
     * @param nl {@link Collection} {@link DocumentBlockRectangle}
     */
    public TextLineExtractor(final Range xrange,
            final Range yrange,
            final Collection nl) {
        this(xrange, yrange);
        setRectangles(new ArrayList<>(nl));
    }

    /**
     * Sort {@link DocumentBlockRectangle}.
     */
    private void sort() {
        this.rectangles.sort(new DocumentBlockRectangleComparator());
    }

    /**
     * Add {@link DocumentBlockRectangle}.
     * @param rect {@link DocumentBlockRectangle}
     */
    public void addRectangle(final DocumentBlockRectangle rect) {
        this.rectangles.add(rect);
//        calculateXRange(this.rectangles);
        sort();
    }

    /**
     * Add {@link DocumentBlockRectangle}.
     * @param rects {@link List} {@link DocumentBlockRectangle}
     */
    public void addRectangles(final List rects) {
        this.rectangles.addAll(rects);
        sort();
    }

    /**
     * @return {@link List} {@link DocumentBlockRectangle}
     */
    public List getRectangles() {
        return this.rectangles;
    }

    /**
     * @return {@link Range} {@link Float}
     */
    public Range getY() {
        return this.y;
    }

    /**
     * @return has {@link DocumentField}
     */
    public boolean hasField() {
        return this.rectangles.stream().filter(t -> t instanceof DocumentField)
                .findFirst().isPresent();
    }

    /**
     * @return boolean
     */
    public boolean isMultiLine() {
        return this.rectangles.size() > 1;
    }

    /**
     * @param rects {@link List} {@link DocumentBlockRectangle}
     */
    public void setRectangles(final List rects) {
        this.rectangles = rects;
        sort();
    }

    @Override
    public String toString() {
        return "y=" + this.y + ", rectangles=" + this.rectangles;
    }

    /**
     * @return {@link Range}
     */
    public Range getX() {
        return this.x;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy