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

org.deeplearning4j.models.glove.count.ASCIICoOccurrenceWriter Maven / Gradle / Ivy

There is a newer version: 1.0.0-M2.1
Show newest version
package org.deeplearning4j.models.glove.count;

import lombok.NonNull;
import org.deeplearning4j.models.sequencevectors.sequence.SequenceElement;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;

/**
 * @author [email protected]
 */
public class ASCIICoOccurrenceWriter implements CoOccurrenceWriter {

    private File file;
    private PrintWriter writer;

    public ASCIICoOccurrenceWriter(@NonNull File file) {
        this.file = file;
        try {
            this.writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(file), 10 * 1024 * 1024));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void writeObject(CoOccurrenceWeight object) {
        StringBuilder builder = new StringBuilder(String.valueOf(object.getElement1().getIndex())).append(" ")
                        .append(String.valueOf(object.getElement2().getIndex())).append(" ")
                        .append(String.valueOf(object.getWeight()));
        writer.println(builder.toString());
    }

    @Override
    public void queueObject(CoOccurrenceWeight object) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void finish() {
        try {
            writer.flush();
        } catch (Exception e) {
        }

        try {
            writer.close();
        } catch (Exception e) {
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy