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

org.deeplearning4j.text.sentenceiterator.BasicResultSetIterator Maven / Gradle / Ivy

There is a newer version: 1.0.0-M2.1
Show newest version
package org.deeplearning4j.text.sentenceiterator;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Primitive iterator over a SQL ResultSet
 *
 * Please note: for reset functionality, the underlying JDBC ResultSet must not be of TYPE_FORWARD_ONLY
 * To achieve this using postgres you can make your query using: connection.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
 *
 * This class is designed in a similar fashion to org.deeplearning4j.text.sentenceiterator.BasicLineIterator
 *
 * @author Brad Heap [email protected]
 */
public class BasicResultSetIterator implements SentenceIterator {

    private ResultSet rs;
    private String columnName;

    private SentencePreProcessor preProcessor;

    private boolean nextCalled; // we use this to ensure that next is only called once by hasNext() to ensure we don't skip over data
    private boolean resultOfNext;

    public BasicResultSetIterator(ResultSet rs, String columnName) {
        this.rs = rs;
        this.columnName = columnName;

        this.nextCalled = false;
        this.resultOfNext = false;
    }

    public synchronized String nextSentence() {
        try {
            if (!nextCalled) { // move onto the next row if we haven't yet
                rs.next();
            } else {
                nextCalled = false; // reset that next has been called for next time we call nextSentence() or hasNext()
            }
            return (preProcessor != null) ? this.preProcessor.preProcess(rs.getString(columnName))
                            : rs.getString(columnName);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public synchronized boolean hasNext() {
        try {
            if (!nextCalled) {
                resultOfNext = rs.next();
                nextCalled = true;
            }
            return resultOfNext;
        } catch (SQLException e) {
            return false;
        }
    }

    public synchronized void reset() {
        try {
            rs.beforeFirst();
            nextCalled = false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void finish() {
        try {
            rs.close();
        } catch (SQLException e) {
            // do nothing here
        }
    }

    public SentencePreProcessor getPreProcessor() {
        return preProcessor;
    }

    public void setPreProcessor(SentencePreProcessor preProcessor) {
        this.preProcessor = preProcessor;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy